|
1 | 1 | package graphql.servlet.examples.dataloader.requestscope; |
2 | 2 |
|
3 | | -import graphql.servlet.context.DefaultGraphQLContext; |
| 3 | +import graphql.kickstart.execution.context.DefaultGraphQLContext; |
| 4 | +import graphql.kickstart.execution.context.GraphQLContext; |
4 | 5 | import graphql.servlet.context.DefaultGraphQLServletContext; |
5 | 6 | import graphql.servlet.context.DefaultGraphQLWebSocketContext; |
6 | | -import graphql.servlet.context.GraphQLContext; |
7 | | -import graphql.servlet.context.GraphQLContextBuilder; |
8 | | -import org.dataloader.DataLoader; |
9 | | -import org.dataloader.DataLoaderRegistry; |
10 | | -import org.springframework.stereotype.Component; |
11 | | - |
| 7 | +import graphql.servlet.context.GraphQLServletContextBuilder; |
| 8 | +import java.util.concurrent.CompletableFuture; |
12 | 9 | import javax.servlet.http.HttpServletRequest; |
13 | 10 | import javax.servlet.http.HttpServletResponse; |
14 | 11 | import javax.websocket.Session; |
15 | 12 | import javax.websocket.server.HandshakeRequest; |
16 | | -import java.util.concurrent.CompletableFuture; |
| 13 | +import org.dataloader.DataLoader; |
| 14 | +import org.dataloader.DataLoaderRegistry; |
| 15 | +import org.springframework.stereotype.Component; |
17 | 16 |
|
18 | 17 | @Component |
19 | | -public class CustomGraphQLContextBuilder implements GraphQLContextBuilder { |
20 | | - |
21 | | - private final CustomerRepository customerRepository; |
22 | | - |
23 | | - public CustomGraphQLContextBuilder(CustomerRepository customerRepository) { |
24 | | - this.customerRepository = customerRepository; |
25 | | - } |
26 | | - |
27 | | - @Override |
28 | | - public GraphQLContext build(HttpServletRequest req, HttpServletResponse response) { |
29 | | - return DefaultGraphQLServletContext.createServletContext(buildDataLoaderRegistry(), null).with(req).with(response).build(); |
30 | | - } |
31 | | - |
32 | | - @Override |
33 | | - public GraphQLContext build() { |
34 | | - return new DefaultGraphQLContext(buildDataLoaderRegistry(), null); |
35 | | - } |
36 | | - |
37 | | - @Override |
38 | | - public GraphQLContext build(Session session, HandshakeRequest request) { |
39 | | - return DefaultGraphQLWebSocketContext.createWebSocketContext(buildDataLoaderRegistry(), null).with(session).with(request).build(); |
40 | | - } |
41 | | - |
42 | | - private DataLoaderRegistry buildDataLoaderRegistry() { |
43 | | - DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry(); |
44 | | - dataLoaderRegistry.register("customerDataLoader", |
45 | | - new DataLoader<Integer, String>(customerIds -> |
46 | | - CompletableFuture.supplyAsync(() -> |
47 | | - customerRepository.getUserNamesForIds(customerIds)))); |
48 | | - return dataLoaderRegistry; |
49 | | - } |
| 18 | +public class CustomGraphQLContextBuilder implements GraphQLServletContextBuilder { |
| 19 | + |
| 20 | + private final CustomerRepository customerRepository; |
| 21 | + |
| 22 | + public CustomGraphQLContextBuilder(CustomerRepository customerRepository) { |
| 23 | + this.customerRepository = customerRepository; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public GraphQLContext build(HttpServletRequest req, HttpServletResponse response) { |
| 28 | + return DefaultGraphQLServletContext.createServletContext(buildDataLoaderRegistry(), null).with(req).with(response) |
| 29 | + .build(); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public GraphQLContext build() { |
| 34 | + return new DefaultGraphQLContext(buildDataLoaderRegistry(), null); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public GraphQLContext build(Session session, HandshakeRequest request) { |
| 39 | + return DefaultGraphQLWebSocketContext.createWebSocketContext(buildDataLoaderRegistry(), null).with(session) |
| 40 | + .with(request).build(); |
| 41 | + } |
| 42 | + |
| 43 | + private DataLoaderRegistry buildDataLoaderRegistry() { |
| 44 | + DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry(); |
| 45 | + dataLoaderRegistry.register("customerDataLoader", |
| 46 | + new DataLoader<Integer, String>(customerIds -> |
| 47 | + CompletableFuture.supplyAsync(() -> |
| 48 | + customerRepository.getUserNamesForIds(customerIds)))); |
| 49 | + return dataLoaderRegistry; |
| 50 | + } |
50 | 51 | } |
0 commit comments