Skip to content

Commit 536b906

Browse files
authored
Merge pull request #101 from ashu-walmart/request-scoped-data-loading-doc
Add documentation for using Request Scoped Data Loaders
2 parents e94d00e + 69bae49 commit 536b906

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,61 @@ And here is a sample src/main/feature/feature.xml file to add some dependencies
186186
Here's an example of a GraphQL provider that implements three interfaces at the same time.
187187

188188
* [ExampleGraphQLProvider](examples/osgi/providers/src/main/java/graphql/servlet/examples/osgi/ExampleGraphQLProvider.java)
189+
190+
## Request-scoped DataLoaders
191+
192+
It is possible to use dataloaders in a request scope by customizing [GraphQLContextBuilder](https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLContextBuilder.java).
193+
And instantiating a new [DataLoaderRegistry](https://github.com/graphql-java/java-dataloader/blob/master/src/main/java/org/dataloader/DataLoaderRegistry.java) for each GraphQLContext.
194+
For eg:
195+
```java
196+
public class CustomGraphQLContextBuilder implements GraphQLContextBuilder {
197+
198+
private final DataLoader userDataLoader;
199+
200+
public CustomGraphQLContextBuilder(DataLoader userDataLoader) {
201+
this.userDataLoader = userDataLoader;
202+
}
203+
204+
@Override
205+
public GraphQLContext build(HttpServletRequest req) {
206+
GraphQLContext context = new GraphQLContext(req);
207+
context.setDataLoaderRegistry(buildDataLoaderRegistry());
208+
209+
return context;
210+
}
211+
212+
@Override
213+
public GraphQLContext build() {
214+
GraphQLContext context = new GraphQLContext();
215+
context.setDataLoaderRegistry(buildDataLoaderRegistry());
216+
217+
return context;
218+
}
219+
220+
@Override
221+
public GraphQLContext build(HandshakeRequest request) {
222+
GraphQLContext context = new GraphQLContext(request);
223+
context.setDataLoaderRegistry(buildDataLoaderRegistry());
224+
225+
return context;
226+
}
227+
228+
private DataLoaderRegistry buildDataLoaderRegistry() {
229+
DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
230+
dataLoaderRegistry.register("userDataLoader", userDataLoader);
231+
return dataLoaderRegistry;
232+
}
233+
}
234+
235+
```
236+
It is then possible to access the [DataLoader](https://github.com/graphql-java/java-dataloader/blob/master/src/main/java/org/dataloader/DataLoader.java) in the resolvers by accessing the [DataLoaderRegistry] from context. For eg:
237+
```java
238+
public CompletableFuture<String> getEmailAddress(User user, DataFetchingEnvironment dfe) { // User is the graphQL type
239+
final DataLoader<String, UserDetail> userDataloader =
240+
dfe.getContext().getDataLoaderRegistry().get().getDataLoader("userDataLoader"); // UserDetail is the data that is loaded
241+
242+
return userDataloader.load(User.getName())
243+
.thenApply(userDetail -> userDetail != null ? userDetail.getEmailAddress() : null);
244+
}
245+
246+
```

0 commit comments

Comments
 (0)