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
In GraphQL, it's common to have input types that mirror output types. For example, you might have a `UserInput` type for creating a user and a `User` type for querying a user. These types might have the same fields but are treated as separate types in GraphQL.
8
+
9
+
With the class consolidation feature, GraphQL Kotlin Codegen can detect when these types are equivalent and consolidate them into a single Kotlin class.
10
+
If this class functions in your resolver code as both an input and an output type, GraphQL Kotlin will subsequently
11
+
transform it into the separate input and output types we started with.
12
+
13
+
## How It Works
14
+
15
+
The class consolidation feature works by comparing the fields of input and output types. If the fields and their types
16
+
are exactly the same, the types are considered equivalent.
17
+
18
+
Here's an example:
19
+
20
+
```graphql
21
+
inputUserInput {
22
+
name: String!
23
+
email: String!
24
+
}
25
+
26
+
typeUser {
27
+
name: String!
28
+
email: String!
29
+
}
30
+
```
31
+
32
+
Inthiscase, `UserInput` and `User` havethesamefields, sotheywouldbeconsolidatedintoasingleKotlinclass:
33
+
34
+
```kotlin
35
+
dataclassUser(
36
+
val name: String,
37
+
val email: String
38
+
)
39
+
```
40
+
41
+
This also works recursively. If the fields of a type are themselves input or output types, they will be consolidated as well.
42
+
43
+
```graphql
44
+
inputUserInput {
45
+
name: NameInput!
46
+
email: String!
47
+
}
48
+
49
+
inputNameInput {
50
+
first: String!
51
+
last: String!
52
+
}
53
+
54
+
typeUser {
55
+
name: Name!
56
+
email: String!
57
+
}
58
+
59
+
typeName {
60
+
first: String!
61
+
last: String!
62
+
}
63
+
```
64
+
65
+
```kotlin
66
+
dataclassUser(
67
+
val name: Name,
68
+
val email: String
69
+
)
70
+
71
+
dataclassName(
72
+
val first: String,
73
+
val last: String
74
+
)
75
+
```
76
+
77
+
## Limitations
78
+
79
+
The class consolidation feature only works with types that have the same fields with the same types.
80
+
If the fields are different, the types will not be consolidated. Instead, individual classes will be generated with the
81
+
`@GraphQLValidObjectLocations` annotation, enforcing that the class can only be used as either an input or output type.
82
+
Check out the [GraphQL Kotlin docs](https://opensource.expediagroup.com/graphql-kotlin/docs/schema-generator/customizing-schemas/restricting-input-output)
Copy file name to clipboardExpand all lines: src/config.ts
+65-54Lines changed: 65 additions & 54 deletions
Original file line number
Diff line number
Diff line change
@@ -24,28 +24,51 @@ import {
24
24
import{Kind}from"graphql";
25
25
26
26
exportconstconfigSchema=object({
27
-
/**
28
-
* The package name for the generated file. Defaults to the directory containing the generated file.
29
-
* @example "com.example.generated"
30
-
*/
31
-
packageName: optional(string()),
32
-
/**
33
-
* Only generate types matching those in this list. If empty, no types will be generated. If omitted, all types will be generated.
34
-
* @example ["MyType", "MyEnum"]
35
-
*/
36
-
onlyTypes: optional(array(string())),
37
-
/**
38
-
* Determines whether to generate dependent types from types listed in `onlyTypes`. Defaults to `true`.
39
-
*/
40
-
includeDependentTypes: optional(boolean()),
41
27
/**
42
28
* Limits dependent types to include from `onlyTypes` list. Can be used to exclude classes that are imported from external packages.
43
-
* @description If `MyType` depends on `MyDependentType1` and `MyDependentType2`, we can allow `MyDependentType2` to be imported externally by including its import in `extraImports` and omitting it in the `dependentTypesInScope` list: `["MyType", "MyDependentType1"]`
29
+
*
30
+
* If `MyType` depends on `MyDependentType1` and `MyDependentType2`, we can allow `MyDependentType2` to be imported
31
+
* externally by including its import in `extraImports` and omitting it in the `dependentTypesInScope` list.
32
+
* @example ["MyType", "MyDependentType1"]
44
33
*/
45
34
dependentTypesInScope: optional(array(string())),
35
+
/**
36
+
* Denotes Kotlin annotations to replace GraphQL directives.
37
+
*
38
+
* `directive` is the name of the directive to replace, and `kotlinAnnotations` is a list of Kotlin annotations to replace the directive with.
39
+
*
40
+
* Use `argumentsToRetain` to forward arguments from the directive directly to the Kotlin annotation. Can be INT, FLOAT, STRING, BOOLEAN, or ENUM. ```@YourGraphQLDirective(arg1: "value1") -> @YourKotlinAnnotation(arg1 = "value1")```
41
+
*
42
+
* Use `definitionType` to apply the directive replacement to a specific kind of type definition. If omitted, the replacement will apply to all definition types.
* Denotes Kotlin classes representing union types to be treated as interfaces rather than annotation classes.
48
-
* @description This should be used for types outside `dependentTypesInScope` that are not generated by the plugin. Only use when unionGeneration is set to `ANNOTATION_CLASS`.
69
+
*
70
+
* This should be used for types outside `dependentTypesInScope` that are not generated by the plugin.
71
+
* Only use when unionGeneration is set to `ANNOTATION_CLASS`.
0 commit comments