.newGraphQL(schema) .instrumentation(instrumentation) .build(); ExecutionResult executionResult = graphQL.execute(executionInput.build());
/** * This helps you transform the current GraphQL object into another one by starting a builder with all * the current values and allows you to transform it how you want. * * @param builderConsumer the consumer code that will be given a builder to transform * * @return a new GraphQL object based on calling build on that builder */ public GraphQL transform(Consumer<GraphQL.Builder> builderConsumer) { Builder builder = new Builder(this.graphQLSchema); builder .queryExecutionStrategy(nvl(this.queryStrategy, builder.queryExecutionStrategy)) .mutationExecutionStrategy(nvl(this.mutationStrategy, builder.mutationExecutionStrategy)) .subscriptionExecutionStrategy(nvl(this.subscriptionStrategy, builder.subscriptionExecutionStrategy)) .executionIdProvider(nvl(this.idProvider, builder.idProvider)) .instrumentation(nvl(this.instrumentation, builder.instrumentation)) .preparsedDocumentProvider(nvl(this.preparsedDocumentProvider, builder.preparsedDocumentProvider)); builderConsumer.accept(builder); return builder.build(); }
builder.queryExecutionStrategy(new SerializerExecutionStrategy()); GraphQL graphQl = builder.build();
.instrumentation(instrumentation) .build();
" }"; GraphQL graphQL = new GraphQL.Builder(schema).build();
GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
Objects.requireNonNull(graphqlQuery, "GraphQL query cannot be null"); GraphQL graphQL = new GraphQL.Builder(schema()).build();
@Test public void argMutation() { GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); GraphQLFieldDefinition doSomethingElse = object.getFieldDefinition("doSomethingElse"); assertNotNull(doSomethingElse); assertEquals(doSomethingElse.getArguments().size(), 1); GraphQLInputType input = doSomethingElse.getArgument("input").getType(); assertTrue(input instanceof GraphQLNonNull); GraphQLType inputType = ((graphql.schema.GraphQLNonNull) input).getWrappedType(); assertTrue(inputType instanceof GraphQLInputObjectType); GraphQLInputObjectType inputType_ = (GraphQLInputObjectType) inputType; assertNotNull(inputType_.getField("a")); assertNotNull(inputType_.getField("b")); assertEquals(inputType_.getField("a").getDescription(), "A"); assertTrue(doSomethingElse.getType() instanceof GraphQLObjectType); GraphQLObjectType returnType = (GraphQLObjectType) doSomethingElse.getType(); assertNotNull(returnType.getFieldDefinition("getI")); assertNotNull(returnType.getFieldDefinition("clientMutationId")); GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); ExecutionResult result = graphQL.execute("mutation { doSomethingElse(input: {a: 0, b: 1, clientMutationId: \"1\"}) { getI clientMutationId } }", new TestObject()); assertEquals(result.getErrors().size(), 0); Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomethingElse"); assertEquals(returns.get("getI"), -1); assertEquals(returns.get("clientMutationId"), "1"); }
@Test public void noArgMutation() { GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); GraphQLFieldDefinition doSomething = object.getFieldDefinition("doSomething"); assertNotNull(doSomething); assertEquals(doSomething.getArguments().size(), 1); GraphQLInputType input = doSomething.getArgument("input").getType(); assertTrue(input instanceof GraphQLNonNull); GraphQLType inputType = ((graphql.schema.GraphQLNonNull) input).getWrappedType(); assertTrue(inputType instanceof GraphQLInputObjectType); assertTrue(doSomething.getType() instanceof GraphQLObjectType); GraphQLObjectType returnType = (GraphQLObjectType) doSomething.getType(); assertNotNull(returnType.getFieldDefinition("getI")); assertNotNull(returnType.getFieldDefinition("clientMutationId")); GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); ExecutionResult result = graphQL.execute("mutation { doSomething(input: {clientMutationId: \"1\"}) { getI clientMutationId } }", new TestObject()); assertEquals(result.getErrors().size(), 0); Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomething"); assertEquals(returns.get("getI"), 0); assertEquals(returns.get("clientMutationId"), "1"); }
@Autowired public GraphQLController(CartGraph cartGraph, ProductGraph productQuery) { GraphQLSchema schema = new GraphQLSchemaGenerator() .withResolverBuilders( new BeanResolverBuilder("org.vtsukur.graphql.demo.cart.domain"), // Resolve by annotations. new AnnotatedResolverBuilder(), // Resolve public methods inside root package. new PublicResolverBuilder("org.vtsukur.graphql.demo.cart.web.graphql.spqr")) .withOperationsFromSingleton(cartGraph) .withOperationsFromSingleton(productQuery) .withValueMapperFactory(new JacksonValueMapperFactory()) .generate(); graphQL = GraphQL.newGraphQL(schema) .queryExecutionStrategy(new BatchedExecutionStrategy()) .instrumentation(new ChainedInstrumentation(Arrays.asList( new MaxQueryComplexityInstrumentation(200), new MaxQueryDepthInstrumentation(20) ))) .build(); }
@Test public void batchedDataFetcher() throws Throwable { GraphQLObjectType nestedObject = GraphQLAnnotations.object(SimpleBatchedField.class); assertEquals(nestedObject.getFieldDefinition("a").getType(), GraphQLString); GraphQLObjectType object = GraphQLAnnotations.object(TestBatchedObject.class); GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphql = GraphQL.newGraphQL(schema).queryExecutionStrategy(new BatchedExecutionStrategy()).build(); ExecutionResult result = graphql.execute("{ fields { a } }", new TestBatchedObject()); List errors = result.getErrors(); for (Object e : errors) { throw ((ExceptionWhileDataFetching) e).getException(); } assertTrue(result.getErrors().isEmpty()); Map<String, Object> data = (Map) result.getData(); List<Map<String, Object>> fields = (List) data.get("fields"); assertEquals(fields.get(0).get("a"), "one"); assertEquals(fields.get(1).get("a"), "two"); }
/** * This helps you transform the current GraphQL object into another one by starting a builder with all * the current values and allows you to transform it how you want. * * @param builderConsumer the consumer code that will be given a builder to transform * * @return a new GraphQL object based on calling build on that builder */ public GraphQL transform(Consumer<GraphQL.Builder> builderConsumer) { Builder builder = new Builder(this.graphQLSchema); builder .queryExecutionStrategy(nvl(this.queryStrategy, builder.queryExecutionStrategy)) .mutationExecutionStrategy(nvl(this.mutationStrategy, builder.mutationExecutionStrategy)) .subscriptionExecutionStrategy(nvl(this.subscriptionStrategy, builder.subscriptionExecutionStrategy)) .executionIdProvider(nvl(this.idProvider, builder.idProvider)) .instrumentation(nvl(this.instrumentation, builder.instrumentation)) .preparsedDocumentProvider(nvl(this.preparsedDocumentProvider, builder.preparsedDocumentProvider)); builderConsumer.accept(builder); return builder.build(); }
@Test public void interfaceReturningMutation() { GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); GraphQLFieldDefinition doSomething = object.getFieldDefinition("doSomethingI"); assertNotNull(doSomething); GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); ExecutionResult result = graphQL.execute("mutation { doSomethingI(input: {clientMutationId: \"1\"}) { getI clientMutationId } }", new TestObject()); assertEquals(result.getErrors().size(), 0); Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomethingI"); assertEquals(returns.get("getI"), 0); assertEquals(returns.get("clientMutationId"), "1"); }
@Test public void queryOptional() { GraphQLObjectType object = GraphQLAnnotations.object(OptionalTest.class); GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{empty, nonempty}", new OptionalTest()); assertTrue(result.getErrors().isEmpty()); Map<String, Object> v = (Map<String, Object>) result.getData(); assertNull(v.get("empty")); assertEquals(v.get("nonempty"), "test"); }
@Test public void defaultArg() { GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); GraphQLSchema schema = newSchema().query(object).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{fieldWithArgs(a: \"test\")}", new TestObject()); assertTrue(result.getErrors().isEmpty()); assertEquals(((Map<String, String>) result.getData()).get("fieldWithArgs"), "default"); }
@Test public void queryPrivateField() { GraphQLObjectType object = GraphQLAnnotations.object(PrivateTestField.class); GraphQLSchema schema = newSchema().query(object).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{field1, field2, booleanField}", new PrivateTestField()); assertTrue(result.getErrors().isEmpty()); assertEquals(((Map<String, String>) result.getData()).get("field1"), "test"); assertEquals(((Map<String, String>) result.getData()).get("field2"), "test"); assertTrue(((Map<String, Boolean>) result.getData()).get("booleanField")); }
@Test public void queryWithList() { GraphQLObjectType object = GraphQLAnnotations.object(IterableAndArrayTestQuery.class); GraphQLSchema schema = newSchema().query(object).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{list {name foo}}"); assertTrue(result.getErrors().isEmpty()); assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "list", 0)).get("name"), "test name"); assertEquals(((LinkedHashMap) getQueryResultAtIndex(result, "list", 0)).get("foo"), "test foo"); }
@Test public void queryNameWithNoArgs_directivesProvidedToRegistry_wiringIsActivated() throws Exception { GraphQLDirective upperCase = newDirective().name("upperCase").argument(builder -> builder.name("isActive").type(GraphQLBoolean).defaultValue(true)) .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build(); GraphQLObjectType object = GraphQLAnnotations.object(Query2.class, upperCase); GraphQLSchema schema = newSchema().query(object).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { nameWithNoArgs }"); assertTrue(result.getErrors().isEmpty()); assertEquals(((Map<String, String>) result.getData()).get("nameWithNoArgs").toString(), "YARIN"); }
@Test(expectedExceptions = GraphQLAnnotationsException.class) public void queryName_noDirectivesProvidedToRegistry_exceptionIsThrown() throws Exception { GraphQLObjectType object = GraphQLAnnotations.object(Query.class); GraphQLSchema schema = newSchema().query(object).build(); GraphQL.newGraphQL(schema).build().execute("query { name }"); }
@Test public void queryUnion() { GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(UnionQuery.class)).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ union { ... on TestObject1 { value } } }", new UnionQuery(new TestObject1())); assertTrue(result.getErrors().isEmpty()); assertEquals(((Map<String, Map<String, String>>) result.getData()).get("union").get("value"), "a"); }