Help with mutations

52 views
Skip to first unread message

Juan Carlos González González

unread,
Nov 28, 2016, 12:59:56 PM11/28/16
to graphql-java
Hi.

I'm evaluating this library and I can submit queries properly. Currently I'm trying to submit a simple mutation but I can't guess how to do it. These are my definitions contained in a guice module:

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'}].


The problem is that my previous request is always assumed as a QUERY. 

Please any help would be appreciated.

Best regards,

Juan Carlos González.


Juan Carlos González González

unread,
Nov 30, 2016, 9:08:04 AM11/30/16
to graphql-java
Looks like the mutation syntax was wrong. I've tested with


mutation M { updateWarehouseName(id:\"1\",name:\"Warehouse11\") { id, name, virtual } }


and is working now.
Reply all
Reply to author
Forward
0 new messages