-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathOsgiSchemaBuilder.java
251 lines (213 loc) · 8.81 KB
/
OsgiSchemaBuilder.java
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package graphql.kickstart.servlet;
import static graphql.schema.GraphQLObjectType.newObject;
import static graphql.schema.GraphQLSchema.newSchema;
import static java.util.stream.Collectors.toSet;
import graphql.Scalars;
import graphql.execution.preparsed.NoOpPreparsedDocumentProvider;
import graphql.execution.preparsed.PreparsedDocumentProvider;
import graphql.kickstart.execution.GraphQLObjectMapper;
import graphql.kickstart.execution.GraphQLQueryInvoker;
import graphql.kickstart.execution.config.DefaultExecutionStrategyProvider;
import graphql.kickstart.execution.config.ExecutionStrategyProvider;
import graphql.kickstart.execution.config.InstrumentationProvider;
import graphql.kickstart.execution.error.DefaultGraphQLErrorHandler;
import graphql.kickstart.execution.error.GraphQLErrorHandler;
import graphql.kickstart.execution.instrumentation.NoOpInstrumentationProvider;
import graphql.kickstart.servlet.config.DefaultGraphQLSchemaServletProvider;
import graphql.kickstart.servlet.config.GraphQLSchemaServletProvider;
import graphql.kickstart.servlet.context.DefaultGraphQLServletContextBuilder;
import graphql.kickstart.servlet.context.GraphQLServletContextBuilder;
import graphql.kickstart.servlet.core.DefaultGraphQLRootObjectBuilder;
import graphql.kickstart.servlet.core.GraphQLServletListener;
import graphql.kickstart.servlet.core.GraphQLServletRootObjectBuilder;
import graphql.kickstart.servlet.input.GraphQLInvocationInputFactory;
import graphql.kickstart.servlet.osgi.GraphQLCodeRegistryProvider;
import graphql.kickstart.servlet.osgi.GraphQLFieldProvider;
import graphql.kickstart.servlet.osgi.GraphQLDirectiveProvider;
import graphql.kickstart.servlet.osgi.GraphQLMutationProvider;
import graphql.kickstart.servlet.osgi.GraphQLQueryProvider;
import graphql.kickstart.servlet.osgi.GraphQLSubscriptionProvider;
import graphql.kickstart.servlet.osgi.GraphQLTypesProvider;
import graphql.schema.GraphQLCodeRegistry;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import lombok.Setter;
@Setter
class OsgiSchemaBuilder {
private final List<GraphQLQueryProvider> queryProviders = new ArrayList<>();
private final List<GraphQLMutationProvider> mutationProviders = new ArrayList<>();
private final List<GraphQLSubscriptionProvider> subscriptionProviders = new ArrayList<>();
private final List<GraphQLTypesProvider> typesProviders = new ArrayList<>();
private final List<GraphQLDirectiveProvider> directiveProviders = new ArrayList<>();
private final List<GraphQLServletListener> listeners = new ArrayList<>();
private GraphQLServletContextBuilder contextBuilder = new DefaultGraphQLServletContextBuilder();
private GraphQLServletRootObjectBuilder rootObjectBuilder = new DefaultGraphQLRootObjectBuilder();
private ExecutionStrategyProvider executionStrategyProvider =
new DefaultExecutionStrategyProvider();
private InstrumentationProvider instrumentationProvider = new NoOpInstrumentationProvider();
private GraphQLErrorHandler errorHandler = new DefaultGraphQLErrorHandler();
private PreparsedDocumentProvider preparsedDocumentProvider =
NoOpPreparsedDocumentProvider.INSTANCE;
private GraphQLCodeRegistryProvider codeRegistryProvider =
() -> GraphQLCodeRegistry.newCodeRegistry().build();
private GraphQLSchemaServletProvider schemaProvider;
private ScheduledExecutorService executor;
private ScheduledFuture<?> updateFuture;
private int schemaUpdateDelay;
void activate(int schemaUpdateDelay) {
this.schemaUpdateDelay = schemaUpdateDelay;
if (schemaUpdateDelay != 0) {
executor = Executors.newSingleThreadScheduledExecutor();
}
}
void deactivate() {
if (executor != null) {
executor.shutdown();
}
}
void updateSchema() {
if (schemaUpdateDelay == 0) {
doUpdateSchema();
} else {
if (updateFuture != null) {
updateFuture.cancel(true);
}
updateFuture =
executor.schedule(this::doUpdateSchema, schemaUpdateDelay, TimeUnit.MILLISECONDS);
}
}
private void doUpdateSchema() {
this.schemaProvider =
new DefaultGraphQLSchemaServletProvider(
newSchema()
.query(buildQueryType())
.mutation(buildMutationType())
.subscription(buildSubscriptionType())
.additionalTypes(buildTypes())
.additionalDirectives(buildDirectives())
.codeRegistry(codeRegistryProvider.getCodeRegistry())
.build());
}
private GraphQLObjectType buildQueryType() {
final GraphQLObjectType.Builder queryTypeBuilder =
newObject().name("Query").description("Root query type");
if (!queryProviders.isEmpty()) {
for (GraphQLQueryProvider provider : queryProviders) {
if (provider.getQueries() != null && !provider.getQueries().isEmpty()) {
provider.getQueries().forEach(queryTypeBuilder::field);
}
}
} else {
// graphql-java enforces Query type to be there with at least some field.
queryTypeBuilder.field(
GraphQLFieldDefinition.newFieldDefinition()
.name("_empty")
.type(Scalars.GraphQLBoolean)
.build());
}
return queryTypeBuilder.build();
}
private Set<GraphQLType> buildTypes() {
return typesProviders.stream()
.map(GraphQLTypesProvider::getTypes)
.flatMap(Collection::stream)
.collect(toSet());
}
private GraphQLObjectType buildMutationType() {
return buildObjectType("Mutation", new ArrayList<>(mutationProviders));
}
private GraphQLObjectType buildSubscriptionType() {
return buildObjectType("Subscription", new ArrayList<>(subscriptionProviders));
}
private GraphQLObjectType buildObjectType(String name, List<GraphQLFieldProvider> providers) {
if (!providers.isEmpty()) {
final GraphQLObjectType.Builder typeBuilder =
newObject().name(name).description("Root " + name.toLowerCase() + " type");
for (GraphQLFieldProvider provider : providers) {
provider.getFields().forEach(typeBuilder::field);
}
if (!typeBuilder.build().getFieldDefinitions().isEmpty()) {
return typeBuilder.build();
}
}
return null;
}
private Set<GraphQLDirective> buildDirectives() {
return directiveProviders.stream()
.map(GraphQLDirectiveProvider::getDirectives)
.flatMap(Collection::stream)
.collect(toSet());
}
void add(GraphQLQueryProvider provider) {
queryProviders.add(provider);
}
void add(GraphQLMutationProvider provider) {
mutationProviders.add(provider);
}
void add(GraphQLSubscriptionProvider provider) {
subscriptionProviders.add(provider);
}
void add(GraphQLTypesProvider provider) {
typesProviders.add(provider);
}
void add(GraphQLDirectiveProvider provider) {
directiveProviders.add(provider);
}
void remove(GraphQLQueryProvider provider) {
queryProviders.remove(provider);
}
void remove(GraphQLMutationProvider provider) {
mutationProviders.remove(provider);
}
void remove(GraphQLSubscriptionProvider provider) {
subscriptionProviders.remove(provider);
}
void remove(GraphQLTypesProvider provider) {
typesProviders.remove(provider);
}
void remove(GraphQLDirectiveProvider provider) {
directiveProviders.remove(provider);
}
GraphQLSchemaServletProvider getSchemaProvider() {
return schemaProvider;
}
GraphQLConfiguration buildConfiguration() {
return GraphQLConfiguration.with(buildInvocationInputFactory())
.with(buildQueryInvoker())
.with(buildObjectMapper())
.with(listeners)
.build();
}
private GraphQLInvocationInputFactory buildInvocationInputFactory() {
return GraphQLInvocationInputFactory.newBuilder(this::getSchemaProvider)
.withGraphQLContextBuilder(contextBuilder)
.withGraphQLRootObjectBuilder(rootObjectBuilder)
.build();
}
private GraphQLQueryInvoker buildQueryInvoker() {
return GraphQLQueryInvoker.newBuilder()
.withPreparsedDocumentProvider(preparsedDocumentProvider)
.withInstrumentation(() -> instrumentationProvider.getInstrumentation())
.withExecutionStrategyProvider(executionStrategyProvider)
.build();
}
private GraphQLObjectMapper buildObjectMapper() {
return GraphQLObjectMapper.newBuilder().withGraphQLErrorHandler(errorHandler).build();
}
void add(GraphQLServletListener listener) {
listeners.add(listener);
}
void remove(GraphQLServletListener listener) {
listeners.remove(listener);
}
}