Skip to content

Commit c64aeba

Browse files
authored
Merge pull request #184 from graphql-java/update-deprecated-methods
Update documentation to remove deprecated methods
2 parents 9cbe73d + f66f8e8 commit c64aeba

File tree

6 files changed

+59
-63
lines changed

6 files changed

+59
-63
lines changed

documentation/concerns.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ This made up example shows how you can pass yourself information to help execute
4444
UserContext contextForUser = YourGraphqlContextBuilder.getContextForUser(getCurrentUser());
4545

4646
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
47-
.context(contextForUser)
47+
.graphQLContext(context -> context.put("userContext", contextForUser))
4848
.build();
4949

5050
ExecutionResult executionResult = graphQL.execute(executionInput);
@@ -57,7 +57,7 @@ ExecutionResult executionResult = graphQL.execute(executionInput);
5757
DataFetcher dataFetcher = new DataFetcher() {
5858
@Override
5959
public Object get(DataFetchingEnvironment environment) {
60-
UserContext userCtx = environment.getContext();
60+
UserContext userCtx = environment.getGraphQlContext().get("userContext");
6161
Long businessObjId = environment.getArgument("businessObjId");
6262

6363
return invokeBusinessLayerMethod(userCtx, businessObjId);

documentation/data-fetching.md

Lines changed: 5 additions & 6 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
150-
of the query. The context can be any value and is typically used to give each data fetcher some calling context needed
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 is a map that can contain 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

documentation/sdl-directives.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class AuthorisationDirective implements SchemaDirectiveWiring {
7575

7676
@Override
7777
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> environment) {
78-
String targetAuthRole = (String) environment.getDirective().getArgument("role").getArgumentValue().getValue();
78+
String targetAuthRole = (String) environment.getAppliedDirective().getArgument("role").getArgumentValue().getValue();
7979

8080
//
8181
// build a data fetcher that first checks authorisation roles before then calling the original data fetcher
@@ -84,8 +84,7 @@ class AuthorisationDirective implements SchemaDirectiveWiring {
8484
DataFetcher authDataFetcher = new DataFetcher() {
8585
@Override
8686
public Object get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception {
87-
Map<String, Object> contextMap = dataFetchingEnvironment.getContext();
88-
AuthorisationCtx authContext = (AuthorisationCtx) contextMap.get("authContext");
87+
AuthorisationCtx authContext = dataFetchingEnvironment.getGraphQlContext().get("authContext");
8988

9089
if (authContext.hasRole(targetAuthRole)) {
9190
return originalDataFetcher.get(dataFetchingEnvironment);
@@ -119,7 +118,7 @@ AuthorisationCtx authCtx = AuthorisationCtx.obtain();
119118

120119
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
121120
.query(query)
122-
.context(authCtx)
121+
.graphQLContext(builder -> builder.put("authContext", authCtx))
123122
.build();
124123
```
125124

versioned_docs/version-v22/concerns.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ This made up example shows how you can pass yourself information to help execute
4444
UserContext contextForUser = YourGraphqlContextBuilder.getContextForUser(getCurrentUser());
4545

4646
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
47-
.context(contextForUser)
47+
.graphQLContext(context -> context.put("userContext", contextForUser))
4848
.build();
4949

5050
ExecutionResult executionResult = graphQL.execute(executionInput);
@@ -57,7 +57,7 @@ ExecutionResult executionResult = graphQL.execute(executionInput);
5757
DataFetcher dataFetcher = new DataFetcher() {
5858
@Override
5959
public Object get(DataFetchingEnvironment environment) {
60-
UserContext userCtx = environment.getContext();
60+
UserContext userCtx = environment.getGraphQlContext().get("userContext");
6161
Long businessObjId = environment.getArgument("businessObjId");
6262

6363
return invokeBusinessLayerMethod(userCtx, businessObjId);

versioned_docs/version-v22/data-fetching.md

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@ It might look like the following :
4242
4343
```java
4444
DataFetcher productsDataFetcher = new DataFetcher<List<ProductDTO>>() {
45-
@Override
46-
public List<ProductDTO> get(DataFetchingEnvironment environment) {
47-
DatabaseSecurityCtx ctx = environment.getContext();
48-
49-
List<ProductDTO> products;
50-
String match = environment.getArgument("match");
51-
if (match != null) {
52-
products = fetchProductsFromDatabaseWithMatching(ctx, match);
53-
} else {
54-
products = fetchAllProductsFromDatabase(ctx);
55-
}
56-
return products;
45+
@Override
46+
public List<ProductDTO> get(DataFetchingEnvironment environment) {
47+
DatabaseSecurityCtx ctx = environment.getGraphQlContext().get("databaseSecurityCtx");
48+
49+
List<ProductDTO> products;
50+
String match = environment.getArgument("match");
51+
if (match != null) {
52+
products = fetchProductsFromDatabaseWithMatching(ctx, match);
53+
} else {
54+
products = fetchAllProductsFromDatabase(ctx);
5755
}
56+
return products;
57+
}
5858
};
5959
```
6060
6161
Each ``DataFetcher`` is passed a ``graphql.schema.DataFetchingEnvironment`` object which contains what field is being fetched, what
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
@@ -80,25 +80,25 @@ argument. We can have the ProductDTO have logic that applies this date formatti
8080
```java
8181
class ProductDTO {
8282
83-
private ID id;
84-
private String name;
85-
private String description;
86-
private Double cost;
87-
private Double tax;
88-
private LocalDateTime launchDate;
83+
private ID id;
84+
private String name;
85+
private String description;
86+
private Double cost;
87+
private Double tax;
88+
private LocalDateTime launchDate;
8989
90-
// ...
90+
// ...
9191
92-
public String getName() {
93-
return name;
94-
}
92+
public String getName() {
93+
return name;
94+
}
9595
96-
// ...
96+
// ...
9797
98-
public String getLaunchDate(DataFetchingEnvironment environment) {
99-
String dateFormat = environment.getArgument("dateFormat");
100-
return yodaTimeFormatter(launchDate,dateFormat);
101-
}
98+
public String getLaunchDate(DataFetchingEnvironment environment) {
99+
String dateFormat = environment.getArgument("dateFormat");
100+
return yodaTimeFormatter(launchDate,dateFormat);
101+
}
102102
}
103103
```
104104
@@ -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 is a map that can contain 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
@@ -183,7 +182,7 @@ query {
183182
name
184183
description
185184
sellingLocations {
186-
state
185+
state
187186
}
188187
}
189188
}

versioned_docs/version-v22/sdl-directives.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class AuthorisationDirective implements SchemaDirectiveWiring {
7575

7676
@Override
7777
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> environment) {
78-
String targetAuthRole = (String) environment.getDirective().getArgument("role").getArgumentValue().getValue();
78+
String targetAuthRole = (String) environment.getAppliedDirective().getArgument("role").getArgumentValue().getValue();
7979

8080
//
8181
// build a data fetcher that first checks authorisation roles before then calling the original data fetcher
@@ -84,8 +84,7 @@ class AuthorisationDirective implements SchemaDirectiveWiring {
8484
DataFetcher authDataFetcher = new DataFetcher() {
8585
@Override
8686
public Object get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception {
87-
Map<String, Object> contextMap = dataFetchingEnvironment.getContext();
88-
AuthorisationCtx authContext = (AuthorisationCtx) contextMap.get("authContext");
87+
AuthorisationCtx authContext = dataFetchingEnvironment.getGraphQlContext().get("authContext");
8988

9089
if (authContext.hasRole(targetAuthRole)) {
9190
return originalDataFetcher.get(dataFetchingEnvironment);
@@ -119,7 +118,7 @@ AuthorisationCtx authCtx = AuthorisationCtx.obtain();
119118

120119
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
121120
.query(query)
122-
.context(authCtx)
121+
.graphQLContext(builder -> builder.put("authContext", authCtx))
123122
.build();
124123
```
125124

0 commit comments

Comments
 (0)