public class SchemaModule extends AbstractModule {
@Override
protected void configure() {
bind(DataFetcher.class).annotatedWith(named("warehousesGetDataFetcher")).to(WarehousesGetDataFetcher.class)
.in(Singleton.class);
bind(DataFetcher.class).annotatedWith(named("warehousesUpdateDataFetcher"))
.to(WarehousesUpdateDataFetcher.class).in(Singleton.class);
}
@Provides
@Singleton
@Named("mutationType")
GraphQLObjectType provideMutationType(@Named("warehouseType") final GraphQLObjectType warehouseType,
@Named("warehousesUpdateDataFetcher") final DataFetcher warehousesUpdateDataFetcher) {
return newObject()
.name("MutationType")
.field(newFieldDefinition()
.name("updateWarehouseName")
.description("updates a warehouse name from its id and returns the updated warehouse")
.type(warehouseType)
.dataFetcher(warehousesUpdateDataFetcher)
.argument(newArgument()
.name("id")
.description("id of the warehouse")
.type(new GraphQLNonNull(GraphQLString)))
.argument(newArgument()
.name("name")
.description("new name of the warehouse")
.type(new GraphQLNonNull(GraphQLString))))
.build();
}
@Provides
@Singleton
@Named("queryType")
GraphQLObjectType provideQueryType(@Named("warehouseType") final GraphQLObjectType warehouseType,
@Named("warehousesGetDataFetcher") final DataFetcher warehousesGetDataFetcher) {
return newObject()
.name("QueryType")
.field(newFieldDefinition()
.name("warehouse")
.description("Obtains a warehouse from its id")
.type(warehouseType)
.dataFetcher(warehousesGetDataFetcher)
.argument(newArgument()
.name("id")
.description("id of the warehouse")
.type(new GraphQLNonNull(GraphQLString))))
.build();
}
@Provides
@Singleton
GraphQLSchema provideSchema(@Named("queryType") final GraphQLObjectType queryType,
@Named("mutationType") final GraphQLObjectType mutationType) {
return GraphQLSchema.newSchema().query(queryType).mutation(mutationType).build();
}
@Provides
@Singleton
@Named("warehouseType")
GraphQLObjectType provideWarehouseType() {
return newObject()
.name("Warehouse")
.description("A simple warehouse.")
.field(newFieldDefinition()
.name("id")
.description("The id of the warehouse.")
.type(new GraphQLNonNull(GraphQLString)))
.field(newFieldDefinition()
.name("name")
.description("The name of the warehouse.")
.type(new GraphQLNonNull(GraphQLString)))
.field(newFieldDefinition()
.name("virtual")
.description("True if virtual warehouse, else otherwise.")
.type(new GraphQLNonNull(GraphQLBoolean)))
.build();
}
}
I execute queries and mutations with: return new GraphQL(schema).execute(requestString);
When I execute this request string:
{ warehouse(id:1) { id, name, virtual } }, everything works ok, assuming the warehouse with id 1 exists.
When I execute this request string:
{ updateWarehouseName(id:\"1\",name:\"Warehouse11\") { id, name, virtual } }, I get validation error, saying [ValidationError{validationErrorType=FieldUndefined, sourceLocations=[SourceLocation{line=1, column=3}], description='Field updateWarehouseName is undefined'}].
mutation M { updateWarehouseName(id:\"1\",name:\"Warehouse11\") { id, name, virtual } }