schemaBuilder.mutation(GraphQLObjectType.newObject().name("Mutation").fields(mutations));
schemaBuilder.mutation(mutationBuilder);
.mutation(mutationRoot) .build(new HashSet<>(CollectionUtils.union( connectionObjectRegistry.values(),
assertNotNull(returnType.getFieldDefinition("clientMutationId")); GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).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"); }
private void doUpdateSchema() { final GraphQLObjectType.Builder queryTypeBuilder = newObject().name("Query").description("Root query type"); for (GraphQLQueryProvider provider : queryProviders) { if (provider.getQueries() != null && !provider.getQueries().isEmpty()) { provider.getQueries().forEach(queryTypeBuilder::field); } } final Set<GraphQLType> types = new HashSet<>(); for (GraphQLTypesProvider typesProvider : typesProviders) { types.addAll(typesProvider.getTypes()); } GraphQLObjectType mutationType = null; if (!mutationProviders.isEmpty()) { final GraphQLObjectType.Builder mutationTypeBuilder = newObject().name("Mutation").description("Root mutation type"); for (GraphQLMutationProvider provider : mutationProviders) { provider.getMutations().forEach(mutationTypeBuilder::field); } if (!mutationTypeBuilder.build().getFieldDefinitions().isEmpty()) { mutationType = mutationTypeBuilder.build(); } } this.schemaProvider = new DefaultGraphQLSchemaProvider(newSchema().query(queryTypeBuilder.build()) .mutation(mutationType) .additionalTypes(types) .build()); }
@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"); }
schemaBuilder.mutation(mutationBuilder);
protected void updateSchema() { final GraphQLObjectType.Builder queryTypeBuilder = newObject().name("Query").description("Root query type"); for (GraphQLQueryProvider provider : queryProviders) { if (provider.getQueries() != null && !provider.getQueries().isEmpty()) { provider.getQueries().forEach(queryTypeBuilder::field); } } final Set<GraphQLType> types = new HashSet<>(); for (GraphQLTypesProvider typesProvider : typesProviders) { types.addAll(typesProvider.getTypes()); } GraphQLObjectType mutationType = null; if (!mutationProviders.isEmpty()) { final GraphQLObjectType.Builder mutationTypeBuilder = newObject().name("Mutation").description("Root mutation type"); for (GraphQLMutationProvider provider : mutationProviders) { provider.getMutations().forEach(mutationTypeBuilder::field); } if (!mutationTypeBuilder.build().getFieldDefinitions().isEmpty()) { mutationType = mutationTypeBuilder.build(); } } this.schemaProvider = new DefaultGraphQLSchemaProvider(newSchema().query(queryTypeBuilder.build()).mutation(mutationType).build(types)); }
@Test public void optionalInput() { GraphQLObjectType object = GraphQLAnnotations.object(OptionalTest.class); GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever(); GraphQLInputObjectType inputObject = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever), new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever()). getInputObjectBuilder(OptionalTest.class, GraphQLAnnotations.getInstance().getContainer()).build(); GraphQLObjectType mutation = GraphQLObjectType.newObject().name("mut").field(newFieldDefinition().name("test").type(object). argument(GraphQLArgument.newArgument().type(inputObject).name("input").build()).dataFetcher(environment -> { Map<String, String> input = environment.getArgument("input"); return new OptionalTest(Optional.ofNullable(input.get("empty")), Optional.ofNullable(input.get("nonempty"))); }).build()).build(); GraphQLSchema schema = newSchema().query(object).mutation(mutation).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("mutation {test(input: {empty: \"test\"}) { empty nonempty } }", new OptionalTest()); assertTrue(result.getErrors().isEmpty()); Map<String, Object> v = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("test"); assertEquals(v.get("empty"), "test"); assertNull(v.get("nonempty")); }
/** * This allows you to build a schema from an existing schema. It copies everything from the existing * schema and then allows you to replace them. * * @param existingSchema the existing schema * * @return a new schema builder */ public static Builder newSchema(GraphQLSchema existingSchema) { return new Builder() .query(existingSchema.getQueryType()) .mutation(existingSchema.getMutationType()) .subscription(existingSchema.getSubscriptionType()) .codeRegistry(existingSchema.getCodeRegistry()) .clearAdditionalTypes() .clearDirectives() .additionalDirectives(existingSchema.directives) .additionalTypes(existingSchema.additionalTypes); }
private void buildSchema(GraphQLObjectType.Builder queryBuilder, GraphQLObjectType.Builder mutationBuilder, boolean foundQueryDefinitions, boolean foundMutationDefinitions) { log.debug("Start building graphql schema"); GraphQLSchema.Builder schemaBuilder = GraphQLSchema.newSchema(); if (foundQueryDefinitions) { schemaBuilder = schemaBuilder.query(queryBuilder.build()); } if (foundMutationDefinitions) { schemaBuilder = schemaBuilder.mutation(mutationBuilder.build()); } if (foundQueryDefinitions || foundMutationDefinitions) { schema = schemaBuilder.build(); } else { schema = generateGettingStartedGraphQlSchema(); } }
public GraphQLSchema traverse() { schemaName = StereotypeUtils.getGraphQLSchemaName(schemaClass, schemaClass.getName()); DfsContext dfsContext = new DfsContext(); GraphQLObjectType graphQLRootQueryObjectType = (GraphQLObjectType) findSchemaQueryRoot(dfsContext, schemaClass); GraphQLObjectType graphQLMutationObjectType = findSchemaMutations(dfsContext, schemaClass); GraphQLSchema graphQLSchema = newSchema() .query(graphQLRootQueryObjectType) .mutation(graphQLMutationObjectType) .build(); SchemaHelper.replaceTypeReferencesForUnionType(graphQLSchema, graphQLUnionTypeMap); return graphQLSchema; }
@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"); }
public Builder mutation(GraphQLObjectType.Builder builder) { return mutation(builder.build()); }
.mutation(createReviewForEpisodeMutation) .build();
/** * This allows you to build a schema from an existing schema. It copies everything from the existing * schema and then allows you to replace them. * * @param existingSchema the existing schema * * @return a new schema builder */ public static Builder newSchema(GraphQLSchema existingSchema) { return new Builder() .query(existingSchema.getQueryType()) .mutation(existingSchema.getMutationType()) .subscription(existingSchema.getSubscriptionType()) .codeRegistry(existingSchema.getCodeRegistry()) .clearAdditionalTypes() .clearDirectives() .additionalDirectives(existingSchema.directives) .additionalTypes(existingSchema.additionalTypes); }
public Builder mutation(GraphQLObjectType.Builder builder) { return mutation(builder.build()); }
void creatingASchema() { GraphQLSchema schema = GraphQLSchema.newSchema() .query(queryType) // must be provided .mutation(mutationType) // is optional .build(); }