|
4 | 4 | import com.introproventures.graphql.jpa.query.autoconfigure.support.MutationRoot;
|
5 | 5 | import com.introproventures.graphql.jpa.query.autoconfigure.support.QueryRoot;
|
6 | 6 | import com.introproventures.graphql.jpa.query.autoconfigure.support.SubscriptionRoot;
|
| 7 | +import com.introproventures.graphql.jpa.query.schema.JavaScalars; |
| 8 | +import com.introproventures.graphql.jpa.query.schema.JavaScalarsWiringPostProcessor; |
| 9 | +import graphql.ExecutionResult; |
7 | 10 | import graphql.GraphQL;
|
8 | 11 | import graphql.Scalars;
|
9 | 12 | import graphql.annotations.AnnotationsSchemaCreator;
|
|
12 | 15 | import graphql.annotations.annotationTypes.GraphQLName;
|
13 | 16 | import graphql.annotations.annotationTypes.directives.definition.GraphQLDirectiveDefinition;
|
14 | 17 | import graphql.scalars.ExtendedScalars;
|
15 |
| -import graphql.schema.FieldCoordinates; |
| 18 | +import graphql.schema.DataFetcher; |
16 | 19 | import graphql.schema.GraphQLCodeRegistry;
|
17 | 20 | import graphql.schema.GraphQLDirective;
|
18 | 21 | import graphql.schema.GraphQLFieldDefinition;
|
19 | 22 | import graphql.schema.GraphQLObjectType;
|
| 23 | +import graphql.schema.GraphQLScalarType; |
20 | 24 | import graphql.schema.GraphQLSchema;
|
21 | 25 | import graphql.schema.StaticDataFetcher;
|
| 26 | +import graphql.schema.idl.RuntimeWiring; |
| 27 | +import graphql.schema.idl.SchemaGenerator; |
| 28 | +import graphql.schema.idl.SchemaParser; |
| 29 | +import graphql.schema.idl.TypeDefinitionRegistry; |
22 | 30 | import org.junit.Test;
|
23 | 31 | import org.junit.runner.RunWith;
|
| 32 | +import org.reactivestreams.Publisher; |
24 | 33 | import org.reflections.Reflections;
|
25 | 34 | import org.springframework.beans.factory.annotation.Autowired;
|
26 | 35 | import org.springframework.beans.factory.annotation.Value;
|
27 | 36 | import org.springframework.boot.autoconfigure.SpringBootApplication;
|
28 | 37 | import org.springframework.boot.test.context.SpringBootTest;
|
29 | 38 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
30 | 39 | import org.springframework.context.annotation.Configuration;
|
| 40 | +import org.springframework.core.io.Resource; |
31 | 41 | import org.springframework.stereotype.Component;
|
32 | 42 | import org.springframework.test.context.junit4.SpringRunner;
|
33 | 43 | import org.springframework.util.StringUtils;
|
34 |
| - |
35 |
| -import java.util.Collections; |
| 44 | +import reactor.core.publisher.Flux; |
| 45 | +import reactor.test.StepVerifier; |
| 46 | + |
| 47 | +import java.io.File; |
| 48 | +import java.io.IOException; |
| 49 | +import java.time.Duration; |
| 50 | +import java.time.Instant; |
| 51 | +import java.util.LinkedHashMap; |
| 52 | +import java.util.List; |
36 | 53 | import java.util.Map;
|
37 | 54 | import java.util.Set;
|
| 55 | +import java.util.UUID; |
| 56 | +import java.util.function.Supplier; |
38 | 57 | import java.util.stream.Collectors;
|
| 58 | +import java.util.stream.Stream; |
39 | 59 |
|
40 | 60 | import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema;
|
| 61 | +import static graphql.schema.FieldCoordinates.coordinates; |
| 62 | +import static graphql.schema.GraphQLCodeRegistry.newCodeRegistry; |
41 | 63 | import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
|
| 64 | +import static graphql.schema.GraphQLObjectType.newObject; |
| 65 | +import static graphql.schema.GraphQLSchema.newSchema; |
| 66 | +import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring; |
| 67 | +import static java.util.Collections.emptyMap; |
42 | 68 | import static org.assertj.core.api.Assertions.assertThat;
|
43 | 69 |
|
44 | 70 | @RunWith(SpringRunner.class)
|
@@ -146,79 +172,121 @@ static class MutationGraphQLSchemaConfigurer implements GraphQLSchemaConfigurer
|
146 | 172 | @Override
|
147 | 173 | public void configure(GraphQLShemaRegistration registry) {
|
148 | 174 |
|
149 |
| - GraphQLObjectType mutation = GraphQLObjectType.newObject() |
150 |
| - .name("mutation") |
151 |
| - .field(GraphQLFieldDefinition.newFieldDefinition() |
152 |
| - .name("greet") |
153 |
| - .type(Scalars.GraphQLString) |
154 |
| - .dataFetcher(new StaticDataFetcher("hello world"))) |
155 |
| - .field(GraphQLFieldDefinition.newFieldDefinition() |
156 |
| - .name("greet2") |
157 |
| - .type(Scalars.GraphQLString)) |
158 |
| - .field(GraphQLFieldDefinition.newFieldDefinition() |
159 |
| - .name("count1") |
160 |
| - .type(ExtendedScalars.GraphQLLong)) |
161 |
| - .build(); |
162 |
| - |
163 |
| - GraphQLCodeRegistry codeRegistry = GraphQLCodeRegistry.newCodeRegistry() |
164 |
| - .dataFetcher(FieldCoordinates.coordinates(mutation.getName(), |
165 |
| - "greet2"), |
166 |
| - new StaticDataFetcher("hello world2")) |
167 |
| - .build(); |
168 |
| - |
169 |
| - GraphQLSchema graphQLSchema = GraphQLSchema.newSchema() |
170 |
| - .query(GraphQLObjectType.newObject().name("null") |
171 |
| - .field(GraphQLFieldDefinition.newFieldDefinition() |
172 |
| - .name("null") |
173 |
| - .type(Scalars.GraphQLString))) |
| 175 | + GraphQLObjectType mutation = newObject().name("mutation") |
| 176 | + .field(GraphQLFieldDefinition.newFieldDefinition() |
| 177 | + .name("greet") |
| 178 | + .type(Scalars.GraphQLString) |
| 179 | + .dataFetcher(new StaticDataFetcher("hello world"))) |
| 180 | + .field(GraphQLFieldDefinition.newFieldDefinition() |
| 181 | + .name("greet2") |
| 182 | + .type(Scalars.GraphQLString)) |
| 183 | + .field(GraphQLFieldDefinition.newFieldDefinition() |
| 184 | + .name("count1") |
| 185 | + .type(ExtendedScalars.GraphQLLong)) |
| 186 | + .build(); |
| 187 | + |
| 188 | + GraphQLCodeRegistry codeRegistry = newCodeRegistry().dataFetcher(coordinates(mutation.getName(),"greet2"), |
| 189 | + new StaticDataFetcher("hello world2")) |
| 190 | + .build(); |
| 191 | + |
| 192 | + GraphQLSchema graphQLSchema = newSchema() |
| 193 | + .query(newObject().name("null") |
| 194 | + .field(GraphQLFieldDefinition.newFieldDefinition() |
| 195 | + .name("null") |
| 196 | + .type(Scalars.GraphQLString))) |
174 | 197 | .mutation(mutation)
|
175 | 198 | .codeRegistry(codeRegistry)
|
176 | 199 | .build();
|
177 | 200 |
|
178 | 201 | registry.register(graphQLSchema);
|
179 | 202 | }
|
180 | 203 | }
|
| 204 | + |
181 | 205 | @Component
|
182 | 206 | static class QueryGraphQLSchemaConfigurer implements GraphQLSchemaConfigurer {
|
183 | 207 |
|
184 | 208 | @Override
|
185 | 209 | public void configure(GraphQLShemaRegistration registry) {
|
186 |
| - GraphQLObjectType query = GraphQLObjectType.newObject() |
187 |
| - .name("query") |
188 |
| - .field(newFieldDefinition().name("hello") |
189 |
| - .type(Scalars.GraphQLString) |
190 |
| - .dataFetcher(new StaticDataFetcher("world"))) |
191 |
| - .field(newFieldDefinition().name("hello2") |
192 |
| - .type(Scalars.GraphQLString)) |
193 |
| - .field(newFieldDefinition().name("hello3") |
194 |
| - .type(GraphQLObjectType.newObject() |
195 |
| - .name("Hello3") |
196 |
| - .field(newFieldDefinition().name("canada") |
197 |
| - .type(Scalars.GraphQLString)) |
198 |
| - .field(newFieldDefinition().name("america") |
199 |
| - .type(Scalars.GraphQLString)))) |
200 |
| - .build(); |
201 |
| - |
202 |
| - GraphQLCodeRegistry codeRegistry = GraphQLCodeRegistry.newCodeRegistry() |
203 |
| - .dataFetcher(FieldCoordinates.coordinates(query.getName(), "hello2"), |
204 |
| - new StaticDataFetcher("world2")) |
205 |
| - .dataFetcher(FieldCoordinates.coordinates(query.getName(), "hello3"), |
206 |
| - new StaticDataFetcher(Collections.emptyMap())) |
207 |
| - .dataFetcher(FieldCoordinates.coordinates("Hello3", "america"), |
208 |
| - new StaticDataFetcher("Hi!")) |
209 |
| - .dataFetcher(FieldCoordinates.coordinates("Hello3", "canada"), |
210 |
| - new StaticDataFetcher("Eh?")) |
211 |
| - .build(); |
212 |
| - |
213 |
| - GraphQLSchema graphQLSchema = GraphQLSchema.newSchema() |
214 |
| - .query(query) |
215 |
| - .codeRegistry(codeRegistry) |
216 |
| - //.additionalDirective(Directives.DeferDirective) |
217 |
| - .build(); |
| 210 | + GraphQLObjectType query = newObject().name("query") |
| 211 | + .field(newFieldDefinition().name("hello") |
| 212 | + .type(Scalars.GraphQLString) |
| 213 | + .dataFetcher(new StaticDataFetcher("world"))) |
| 214 | + .field(newFieldDefinition().name("hello2") |
| 215 | + .type(Scalars.GraphQLString)) |
| 216 | + .field(newFieldDefinition().name("hello3") |
| 217 | + .type(newObject() |
| 218 | + .name("Hello3") |
| 219 | + .field(newFieldDefinition().name("canada") |
| 220 | + .type(Scalars.GraphQLString)) |
| 221 | + .field(newFieldDefinition().name("america") |
| 222 | + .type(Scalars.GraphQLString)))) |
| 223 | + .build(); |
| 224 | + |
| 225 | + GraphQLCodeRegistry codeRegistry = newCodeRegistry().dataFetcher(coordinates(query.getName(), "hello2"), |
| 226 | + new StaticDataFetcher("world2")) |
| 227 | + .dataFetcher(coordinates(query.getName(), "hello3"), |
| 228 | + new StaticDataFetcher(emptyMap())) |
| 229 | + .dataFetcher(coordinates("Hello3", "america"), |
| 230 | + new StaticDataFetcher("Hi!")) |
| 231 | + .dataFetcher(coordinates("Hello3", "canada"), |
| 232 | + new StaticDataFetcher("Eh?")) |
| 233 | + .build(); |
| 234 | + |
| 235 | + GraphQLSchema graphQLSchema = newSchema().query(query) |
| 236 | + .codeRegistry(codeRegistry) |
| 237 | + .build(); |
218 | 238 |
|
219 | 239 | registry.register(graphQLSchema);
|
220 | 240 | }
|
221 | 241 | }
|
| 242 | + |
| 243 | + @Component |
| 244 | + static class GraphQLSchemaGeneratorConfigurer implements GraphQLSchemaConfigurer { |
| 245 | + |
| 246 | + @Value("classpath:activiti.graphqls") |
| 247 | + private Resource schemaResource; |
| 248 | + |
| 249 | + @Override |
| 250 | + public void configure(GraphQLShemaRegistration registry) { |
| 251 | + File schemaFile = null; |
| 252 | + try { |
| 253 | + schemaFile = schemaResource.getFile(); |
| 254 | + } catch (IOException e) { |
| 255 | + throw new RuntimeException(e); |
| 256 | + } |
| 257 | + |
| 258 | + TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(schemaFile); |
| 259 | + |
| 260 | + Supplier<Map> generator = () -> new LinkedHashMap() {{ |
| 261 | + put("id", UUID.randomUUID()); |
| 262 | + put("timestamp", Instant.now().toEpochMilli()); |
| 263 | + put("entity", emptyMap()); |
| 264 | + }}; |
| 265 | + |
| 266 | + DataFetcher<Flux<List<Map>>> dataFetcher = new StaticDataFetcher(Flux.fromStream(Stream.generate(generator)) |
| 267 | + .delayElements(Duration.ofMillis(10)) |
| 268 | + .take(100) |
| 269 | + .buffer(10)); |
| 270 | + |
| 271 | + GraphQLCodeRegistry codeRegistry = newCodeRegistry().dataFetcher(coordinates("Subscription", |
| 272 | + "engineEvents"), |
| 273 | + dataFetcher) |
| 274 | + .build(); |
| 275 | + |
| 276 | + RuntimeWiring.Builder wiring = newRuntimeWiring().codeRegistry(codeRegistry) |
| 277 | + .scalar(GraphQLScalarType.newScalar() |
| 278 | + .name("ObjectScalar") |
| 279 | + .description("An object scalar") |
| 280 | + .coercing(new JavaScalars.GraphQLObjectCoercing()) |
| 281 | + .build()) |
| 282 | + .scalar(ExtendedScalars.GraphQLLong) |
| 283 | + .transformer(new JavaScalarsWiringPostProcessor()); |
| 284 | + |
| 285 | + registry.register(new SchemaGenerator().makeExecutableSchema(typeRegistry, |
| 286 | + wiring.build())); |
| 287 | + } |
| 288 | + } |
| 289 | + |
222 | 290 | }
|
223 | 291 |
|
224 | 292 | @Test
|
@@ -279,7 +347,24 @@ public void annotatedSchemaConfigurer() {
|
279 | 347 | assertThat(mutation.toString()).isEqualTo("{salut=Salut, dude!}");
|
280 | 348 | assertThat(query.toString()).isEqualTo("{greeting={value=Hi, dude!}}");
|
281 | 349 | }
|
282 |
| - |
| 350 | + |
| 351 | + @Test |
| 352 | + public void schemaGeneratorConfigurer() { |
| 353 | + // given |
| 354 | + GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); |
| 355 | + |
| 356 | + // when |
| 357 | + ExecutionResult result = graphQL.execute("subscription { engineEvents { id, timestamp, entity } }"); |
| 358 | + |
| 359 | + Publisher<ExecutionResult> source = result.getData(); |
| 360 | + |
| 361 | + // then |
| 362 | + StepVerifier.create(source) |
| 363 | + .expectSubscription() |
| 364 | + .expectNextCount(10) |
| 365 | + .verifyComplete(); |
| 366 | + } |
| 367 | + |
283 | 368 |
|
284 | 369 | @Test
|
285 | 370 | public void defaultConfigurationProperties() {
|
|
0 commit comments