Skip to content

Commit 238a646

Browse files
authored
Merge pull request #166 from graphql-java/fix-sdl-documentation
Fix SDL documentation page
2 parents a077ad8 + 8f0f8b6 commit 238a646

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

documentation/sdl-directives.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,12 @@ class AuthorisationDirective implements SchemaDirectiveWiring {
7575

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

80-
GraphQLFieldDefinition field = environment.getElement();
81-
GraphQLFieldsContainer parentType = environment.getFieldsContainer();
8280
//
8381
// build a data fetcher that first checks authorisation roles before then calling the original data fetcher
8482
//
85-
DataFetcher originalDataFetcher = environment.getCodeRegistry().getDataFetcher(parentType, field);
83+
DataFetcher originalDataFetcher = environment.getFieldDataFetcher();
8684
DataFetcher authDataFetcher = new DataFetcher() {
8785
@Override
8886
public Object get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception {
@@ -98,8 +96,7 @@ class AuthorisationDirective implements SchemaDirectiveWiring {
9896
};
9997
//
10098
// now change the field definition to have the new authorising data fetcher
101-
environment.getCodeRegistry().dataFetcher(parentType, field, authDataFetcher);
102-
return field;
99+
return environment.setFieldDataFetcher(authDataFetcher);
103100
}
104101
}
105102

@@ -112,10 +109,9 @@ RuntimeWiring.newRuntimeWiring()
112109
```
113110

114111
This has modified the ``GraphQLFieldDefinition`` so that its original data fetcher will ONLY be called if the current authorisation context
115-
has the ``manager`` role. Exactly what mechanisms you use for authorisation is up to you. You could use Spring Security for example say, graphql-java doesn't
116-
really care.
112+
has the ``manager`` role. You can use any mechanism for authorisation, for example Spring Security or anything else.
117113

118-
You would provide this authorisation checker into the execution "context" object of the graphql input so it can then be accessed later in the
114+
You would provide this authorisation checker into the execution "context" object of the graphql input, so it can then be accessed later in the
119115
``DataFetchingEnvironment``.
120116

121117
```java
@@ -129,7 +125,7 @@ ExecutionInput executionInput = ExecutionInput.newExecutionInput()
129125

130126
## Declaring Directives
131127

132-
In order to use a directive in SDL, the graphql specification requires that you MUST declare its shape before using it. Our ``@auth`` directive example above needs to be
128+
In order to use a directive in SDL, the graphql specification requires that you MUST declare its shape before using it. Our ``@auth`` directive example above needs to be
133129
declared like so before use.
134130

135131
```graphql
@@ -144,13 +140,13 @@ type Employee
144140
}
145141
```
146142

147-
The one exception to this is the ``@deprecated`` directive which is implicitly declared for you as follows :
143+
The one exception to this is the ``@deprecated`` directive which is implicitly declared for you as follows:
148144

149145
```graphql
150146
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE
151147
```
152148

153-
The valid SDL directive locations are as follows :
149+
The valid SDL directive locations are as follows:
154150

155151
```graphql
156152
SCHEMA,
@@ -170,11 +166,11 @@ Directives are commonly applied to fields definitions but as you can see there a
170166

171167
## Another Example - Date Formatting
172168

173-
Date formatting is a cross cutting concern that we should only have to write once and apply it in many areas.
169+
Date formatting is a cross-cutting concern that we should only have to write once and apply it in many areas.
174170

175171
The following demonstrates an example schema directive that can apply date formatting to fields that are ``LocaleDate`` objects.
176172

177-
Whats great in this example is that it adds an extra ``format`` argument to each field that it is applied to. So the clients can
173+
What's great in this example is that it adds an extra ``format`` argument to each field that it is applied to. So the clients can
178174
opt into what ever date formatting you provide per request.
179175

180176
```graphql
@@ -275,13 +271,13 @@ public static void main(String[] args) {
275271
Notice the SDL definition did not have a ``format`` argument yet once the directive wiring is applied, it is added
276272
to the field definition and hence clients can begin to use it.
277273

278-
Please note that graphql-java does not ship with this implementation. It is merely provided here as
274+
Please note that graphql-java does not ship with this implementation. It is merely provided here as
279275
an example of what you could add yourself.
280276

281277

282278
## Chaining Behaviour
283279

284-
The directives are applied in the order they are encountered. For example imagine directives that changed the case of a field value.
280+
The directives are applied in the order they are encountered. For example imagine directives that changed the case of a field value.
285281

286282
```graphql
287283
directive @uppercase on FIELD_DEFINITION
@@ -301,5 +297,5 @@ type Query {
301297
}
302298
```
303299

304-
When the above was executed each directive would be applied one on top of the other. Each directive implementation should be careful
300+
When the above was executed each directive would be applied one on top of the other. Each directive implementation should be careful
305301
to preserve the previous data fetcher to retain behaviour (unless of course you mean to throw it away)

versioned_docs/version-v22/sdl-directives.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,12 @@ class AuthorisationDirective implements SchemaDirectiveWiring {
7575

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

80-
GraphQLFieldDefinition field = environment.getElement();
81-
GraphQLFieldsContainer parentType = environment.getFieldsContainer();
8280
//
8381
// build a data fetcher that first checks authorisation roles before then calling the original data fetcher
8482
//
85-
DataFetcher originalDataFetcher = environment.getCodeRegistry().getDataFetcher(parentType, field);
83+
DataFetcher originalDataFetcher = environment.getFieldDataFetcher();
8684
DataFetcher authDataFetcher = new DataFetcher() {
8785
@Override
8886
public Object get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception {
@@ -98,8 +96,7 @@ class AuthorisationDirective implements SchemaDirectiveWiring {
9896
};
9997
//
10098
// now change the field definition to have the new authorising data fetcher
101-
environment.getCodeRegistry().dataFetcher(parentType, field, authDataFetcher);
102-
return field;
99+
return environment.setFieldDataFetcher(authDataFetcher);
103100
}
104101
}
105102

@@ -112,10 +109,9 @@ RuntimeWiring.newRuntimeWiring()
112109
```
113110

114111
This has modified the ``GraphQLFieldDefinition`` so that its original data fetcher will ONLY be called if the current authorisation context
115-
has the ``manager`` role. Exactly what mechanisms you use for authorisation is up to you. You could use Spring Security for example say, graphql-java doesn't
116-
really care.
112+
has the ``manager`` role. You can use any mechanism for authorisation, for example Spring Security or anything else.
117113

118-
You would provide this authorisation checker into the execution "context" object of the graphql input so it can then be accessed later in the
114+
You would provide this authorisation checker into the execution "context" object of the graphql input, so it can then be accessed later in the
119115
``DataFetchingEnvironment``.
120116

121117
```java
@@ -129,7 +125,7 @@ ExecutionInput executionInput = ExecutionInput.newExecutionInput()
129125

130126
## Declaring Directives
131127

132-
In order to use a directive in SDL, the graphql specification requires that you MUST declare its shape before using it. Our ``@auth`` directive example above needs to be
128+
In order to use a directive in SDL, the graphql specification requires that you MUST declare its shape before using it. Our ``@auth`` directive example above needs to be
133129
declared like so before use.
134130

135131
```graphql
@@ -144,13 +140,13 @@ type Employee
144140
}
145141
```
146142

147-
The one exception to this is the ``@deprecated`` directive which is implicitly declared for you as follows :
143+
The one exception to this is the ``@deprecated`` directive which is implicitly declared for you as follows:
148144

149145
```graphql
150146
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE
151147
```
152148

153-
The valid SDL directive locations are as follows :
149+
The valid SDL directive locations are as follows:
154150

155151
```graphql
156152
SCHEMA,
@@ -170,11 +166,11 @@ Directives are commonly applied to fields definitions but as you can see there a
170166

171167
## Another Example - Date Formatting
172168

173-
Date formatting is a cross cutting concern that we should only have to write once and apply it in many areas.
169+
Date formatting is a cross-cutting concern that we should only have to write once and apply it in many areas.
174170

175171
The following demonstrates an example schema directive that can apply date formatting to fields that are ``LocaleDate`` objects.
176172

177-
Whats great in this example is that it adds an extra ``format`` argument to each field that it is applied to. So the clients can
173+
What's great in this example is that it adds an extra ``format`` argument to each field that it is applied to. So the clients can
178174
opt into what ever date formatting you provide per request.
179175

180176
```graphql
@@ -275,13 +271,13 @@ public static void main(String[] args) {
275271
Notice the SDL definition did not have a ``format`` argument yet once the directive wiring is applied, it is added
276272
to the field definition and hence clients can begin to use it.
277273

278-
Please note that graphql-java does not ship with this implementation. It is merely provided here as
274+
Please note that graphql-java does not ship with this implementation. It is merely provided here as
279275
an example of what you could add yourself.
280276

281277

282278
## Chaining Behaviour
283279

284-
The directives are applied in the order they are encountered. For example imagine directives that changed the case of a field value.
280+
The directives are applied in the order they are encountered. For example imagine directives that changed the case of a field value.
285281

286282
```graphql
287283
directive @uppercase on FIELD_DEFINITION
@@ -301,5 +297,5 @@ type Query {
301297
}
302298
```
303299

304-
When the above was executed each directive would be applied one on top of the other. Each directive implementation should be careful
300+
When the above was executed each directive would be applied one on top of the other. Each directive implementation should be careful
305301
to preserve the previous data fetcher to retain behaviour (unless of course you mean to throw it away)

0 commit comments

Comments
 (0)