|
| 1 | +--- |
| 2 | +sidebar_position: 6 |
| 3 | +--- |
| 4 | + |
| 5 | +# Inheritance |
| 6 | + |
| 7 | +When dealing with GraphQL schema containing [field arguments](https://graphql.com/learn/arguments/), |
| 8 | +generating Kotlin code can be a bit tricky. This is because the generated code cannot in itself be the implementation |
| 9 | +in a resolver. |
| 10 | + |
| 11 | +Here's an example: |
| 12 | + |
| 13 | +```graphql |
| 14 | +type Query { |
| 15 | + resolveMyType: MyType! |
| 16 | +} |
| 17 | + |
| 18 | +type MyType { |
| 19 | + resolveMe(input: String!): String! |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +Generated Kotlin: |
| 24 | + |
| 25 | +```kotlin |
| 26 | +package com.types.generated |
| 27 | + |
| 28 | +open class Query { |
| 29 | + open fun resolveMyType(): MyType = throw NotImplementedError("Query.resolveMyType must be implemented.") |
| 30 | +} |
| 31 | + |
| 32 | +open class MyType { |
| 33 | + open fun resolveMe(input: String): String = throw NotImplementedError("MyType.resolveMe must be implemented.") |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Source code: |
| 38 | + |
| 39 | +```kotlin |
| 40 | +import com.expediagroup.graphql.server.operations.Query |
| 41 | +import com.types.generated.MyType as MyTypeInterface |
| 42 | +import com.types.generated.Query as QueryInterface |
| 43 | + |
| 44 | +class MyQuery : Query, QueryInterface() { |
| 45 | + override fun resolveMyType(): MyTypeInterface = MyType() |
| 46 | +} |
| 47 | + |
| 48 | +class MyType : MyTypeInterface() { |
| 49 | + override fun resolveMe(input: String): String = "Hello world!" |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +As you can see, the generated code is not part of the implementation. Rather, it becomes an interface to inherit from in your implementation. |
| 54 | +This enforces a type contract between the schema and your resolver code. |
| 55 | + |
| 56 | +Note that GraphQL Kotlin will use the implementation classes to generate the schema, not the generated interfaces. |
| 57 | +This means that all `@GraphQLDescription` and `@Deprecated` annotations have to be added to implementation classes |
| 58 | +in order to be propagated to the resulting schema. |
| 59 | + |
| 60 | +## Top Level Types |
| 61 | + |
| 62 | +When dealing with top-level types, i.e. `Query` and `Mutation`, you can inherit from the corresponding generated class |
| 63 | +to enforce the type contract. This is fine as long as all of your resolvers are contained in the same Query or Mutation class. |
| 64 | + |
| 65 | +```graphql |
| 66 | +type Query { |
| 67 | + foo: String! |
| 68 | + bar: String! |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +```kotlin |
| 73 | +import com.expediagroup.graphql.server.operations.Query |
| 74 | +import com.types.generated.Query as QueryInterface |
| 75 | + |
| 76 | +class MyQuery : Query, QueryInterface() { |
| 77 | + override fun foo(): String = "Hello" |
| 78 | + override fun bar(): String = "World" |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +However, you might want to separate the implementation into multiple classes like so: |
| 83 | + |
| 84 | +```kotlin |
| 85 | +import com.expediagroup.graphql.server.operations.Query |
| 86 | + |
| 87 | +class FooQuery : Query { |
| 88 | + override fun foo(): String = "Hello" |
| 89 | +} |
| 90 | + |
| 91 | +class BarQuery : Query { |
| 92 | + override fun bar(): String = "World" |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +If you try to inherit from the generated `Query` class, you will get an error during schema generation. |
| 97 | + |
| 98 | +```kotlin |
| 99 | +import com.expediagroup.graphql.server.operations.Query |
| 100 | +import com.types.generated.Query as QueryInterface |
| 101 | + |
| 102 | +class FooQuery : Query, QueryInterface() { |
| 103 | + override fun foo(): String = "Hello" |
| 104 | +} |
| 105 | + |
| 106 | +class BarQuery : Query, QueryInterface() { |
| 107 | + override fun bar(): String = "World" |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +This is because the generated `Query` class contains both `foo` and `bar` fields, which creates a conflict when inherited by multiple implementation classes. |
| 112 | + |
| 113 | +Instead, you should inherit from the field-level generated `Query` classes like so: |
| 114 | + |
| 115 | +```kotlin |
| 116 | +import com.expediagroup.graphql.server.operations.Query |
| 117 | +import com.types.generated.FooQuery as FooQueryInterface |
| 118 | +import com.types.generated.BarQuery as BarQueryInterface |
| 119 | + |
| 120 | +class FooQuery : Query, FooQueryInterface() { |
| 121 | + override fun foo(): String = "Hello" |
| 122 | +} |
| 123 | + |
| 124 | +class BarQuery : Query, BarQueryInterface() { |
| 125 | + override fun bar(): String = "World" |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +This way, schema generation can complete without conflict, and you can separate your implementation into multiple classes! |
0 commit comments