Skip to content

Commit 6d8d2fc

Browse files
Fix links in documentation and add some additional info for DataFetchingEnvironment selectionSet (#1948)
Fixed links and added info for `selectionSet`. It took me a while to find how to retrieve sub field information, so I figures it might help the next guy. --------- Co-authored-by: Samuel Vazquez <[email protected]>
1 parent c38f1c5 commit 6d8d2fc

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

website/docs/schema-generator/execution/data-fetching-environment.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: data-fetching-environment
33
title: Data Fetching Environment
44
---
55
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/v14/data-fetching/)
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/data-fetching/)
77

88
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
99
during the query execution but will not be included in the schema definition.
@@ -44,4 +44,30 @@ Then the following query would return `"The parentField was foo and the childFie
4444
```
4545

4646
You can also use this to retrieve arguments and query information from higher up the query chain. You can see a working
47-
example in the `graphql-kotlin-spring-example` module [[link](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/query/EnvironmentQuery.kt)].
47+
example in the `graphql-kotlin-spring-example` module [[link](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/examples/server/spring-server/src/main/kotlin/com/expediagroup/graphql/examples/server/spring/query/EnvironmentQuery.kt)].
48+
49+
```kotlin
50+
class ProductQueryService : Query {
51+
52+
fun products(environment: DataFetchingEnvironment): Product {
53+
environment.selectionSet.fields.forEach { println("field: ${it.name}") }
54+
55+
return Product(1, "Product title", 100)
56+
}
57+
}
58+
59+
```
60+
61+
```graphql
62+
{
63+
product {
64+
id
65+
title
66+
price
67+
}
68+
}
69+
```
70+
71+
You can also use `selectionSet` to access the selected fields of the current field. It can be useful to know which selections have been requested so the data fetcher can optimize the data access queries. For example, in an SQL-backed system, the data fetcher can access the database and use the field selection criteria to specifically retrieve only the columns that have been requested by the client.
72+
what selection has been asked for so the data fetcher can optimise the data access queries.
73+
For example an SQL backed system may be able to use the field selection to only retrieve the columns that have been asked for.

0 commit comments

Comments
 (0)