|
| 1 | +package graphql.kickstart.servlet; |
| 2 | + |
| 3 | +import static graphql.schema.GraphQLObjectType.newObject; |
| 4 | +import static graphql.schema.GraphQLSchema.newSchema; |
| 5 | +import static java.util.stream.Collectors.toSet; |
| 6 | + |
| 7 | +import graphql.Scalars; |
| 8 | +import graphql.execution.preparsed.NoOpPreparsedDocumentProvider; |
| 9 | +import graphql.execution.preparsed.PreparsedDocumentProvider; |
| 10 | +import graphql.kickstart.execution.GraphQLObjectMapper; |
| 11 | +import graphql.kickstart.execution.GraphQLQueryInvoker; |
| 12 | +import graphql.kickstart.execution.config.DefaultExecutionStrategyProvider; |
| 13 | +import graphql.kickstart.execution.config.ExecutionStrategyProvider; |
| 14 | +import graphql.kickstart.execution.config.InstrumentationProvider; |
| 15 | +import graphql.kickstart.execution.error.DefaultGraphQLErrorHandler; |
| 16 | +import graphql.kickstart.execution.error.GraphQLErrorHandler; |
| 17 | +import graphql.kickstart.execution.instrumentation.NoOpInstrumentationProvider; |
| 18 | +import graphql.kickstart.servlet.config.DefaultGraphQLSchemaServletProvider; |
| 19 | +import graphql.kickstart.servlet.config.GraphQLSchemaServletProvider; |
| 20 | +import graphql.kickstart.servlet.context.DefaultGraphQLServletContextBuilder; |
| 21 | +import graphql.kickstart.servlet.context.GraphQLServletContextBuilder; |
| 22 | +import graphql.kickstart.servlet.core.DefaultGraphQLRootObjectBuilder; |
| 23 | +import graphql.kickstart.servlet.core.GraphQLServletListener; |
| 24 | +import graphql.kickstart.servlet.core.GraphQLServletRootObjectBuilder; |
| 25 | +import graphql.kickstart.servlet.input.GraphQLInvocationInputFactory; |
| 26 | +import graphql.kickstart.servlet.osgi.GraphQLCodeRegistryProvider; |
| 27 | +import graphql.kickstart.servlet.osgi.GraphQLFieldProvider; |
| 28 | +import graphql.kickstart.servlet.osgi.GraphQLMutationProvider; |
| 29 | +import graphql.kickstart.servlet.osgi.GraphQLQueryProvider; |
| 30 | +import graphql.kickstart.servlet.osgi.GraphQLSubscriptionProvider; |
| 31 | +import graphql.kickstart.servlet.osgi.GraphQLTypesProvider; |
| 32 | +import graphql.schema.GraphQLCodeRegistry; |
| 33 | +import graphql.schema.GraphQLFieldDefinition; |
| 34 | +import graphql.schema.GraphQLObjectType; |
| 35 | +import graphql.schema.GraphQLType; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Collection; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Set; |
| 40 | +import java.util.concurrent.Executors; |
| 41 | +import java.util.concurrent.ScheduledExecutorService; |
| 42 | +import java.util.concurrent.ScheduledFuture; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | +import lombok.Setter; |
| 45 | + |
| 46 | +@Setter |
| 47 | +class OsgiSchemaBuilder { |
| 48 | + |
| 49 | + private final List<GraphQLQueryProvider> queryProviders = new ArrayList<>(); |
| 50 | + private final List<GraphQLMutationProvider> mutationProviders = new ArrayList<>(); |
| 51 | + private final List<GraphQLSubscriptionProvider> subscriptionProviders = new ArrayList<>(); |
| 52 | + private final List<GraphQLTypesProvider> typesProviders = new ArrayList<>(); |
| 53 | + private final List<GraphQLServletListener> listeners = new ArrayList<>(); |
| 54 | + |
| 55 | + private GraphQLServletContextBuilder contextBuilder = new DefaultGraphQLServletContextBuilder(); |
| 56 | + private GraphQLServletRootObjectBuilder rootObjectBuilder = new DefaultGraphQLRootObjectBuilder(); |
| 57 | + private ExecutionStrategyProvider executionStrategyProvider = new DefaultExecutionStrategyProvider(); |
| 58 | + private InstrumentationProvider instrumentationProvider = new NoOpInstrumentationProvider(); |
| 59 | + private GraphQLErrorHandler errorHandler = new DefaultGraphQLErrorHandler(); |
| 60 | + private PreparsedDocumentProvider preparsedDocumentProvider = NoOpPreparsedDocumentProvider.INSTANCE; |
| 61 | + private GraphQLCodeRegistryProvider codeRegistryProvider = () -> GraphQLCodeRegistry |
| 62 | + .newCodeRegistry().build(); |
| 63 | + |
| 64 | + private GraphQLSchemaServletProvider schemaProvider; |
| 65 | + |
| 66 | + private ScheduledExecutorService executor; |
| 67 | + private ScheduledFuture<?> updateFuture; |
| 68 | + private int schemaUpdateDelay; |
| 69 | + |
| 70 | + void activate(int schemaUpdateDelay) { |
| 71 | + this.schemaUpdateDelay = schemaUpdateDelay; |
| 72 | + if (schemaUpdateDelay != 0) { |
| 73 | + executor = Executors.newSingleThreadScheduledExecutor(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + void deactivate() { |
| 78 | + if (executor != null) { |
| 79 | + executor.shutdown(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + void updateSchema() { |
| 84 | + if (schemaUpdateDelay == 0) { |
| 85 | + doUpdateSchema(); |
| 86 | + } else { |
| 87 | + if (updateFuture != null) { |
| 88 | + updateFuture.cancel(true); |
| 89 | + } |
| 90 | + |
| 91 | + updateFuture = executor |
| 92 | + .schedule(this::doUpdateSchema, schemaUpdateDelay, TimeUnit.MILLISECONDS); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private void doUpdateSchema() { |
| 97 | + this.schemaProvider = new DefaultGraphQLSchemaServletProvider( |
| 98 | + newSchema().query(buildQueryType()) |
| 99 | + .mutation(buildMutationType()) |
| 100 | + .subscription(buildSubscriptionType()) |
| 101 | + .additionalTypes(buildTypes()) |
| 102 | + .codeRegistry(codeRegistryProvider.getCodeRegistry()) |
| 103 | + .build()); |
| 104 | + } |
| 105 | + |
| 106 | + private GraphQLObjectType buildQueryType() { |
| 107 | + final GraphQLObjectType.Builder queryTypeBuilder = newObject().name("Query") |
| 108 | + .description("Root query type"); |
| 109 | + |
| 110 | + if (!queryProviders.isEmpty()) { |
| 111 | + for (GraphQLQueryProvider provider : queryProviders) { |
| 112 | + if (provider.getQueries() != null && !provider.getQueries().isEmpty()) { |
| 113 | + provider.getQueries().forEach(queryTypeBuilder::field); |
| 114 | + } |
| 115 | + } |
| 116 | + } else { |
| 117 | + // graphql-java enforces Query type to be there with at least some field. |
| 118 | + queryTypeBuilder.field( |
| 119 | + GraphQLFieldDefinition |
| 120 | + .newFieldDefinition() |
| 121 | + .name("_empty") |
| 122 | + .type(Scalars.GraphQLBoolean) |
| 123 | + .build()); |
| 124 | + } |
| 125 | + return queryTypeBuilder.build(); |
| 126 | + } |
| 127 | + |
| 128 | + private Set<GraphQLType> buildTypes() { |
| 129 | + return typesProviders.stream() |
| 130 | + .map(GraphQLTypesProvider::getTypes) |
| 131 | + .flatMap(Collection::stream) |
| 132 | + .collect(toSet()); |
| 133 | + } |
| 134 | + |
| 135 | + private GraphQLObjectType buildMutationType() { |
| 136 | + return buildObjectType("Mutation", new ArrayList<>(mutationProviders)); |
| 137 | + } |
| 138 | + |
| 139 | + private GraphQLObjectType buildSubscriptionType() { |
| 140 | + return buildObjectType("Subscription", new ArrayList<>(subscriptionProviders)); |
| 141 | + } |
| 142 | + |
| 143 | + private GraphQLObjectType buildObjectType(String name, List<GraphQLFieldProvider> providers) { |
| 144 | + if (!providers.isEmpty()) { |
| 145 | + final GraphQLObjectType.Builder typeBuilder = newObject().name(name) |
| 146 | + .description("Root " + name.toLowerCase() + " type"); |
| 147 | + |
| 148 | + for (GraphQLFieldProvider provider : providers) { |
| 149 | + provider.getFields().forEach(typeBuilder::field); |
| 150 | + } |
| 151 | + |
| 152 | + if (!typeBuilder.build().getFieldDefinitions().isEmpty()) { |
| 153 | + return typeBuilder.build(); |
| 154 | + } |
| 155 | + } |
| 156 | + return null; |
| 157 | + } |
| 158 | + |
| 159 | + void add(GraphQLQueryProvider provider) { |
| 160 | + queryProviders.add(provider); |
| 161 | + } |
| 162 | + |
| 163 | + void add(GraphQLMutationProvider provider) { |
| 164 | + mutationProviders.add(provider); |
| 165 | + } |
| 166 | + |
| 167 | + void add(GraphQLSubscriptionProvider provider) { |
| 168 | + subscriptionProviders.add(provider); |
| 169 | + } |
| 170 | + |
| 171 | + void add(GraphQLTypesProvider provider) { |
| 172 | + typesProviders.add(provider); |
| 173 | + } |
| 174 | + |
| 175 | + void remove(GraphQLQueryProvider provider) { |
| 176 | + queryProviders.remove(provider); |
| 177 | + } |
| 178 | + |
| 179 | + void remove(GraphQLMutationProvider provider) { |
| 180 | + mutationProviders.remove(provider); |
| 181 | + } |
| 182 | + |
| 183 | + void remove(GraphQLSubscriptionProvider provider) { |
| 184 | + subscriptionProviders.remove(provider); |
| 185 | + } |
| 186 | + |
| 187 | + void remove(GraphQLTypesProvider provider) { |
| 188 | + typesProviders.remove(provider); |
| 189 | + } |
| 190 | + |
| 191 | + GraphQLSchemaServletProvider getSchemaProvider() { |
| 192 | + return schemaProvider; |
| 193 | + } |
| 194 | + |
| 195 | + GraphQLConfiguration buildConfiguration() { |
| 196 | + return GraphQLConfiguration |
| 197 | + .with(buildInvocationInputFactory()) |
| 198 | + .with(buildQueryInvoker()) |
| 199 | + .with(buildObjectMapper()) |
| 200 | + .with(listeners) |
| 201 | + .build(); |
| 202 | + } |
| 203 | + |
| 204 | + private GraphQLInvocationInputFactory buildInvocationInputFactory() { |
| 205 | + return GraphQLInvocationInputFactory.newBuilder(this::getSchemaProvider) |
| 206 | + .withGraphQLContextBuilder(contextBuilder) |
| 207 | + .withGraphQLRootObjectBuilder(rootObjectBuilder) |
| 208 | + .build(); |
| 209 | + } |
| 210 | + |
| 211 | + private GraphQLQueryInvoker buildQueryInvoker() { |
| 212 | + return GraphQLQueryInvoker.newBuilder() |
| 213 | + .withPreparsedDocumentProvider(preparsedDocumentProvider) |
| 214 | + .withInstrumentation(() -> instrumentationProvider.getInstrumentation()) |
| 215 | + .withExecutionStrategyProvider(executionStrategyProvider).build(); |
| 216 | + } |
| 217 | + |
| 218 | + private GraphQLObjectMapper buildObjectMapper() { |
| 219 | + return GraphQLObjectMapper.newBuilder() |
| 220 | + .withGraphQLErrorHandler(errorHandler) |
| 221 | + .build(); |
| 222 | + } |
| 223 | + |
| 224 | + void add(GraphQLServletListener listener) { |
| 225 | + listeners.add(listener); |
| 226 | + } |
| 227 | + |
| 228 | + void remove(GraphQLServletListener listener) { |
| 229 | + listeners.remove(listener); |
| 230 | + } |
| 231 | +} |
0 commit comments