|
1 |
| ---- |
2 |
| -id: getting-started |
3 |
| -title: Getting Started |
4 |
| ---- |
5 |
| -## Installation |
6 |
| - |
7 |
| -Using a JVM dependency manager, simply link `graphql-kotlin-schema-generator` to your project. You can see the latest |
8 |
| -version and other examples in [Sonatype Central |
9 |
| -Repository](https://search.maven.org/artifact/com.expediagroup/graphql-kotlin-schema-generator) |
10 |
| - |
11 |
| -With Maven: |
12 |
| - |
13 |
| -```xml |
14 |
| -<dependency> |
15 |
| - <groupId>com.expediagroup</groupId> |
16 |
| - <artifactId>graphql-kotlin-schema-generator</artifactId> |
17 |
| - <version>${latestVersion}</version> |
18 |
| -</dependency> |
19 |
| -``` |
20 |
| - |
21 |
| -With Gradle: |
22 |
| - |
23 |
| -```groovy |
24 |
| -compile(group: 'com.expediagroup', name: 'graphql-kotlin-schema-generator', version: "$latestVersion") |
25 |
| -``` |
26 |
| - |
27 |
| -## Generating a schema |
28 |
| - |
29 |
| -`graphql-kotlin-schema-generator` provides a single function, `toSchema`, to generate a schema from Kotlin objects. |
30 |
| - |
31 |
| -```kotlin |
32 |
| -class Query { |
33 |
| - fun getNumber() = 1 |
34 |
| -} |
35 |
| - |
36 |
| -val config = SchemaGeneratorConfig(listOf("com.expediagroup")) |
37 |
| -val queries = listOf(TopLevelObject(Query())) |
38 |
| -val schema: GraphQLSchema = toSchema(config = config, queries = queries) |
39 |
| -``` |
40 |
| - |
41 |
| -generates a `GraphQLSchema` with IDL that looks like this: |
42 |
| - |
43 |
| -```graphql |
44 |
| -schema { |
45 |
| - query: Query |
46 |
| -} |
47 |
| - |
48 |
| -type Query { |
49 |
| - getNumber: Int! |
50 |
| -} |
51 |
| -``` |
52 |
| - |
53 |
| -The `GraphQLSchema` generated can be used to expose a GraphQL API endpoint. |
54 |
| - |
55 |
| -### `toSchema` |
56 |
| - |
57 |
| -This function accepts four arguments: `config`, `queries`, `mutations` and `subscriptions`. The `queries`, `mutations` |
58 |
| -and `subscriptions` are a list of `TopLevelObject`s and will be used to generate corresponding GraphQL root types. See |
59 |
| -below on why we use this wrapper class. The `config` contains all the extra information you need to pass, including |
60 |
| -custom hooks, supported packages, and name overrides. See the full documentation: [Spring Configuration](spring-config). |
61 |
| - |
62 |
| -You can see the definition for `toSchema` [in the |
63 |
| -source](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/toSchema.kt) |
64 |
| - |
65 |
| -### Class `TopLevelObject` |
66 |
| - |
67 |
| -`toSchema` uses Kotlin reflection to build a GraphQL schema from given classes using `graphql-java`'s schema builder. We |
68 |
| -don't just pass a `KClass` though, we have to actually pass an object, because the functions on the object are |
69 |
| -transformed into the data fetchers. In most cases, a `TopLevelObject` can be constructed with just an object: |
70 |
| - |
71 |
| -```kotlin |
72 |
| -class Query { |
73 |
| - fun getNumber() = 1 |
74 |
| -} |
75 |
| - |
76 |
| -val topLevelObject = TopLevelObject(Query()) |
77 |
| - |
78 |
| -toSchema(config = config, queries = listOf(topLevelObject)) |
79 |
| -``` |
80 |
| - |
81 |
| -In the above case, `toSchema` will use `topLevelObject::class` as the reflection target, and `Query` as the data fetcher |
82 |
| -target. |
83 |
| - |
84 |
| -In a lot of cases, such as with Spring AOP, the object (or bean) being used to generate a schema is a dynamic proxy. In |
85 |
| -this case, `topLevelObject::class` is not `Query`, but rather a generated class that will confuse the schema generator. |
86 |
| -To specify the `KClass` to use for reflection on a proxy, pass the class to `TopLevelObject`: |
87 |
| - |
88 |
| -```kotlin |
89 |
| -@Component |
90 |
| -class Query { |
91 |
| - @Timed |
92 |
| - fun getNumber() = 1 |
93 |
| -} |
94 |
| - |
95 |
| -val query = getObjectFromBean() |
96 |
| -val customDef = TopLevelObject(query, Query::class) |
97 |
| - |
98 |
| -toSchema(config, listOf(customDef)) |
99 |
| -``` |
| 1 | +--- |
| 2 | +id: getting-started |
| 3 | +title: Getting Started |
| 4 | +--- |
| 5 | + |
| 6 | +GraphQL Kotlin consists of number of libraries that aim to simplify GraphQL integration for Kotlin applications. |
| 7 | + |
| 8 | +## Modules |
| 9 | + |
| 10 | +* [graphql-kotlin-federation](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/graphql-kotlin-federation) |
| 11 | + — Schema generator extension to build federated GraphQL schemas |
| 12 | +* [graphql-kotlin-schema-generator](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/graphql-kotlin-schema-generator) |
| 13 | + — Code only GraphQL schema generation for Kotlin |
| 14 | +* [graphql-kotlin-spring-server](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/graphql-kotlin-spring-server) |
| 15 | + — Spring Boot auto-configuration library to create GraphQL web app |
| 16 | +* [examples](https://github.com/ExpediaGroup/graphql-kotlin/tree/master/examples) |
| 17 | + — Example apps that use graphql-kotlin libraries to test and demonstrate usages |
| 18 | + |
| 19 | +If you encounter any problems using this library please open up a new |
| 20 | +[Issue](https://github.com/ExpediaGroup/graphql-kotlin/issues) |
| 21 | + |
| 22 | +Additional resources |
| 23 | + |
| 24 | +* [GraphQL](https://graphql.org/) |
| 25 | +* [graphql-java](https://www.graphql-java.com/documentation/) |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +Using a JVM dependency manager, simply link `graphql-kotlin-schema-generator` to your project. You can see the latest |
| 30 | +version and other examples in [Sonatype Central |
| 31 | +Repository](https://search.maven.org/artifact/com.expediagroup/graphql-kotlin-schema-generator) |
| 32 | + |
| 33 | +With Maven: |
| 34 | + |
| 35 | +```xml |
| 36 | +<dependency> |
| 37 | + <groupId>com.expediagroup</groupId> |
| 38 | + <artifactId>graphql-kotlin-schema-generator</artifactId> |
| 39 | + <version>${latestVersion}</version> |
| 40 | +</dependency> |
| 41 | +``` |
| 42 | + |
| 43 | +With Gradle: |
| 44 | + |
| 45 | +```groovy |
| 46 | +compile(group: 'com.expediagroup', name: 'graphql-kotlin-schema-generator', version: "$latestVersion") |
| 47 | +``` |
| 48 | + |
| 49 | +## Generating a schema |
| 50 | + |
| 51 | +`graphql-kotlin-schema-generator` provides a single function, `toSchema`, to generate a schema from Kotlin objects. |
| 52 | + |
| 53 | +```kotlin |
| 54 | +class Query { |
| 55 | + fun getNumber() = 1 |
| 56 | +} |
| 57 | + |
| 58 | +val config = SchemaGeneratorConfig(listOf("com.expediagroup")) |
| 59 | +val queries = listOf(TopLevelObject(Query())) |
| 60 | +val schema: GraphQLSchema = toSchema(config = config, queries = queries) |
| 61 | +``` |
| 62 | + |
| 63 | +generates a `GraphQLSchema` with IDL that looks like this: |
| 64 | + |
| 65 | +```graphql |
| 66 | +schema { |
| 67 | + query: Query |
| 68 | +} |
| 69 | + |
| 70 | +type Query { |
| 71 | + getNumber: Int! |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +The `GraphQLSchema` generated can be used to expose a GraphQL API endpoint. |
| 76 | + |
| 77 | +### `toSchema` |
| 78 | + |
| 79 | +This function accepts four arguments: `config`, `queries`, `mutations` and `subscriptions`. The `queries`, `mutations` |
| 80 | +and `subscriptions` are a list of `TopLevelObject`s and will be used to generate corresponding GraphQL root types. See |
| 81 | +below on why we use this wrapper class. The `config` contains all the extra information you need to pass, including |
| 82 | +custom hooks, supported packages, and name overrides. See the full documentation: [Spring Configuration](spring-config). |
| 83 | + |
| 84 | +You can see the definition for `toSchema` [in the |
| 85 | +source](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/toSchema.kt) |
| 86 | + |
| 87 | +### Class `TopLevelObject` |
| 88 | + |
| 89 | +`toSchema` uses Kotlin reflection to build a GraphQL schema from given classes using `graphql-java`'s schema builder. We |
| 90 | +don't just pass a `KClass` though, we have to actually pass an object, because the functions on the object are |
| 91 | +transformed into the data fetchers. In most cases, a `TopLevelObject` can be constructed with just an object: |
| 92 | + |
| 93 | +```kotlin |
| 94 | +class Query { |
| 95 | + fun getNumber() = 1 |
| 96 | +} |
| 97 | + |
| 98 | +val topLevelObject = TopLevelObject(Query()) |
| 99 | + |
| 100 | +toSchema(config = config, queries = listOf(topLevelObject)) |
| 101 | +``` |
| 102 | + |
| 103 | +In the above case, `toSchema` will use `topLevelObject::class` as the reflection target, and `Query` as the data fetcher |
| 104 | +target. |
| 105 | + |
| 106 | +In a lot of cases, such as with Spring AOP, the object (or bean) being used to generate a schema is a dynamic proxy. In |
| 107 | +this case, `topLevelObject::class` is not `Query`, but rather a generated class that will confuse the schema generator. |
| 108 | +To specify the `KClass` to use for reflection on a proxy, pass the class to `TopLevelObject`: |
| 109 | + |
| 110 | +```kotlin |
| 111 | +@Component |
| 112 | +class Query { |
| 113 | + @Timed |
| 114 | + fun getNumber() = 1 |
| 115 | +} |
| 116 | + |
| 117 | +val query = getObjectFromBean() |
| 118 | +val customDef = TopLevelObject(query, Query::class) |
| 119 | + |
| 120 | +toSchema(config, listOf(customDef)) |
| 121 | +``` |
0 commit comments