You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you can see, the generated code is not part of the implementation. Rather, it becomes an interface to inherit from in your implementation.
56
+
This enforces a type contract between the schema and your resolver code.
57
+
58
+
## Top Level Types
59
+
60
+
When dealing with top-level types, such as `Query` or `Mutation`, you can inherit from the generated class directly. This is because the generated class is open by default.
This is because the generated `Query` class contains both `foo` and `bar` fields, which causes a conflict when inherited by multiple implementation classes.
101
+
102
+
Instead, you should inherit from the field-level generated `Query` classes like so:
Copy file name to clipboardExpand all lines: docs/docs/recommended-usage.md
+15-16Lines changed: 15 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -21,8 +21,8 @@ type Query {
21
21
}
22
22
23
23
typeMyType {
24
-
field1: String!
25
-
field2: String
24
+
foo: String!
25
+
bar: String
26
26
}
27
27
```
28
28
@@ -38,8 +38,8 @@ open class Query {
38
38
}
39
39
40
40
dataclassMyType(
41
-
val field1: String,
42
-
val field2: String? = null
41
+
val foo: String,
42
+
val bar: String? = null
43
43
)
44
44
```
45
45
@@ -53,16 +53,15 @@ import com.types.generated.Query as QueryInterface
53
53
classMyQuery : Query, QueryInterface() {
54
54
overridefunresolveMyType(input:String): MyType=
55
55
MyType(
56
-
field1= myExpensiveCall1(),
57
-
field2= myExpensiveCall2()
56
+
foo= myExpensiveCall1(),
57
+
bar= myExpensiveCall2()
58
58
)
59
59
}
60
-
61
60
```
62
61
63
62
The resulting source code is extremely unperformant. The `MyType` class is a data class, which means
64
-
that the `field1` and `field2` properties are both initialized when the `MyType` object is created, and
65
-
`myExpensiveCall1()` and `myExpensiveCall2()` will both be called in sequence! Even if I only query for `field1`, not
63
+
that the `foo` and `bar` properties are both initialized when the `MyType` object is created, and
64
+
`myExpensiveCall1()` and `myExpensiveCall2()` will both be called in sequence! Even if I only query for `foo`, not
66
65
only will `myExpensiveCall2()` still run, but it will also wait until `myExpensiveCall1()` is totally finished.
67
66
68
67
### Instead, use the `resolverInterfaces` config!
@@ -91,8 +90,8 @@ open class Query {
91
90
}
92
91
93
92
openclassMyType {
94
-
openfunfield1(): String=throwNotImplementedError("MyType.field1 must be implemented.")
95
-
openfunfield2(): String?=throwNotImplementedError("MyType.field2 must be implemented.")
93
+
openfunfoo(): String=throwNotImplementedError("MyType.foo must be implemented.")
94
+
openfunbar(): String?=throwNotImplementedError("MyType.bar must be implemented.")
96
95
}
97
96
```
98
97
@@ -108,13 +107,13 @@ class MyQuery : Query, QueryInterface() {
108
107
109
108
@GraphQLIgnore
110
109
classMyType : MyTypeInterface() {
111
-
overridefunfield1(): String= myExpensiveCall1()
112
-
overridefunfield2(): String?= myExpensiveCall2()
110
+
overridefunfoo(): String= myExpensiveCall1()
111
+
overridefunbar(): String?= myExpensiveCall2()
113
112
}
114
113
```
115
114
116
-
This code is much more performant! The `MyType` class is no longer a data class, so the `field1` and `field2` properties
117
-
can now be resolved independently of each other. If I query for only `field1`, only `myExpensiveCall1()` will be called, and
118
-
if I query for only `field2`, only `myExpensiveCall2()` will be called.
115
+
This code is much more performant! The `MyType` class is no longer a data class, so the `foo` and `bar` properties
116
+
can now be resolved independently of each other. If I query for only `foo`, only `myExpensiveCall1()` will be called, and
117
+
if I query for only `bar`, only `myExpensiveCall2()` will be called.
119
118
120
119
Check out the [related GraphQL Kotlin docs](https://opensource.expediagroup.com/graphql-kotlin/docs/schema-generator/execution/fetching-data/) for more information on this topic.
0 commit comments