Skip to content

Commit 8e6a198

Browse files
committed
Update datafetching examples to use GraphQLContext
1 parent c9b24ab commit 8e6a198

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

documentation/data-fetching.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ It might look like the following :
4444
DataFetcher productsDataFetcher = new DataFetcher<List<ProductDTO>>() {
4545
@Override
4646
public List<ProductDTO> get(DataFetchingEnvironment environment) {
47-
DatabaseSecurityCtx ctx = environment.getContext();
47+
DatabaseSecurityCtx ctx = environment.getGraphQlContext().get("databaseSecurityCtx");
4848
4949
List<ProductDTO> products;
5050
String match = environment.getArgument("match");
@@ -62,7 +62,7 @@ Each ``DataFetcher`` is passed a ``graphql.schema.DataFetchingEnvironment`` obje
6262
arguments have been supplied to the field and other information such as the field's type, its parent type, the query root object or the query
6363
context object.
6464
65-
Note how the data fetcher code above uses the ``context`` object as an application specific security handle to get access
65+
Note how the data fetcher code above uses the context object as an application specific security handle to get access
6666
to the database. This is a common technique to provide lower layer calling context.
6767
6868
Once we have a list of ``ProductDTO`` objects we typically don't need specialised data fetchers on each field. graphql-java
@@ -146,14 +146,13 @@ top level fields. The root object never changes during the query and it may be
146146
arguments that have been resolved from passed in variables, AST literals and default argument values. You use the arguments
147147
of a field to control what values it returns.
148148
149-
* ``<T> T getContext()`` - the context object is set up when the query is first executed and stays the same over the lifetime
149+
* ``<T> T getGraphQLContext()`` - the context object is set up when the query is first executed and stays the same over the lifetime
150150
of the query. The context can be any value and is typically used to give each data fetcher some calling context needed
151151
when trying to get field data. For example the current user credentials or the database connection parameters could be contained
152-
with a ``context`` object so that data fetchers can make business layer calls. One of the key design decisions you have as a graphql
152+
with a context object so that data fetchers can make business layer calls. One of the key design decisions you have as a graphql
153153
system designer is how you will use context in your fetchers if at all. Some people use a dependency framework that injects context into
154154
data fetchers automatically and hence don't need to use this.
155155
156-
157156
* ``ExecutionStepInfo getExecutionStepInfo()`` - the field type information is a catch all bucket of field type information that is built up as
158157
the query is executed. The following section explains more on this.
159158

versioned_docs/version-v22/data-fetching.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ It might look like the following :
4444
DataFetcher productsDataFetcher = new DataFetcher<List<ProductDTO>>() {
4545
@Override
4646
public List<ProductDTO> get(DataFetchingEnvironment environment) {
47-
DatabaseSecurityCtx ctx = environment.getContext();
47+
DatabaseSecurityCtx ctx = environment.getGraphQlContext().get("databaseSecurityCtx");
4848
4949
List<ProductDTO> products;
5050
String match = environment.getArgument("match");
@@ -62,7 +62,7 @@ Each ``DataFetcher`` is passed a ``graphql.schema.DataFetchingEnvironment`` obje
6262
arguments have been supplied to the field and other information such as the field's type, its parent type, the query root object or the query
6363
context object.
6464
65-
Note how the data fetcher code above uses the ``context`` object as an application specific security handle to get access
65+
Note how the data fetcher code above uses the context object as an application specific security handle to get access
6666
to the database. This is a common technique to provide lower layer calling context.
6767
6868
Once we have a list of ``ProductDTO`` objects we typically don't need specialised data fetchers on each field. graphql-java
@@ -135,33 +135,32 @@ Every data fetcher is passed a ``graphql.schema.DataFetchingEnvironment`` object
135135
and what arguments have been provided. Here are some of the more interesting parts of ``DataFetchingEnvironment``.
136136
137137
* ``<T> T getSource()`` - the ``source`` object is used to get information for a field. Its the object that is the result
138-
of the parent field fetch. In the common case it is an in memory DTO object and hence simple POJO getters will be used for fields values. In more complex cases, you may examine it to know
139-
how to get the specific information for the current field. As the graphql field tree is executed, each returned field value
140-
becomes the ``source`` object for child fields.
138+
of the parent field fetch. In the common case it is an in memory DTO object and hence simple POJO getters will be used for fields values. In more complex cases, you may examine it to know
139+
how to get the specific information for the current field. As the graphql field tree is executed, each returned field value
140+
becomes the ``source`` object for child fields.
141141
142142
* ``<T> T getRoot()`` - this special object is used to seed the graphql query. The ``root`` and the ``source`` is the same thing for the
143-
top level fields. The root object never changes during the query and it may be null and hence no used.
143+
top level fields. The root object never changes during the query and it may be null and hence no used.
144144
145145
* ``Map<String, Object> getArguments()`` - this represents the arguments that have been provided on a field and the values of those
146-
arguments that have been resolved from passed in variables, AST literals and default argument values. You use the arguments
147-
of a field to control what values it returns.
148-
149-
* ``<T> T getContext()`` - the context object is set up when the query is first executed and stays the same over the lifetime
150-
of the query. The context can be any value and is typically used to give each data fetcher some calling context needed
151-
when trying to get field data. For example the current user credentials or the database connection parameters could be contained
152-
with a ``context`` object so that data fetchers can make business layer calls. One of the key design decisions you have as a graphql
153-
system designer is how you will use context in your fetchers if at all. Some people use a dependency framework that injects context into
154-
data fetchers automatically and hence don't need to use this.
146+
arguments that have been resolved from passed in variables, AST literals and default argument values. You use the arguments
147+
of a field to control what values it returns.
155148
149+
* ``<T> T getGraphQLContext()`` - the context object is set up when the query is first executed and stays the same over the lifetime
150+
of the query. The context can be any value and is typically used to give each data fetcher some calling context needed
151+
when trying to get field data. For example the current user credentials or the database connection parameters could be contained
152+
with a context object so that data fetchers can make business layer calls. One of the key design decisions you have as a graphql
153+
system designer is how you will use context in your fetchers if at all. Some people use a dependency framework that injects context into
154+
data fetchers automatically and hence don't need to use this.
156155
157156
* ``ExecutionStepInfo getExecutionStepInfo()`` - the field type information is a catch all bucket of field type information that is built up as
158-
the query is executed. The following section explains more on this.
157+
the query is executed. The following section explains more on this.
159158
160159
* ``DataFetchingFieldSelectionSet getSelectionSet()`` - the selection set represents the child fields that have been "selected" under neath the
161-
currently executing field. This can be useful to help look ahead to see what sub field information a client wants. The following section explains more on this.
160+
currently executing field. This can be useful to help look ahead to see what sub field information a client wants. The following section explains more on this.
162161
163162
* ``ExecutionId getExecutionId()`` - each query execution is given a unique id. You can use this perhaps on logs to tag each individual
164-
query.
163+
query.
165164
166165
## The interesting parts of ExecutionStepInfo
167166

0 commit comments

Comments
 (0)