How to create GraphQL object with arguments?

1.7k views Asked by At

Is it possible an object with arguments? In other words, can I create an object for running queries like this?

{
    artifact(group: "com.graphql-java", name: "graphql-java") {
        group
        name
        version
    }
}

If yes, how?

I couldn't find a method for creating arguments in graphql.schema.GraphQLObjectType.newObject.

2

There are 2 answers

0
CommonsWare On BEST ANSWER

Call field() on the Builder to add an argument.

0
Ivan Mushketyk On

You can use graphql-java-annotations and define it in a very straightforward way:

@GraphQLName("Query")
class Query {

    @GraphQLField
    static List<Item> items(DataFetchingEnvironment environment) {
        // Fetch all
    }

    @GraphQLField
    static Item item(DataFetchingEnvironment environment, @GraphQLName("id") String id) {
        // Fetch using the "id" argument
    }
}