forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeferredExamples.java
More file actions
95 lines (74 loc) · 2.92 KB
/
Copy pathDeferredExamples.java
File metadata and controls
95 lines (74 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package readme;
import graphql.Directives;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
@SuppressWarnings({"unused", "ConstantConditions", "UnusedAssignment", "unchecked"})
public class DeferredExamples {
GraphQLSchema buildSchemaWithDirective() {
GraphQLSchema schema = buildSchema();
schema = schema.transform(builder ->
builder.additionalDirective(Directives.DeferDirective)
);
return schema;
}
void basicExample(HttpServletResponse httpServletResponse, String deferredQuery) {
GraphQLSchema schema = buildSchemaWithDirective();
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
//
// deferredQuery contains the query with @defer directives in it
//
ExecutionResult initialResult = graphQL.execute(ExecutionInput.newExecutionInput().query(deferredQuery).build());
//
// then initial results happen first, the deferred ones will begin AFTER these initial
// results have completed
//
sendResult(httpServletResponse, initialResult);
Map<Object, Object> extensions = initialResult.getExtensions();
Publisher<ExecutionResult> deferredResults = (Publisher<ExecutionResult>) extensions.get(GraphQL.DEFERRED_RESULTS);
//
// you subscribe to the deferred results like any other reactive stream
//
deferredResults.subscribe(new Subscriber<ExecutionResult>() {
Subscription subscription;
@Override
public void onSubscribe(Subscription s) {
subscription = s;
//
// how many you request is up to you
subscription.request(10);
}
@Override
public void onNext(ExecutionResult executionResult) {
//
// as each deferred result arrives, send it to where it needs to go
//
sendResult(httpServletResponse, executionResult);
subscription.request(10);
}
@Override
public void onError(Throwable t) {
handleError(httpServletResponse, t);
}
@Override
public void onComplete() {
completeResponse(httpServletResponse);
}
});
}
private void completeResponse(HttpServletResponse httpServletResponse) {
}
private void handleError(HttpServletResponse httpServletResponse, Throwable t) {
}
private void sendResult(HttpServletResponse httpServletResponse, ExecutionResult initialResult) {
}
private GraphQLSchema buildSchema() {
return null;
}
}