|
1 |
| ---- |
2 |
| -id: generator-config |
3 |
| -title: Schema Generator Configuration |
4 |
| ---- |
5 |
| - |
6 |
| -`graphql-kotlin-schema-generator` provides a single function, `toSchema,` to generate a schema from Kotlin objects. This |
7 |
| -function accepts four arguments: config, queries, mutations and subscriptions. The queries, mutations and subscriptions |
8 |
| -are a list of |
9 |
| -[TopLevelObjects](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/TopLevelObject.kt) |
10 |
| -and will be used to generate corresponding GraphQL root types. The |
11 |
| -[config](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/SchemaGeneratorConfig.kt) |
12 |
| -contains all the extra information you need to pass, including custom hooks, supported packages and name overrides. |
13 |
| -`SchemaGeneratorConfig` has some default settings but you can override them and add custom behaviors for generating your |
14 |
| -schema. |
15 |
| - |
16 |
| -* `supportedPackages` **[Required]** - List of Kotlin packages that can contain schema objects. Limits the scope of |
17 |
| - packages that can be scanned using reflections. |
18 |
| -* `topLevelNames` _[Optional]_ - Set the name of the top level GraphQL fields, defaults to `Query`, `Mutation` and |
19 |
| - `Subscription` |
20 |
| -* `hooks` _[Optional]_ - Set custom behaviors for generating the schema, see below for details. |
21 |
| -* `dataFetcherFactory` _[Optional]_ - Sets custom behavior for generating data fetchers |
22 |
| - |
23 |
| -## Schema generator hooks |
24 |
| - |
25 |
| -Hooks are lifecycle events that are called and triggered while the schema is building that allow users to customize the |
26 |
| -schema. |
27 |
| - |
28 |
| -For exact names and details of every hook, see the comments and descriptions in our latest |
29 |
| -[javadocs](https://www.javadoc.io/doc/com.expediagroup/graphql-kotlin-schema-generator) or directly in the source file: |
30 |
| -[SchemaGeneratorHooks.kt](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/hooks/SchemaGeneratorHooks.kt) |
31 |
| - |
32 |
| -As an example here is how you would write a custom hook and provide it through the configuration |
33 |
| - |
34 |
| -```kotlin |
35 |
| -class MyCustomHooks : SchemaGeneratorHooks { |
36 |
| - // Only generate functions that start with "dog" |
37 |
| - // This would probably be better just to use @GraphQLIgnore, but this is just an example |
38 |
| - override fun isValidFunction(function: KFunction<*>) = function.name.startsWith("dog") |
39 |
| -} |
40 |
| - |
41 |
| -class Query { |
42 |
| - fun dogSound() = "bark" |
43 |
| - |
44 |
| - fun catSound() = "meow" |
45 |
| -} |
46 |
| - |
47 |
| -val config = SchemaGeneratorConfig(supportedPackages = listOf("org.example"), hooks = MyCustomHooks()) |
48 |
| - |
49 |
| -val queries = listOf(TopLevelObject(Query())) |
50 |
| - |
51 |
| -toSchema(queries = queries, config = config) |
52 |
| -``` |
53 |
| - |
54 |
| -will generate |
55 |
| - |
56 |
| -```graphql |
57 |
| - |
58 |
| -schema { |
59 |
| - query: Query |
60 |
| -} |
61 |
| - |
62 |
| -type Query { |
63 |
| - dogSound: String! |
64 |
| -} |
65 |
| -``` |
66 |
| - |
67 |
| -Notice there is no `catSound` function. |
| 1 | +--- |
| 2 | +id: generator-config |
| 3 | +title: Generator Configuration |
| 4 | +--- |
| 5 | + |
| 6 | +`graphql-kotlin-schema-generator` provides a single function, `toSchema,` to generate a schema from Kotlin objects. This |
| 7 | +function accepts four arguments: config, queries, mutations and subscriptions. The queries, mutations and subscriptions |
| 8 | +are a list of |
| 9 | +[TopLevelObjects](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/TopLevelObject.kt) |
| 10 | +and will be used to generate corresponding GraphQL root types. The |
| 11 | +[config](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/SchemaGeneratorConfig.kt) |
| 12 | +contains all the extra information you need to pass, including custom hooks, supported packages and name overrides. |
| 13 | +`SchemaGeneratorConfig` has some default settings but you can override them and add custom behaviors for generating your |
| 14 | +schema. |
| 15 | + |
| 16 | +* `supportedPackages` **[Required]** - List of Kotlin packages that can contain schema objects. Limits the scope of |
| 17 | + packages that can be scanned using reflections. |
| 18 | +* `topLevelNames` _[Optional]_ - Set the name of the top level GraphQL fields, defaults to `Query`, `Mutation` and |
| 19 | + `Subscription` |
| 20 | +* `hooks` _[Optional]_ - Set custom behaviors for generating the schema, see below for details. |
| 21 | +* `dataFetcherFactory` _[Optional]_ - Sets custom behavior for generating data fetchers |
| 22 | + |
| 23 | +## Schema generator hooks |
| 24 | + |
| 25 | +Hooks are lifecycle events that are called and triggered while the schema is building that allow users to customize the |
| 26 | +schema. |
| 27 | + |
| 28 | +For exact names and details of every hook, see the comments and descriptions in our latest |
| 29 | +[javadocs](https://www.javadoc.io/doc/com.expediagroup/graphql-kotlin-schema-generator) or directly in the source file: |
| 30 | +[SchemaGeneratorHooks.kt](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/hooks/SchemaGeneratorHooks.kt) |
| 31 | + |
| 32 | +As an example here is how you would write a custom hook and provide it through the configuration |
| 33 | + |
| 34 | +```kotlin |
| 35 | +class MyCustomHooks : SchemaGeneratorHooks { |
| 36 | + // Only generate functions that start with "dog" |
| 37 | + // This would probably be better just to use @GraphQLIgnore, but this is just an example |
| 38 | + override fun isValidFunction(function: KFunction<*>) = function.name.startsWith("dog") |
| 39 | +} |
| 40 | + |
| 41 | +class Query { |
| 42 | + fun dogSound() = "bark" |
| 43 | + |
| 44 | + fun catSound() = "meow" |
| 45 | +} |
| 46 | + |
| 47 | +val config = SchemaGeneratorConfig(supportedPackages = listOf("org.example"), hooks = MyCustomHooks()) |
| 48 | + |
| 49 | +val queries = listOf(TopLevelObject(Query())) |
| 50 | + |
| 51 | +toSchema(queries = queries, config = config) |
| 52 | +``` |
| 53 | + |
| 54 | +will generate |
| 55 | + |
| 56 | +```graphql |
| 57 | + |
| 58 | +schema { |
| 59 | + query: Query |
| 60 | +} |
| 61 | + |
| 62 | +type Query { |
| 63 | + dogSound: String! |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +Notice there is no `catSound` function. |
0 commit comments