|
1 | | ---- |
2 | | -id: data-fetching-environment |
3 | | -title: Data Fetching Environment |
4 | | ---- |
5 | | -Each resolver has a `DataFetchingEnvironment` that can be accessed from graphql-java: |
6 | | -https://www.graphql-java.com/documentation/v13/data-fetching/ |
7 | | - |
8 | | -You can access this info by including the `DataFetchingEnvironment` as one of the arguments to a Kotlin function. This |
9 | | -argument will not be included in the schema. |
10 | | - |
11 | | -```kotlin |
12 | | -class Query { |
13 | | - fun printEnvironmentInfo(environment: DataFetchingEnvironment, value: Int): String { |
14 | | - // Access env data |
15 | | - } |
16 | | -} |
17 | | -``` |
18 | | - |
19 | | -This will produce the following schema |
20 | | - |
21 | | -```graphql |
22 | | -type Query { |
23 | | - printEnvironmentInfo(value: Int!): String! |
24 | | -} |
25 | | -``` |
26 | | - |
27 | | -You can also use this to retrieve arguments and query information from higher up the query chain. You can see a working |
28 | | -example in the `graphql-kotlin-spring-example` module |
29 | | -[link](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/query/EnvironmentQuery.kt)]. |
| 1 | +--- |
| 2 | +id: data-fetching-environment |
| 3 | +title: Data Fetching Environment |
| 4 | +--- |
| 5 | +Each resolver has access to a `DataFetchingEnvironment` that provides additional information about the currently executed query including information about what data is requested |
| 6 | +as well as details about current execution state. For more details on the `DataFetchingEnvironment` please refer to [graphql-java documentation](https://www.graphql-java.com/documentation/v13/data-fetching/) |
| 7 | + |
| 8 | +You can access this info by including the `DataFetchingEnvironment` as one of the arguments to a Kotlin function. This argument will be automatically populated and injected |
| 9 | +during the query execution but will not be included in the schema definition. |
| 10 | + |
| 11 | +```kotlin |
| 12 | +class Query { |
| 13 | + fun printEnvironmentInfo(environment: DataFetchingEnvironment): String { |
| 14 | + // access env data |
| 15 | + } |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +This will produce the following schema |
| 20 | + |
| 21 | +```graphql |
| 22 | +type Query { |
| 23 | + printEnvironmentInfo: String! |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +You can also use this to retrieve arguments and query information from higher up the query chain. You can see a working |
| 28 | +example in the `graphql-kotlin-spring-example` module |
| 29 | +[[link](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/query/EnvironmentQuery.kt)]. |
| 30 | + |
| 31 | + |
0 commit comments