Arguments of an object type

37 views
Skip to first unread message

Dmitri Pisarenko

unread,
Jan 9, 2017, 4:24:32 AM1/9/17
to graphql-java
Hello!

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

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

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

Thanks in advance

Dmitri Pisarenko

Matthew Cirillo

unread,
Jan 9, 2017, 9:09:38 PM1/9/17
to graphql-java
Arguments are defined on the GraphQLFieldDefinition class. So when you create a field using GraphQLFieldDefinition.newFieldDefinition() you will be able to specify arguments.

Natalya Zaykova

unread,
Jan 11, 2017, 2:12:25 PM1/11/17
to graphql-java
Something like this :

artifact.java:
   
public static GraphQLObjectType ArtifactType = newObject()
            .name("artifact")
            .description("artifact...")
            .field(newFieldDefinition().name("group").description("group of...")
                    .type(GraphQLString)
                    .dataFetcher(getGroupFetcher()))
                    .field(newFieldDefinition().name("name").description("name of...")
                    .type(GraphQLString)
                    .dataFetcher(getNameFetcher()))
                     .field(newFieldDefinition().name("version").description("version of...")
                    .type(GraphQLInt)
                    .dataFetcher(getNameFetcher()))
            .build();



    public static GraphQLFieldDefinition getArtifactWithArguments() {
        return newFieldDefinition()
                .type(ArtifactType ).name("ArtifactTypeGroupName")
                .argument(new GraphQLArgument("groupArg", GraphQLString))
                .dataFetcher(getObjectFetcherArtifactWithArguments())
                .build();
   
    }

getObjectFetcherArtifactWithArguments() {
 return new DataFetcher() {
            @Override
            public Object get(DataFetchingEnvironment env) {
                logger.trace("getting group argument...");
                String groupArg = env.getArgument("groupArg");
                Artifact result = new Artifact ();
                 result.setGroup(groupArg) ;
             return result;

....
}
   




Reply all
Reply to author
Forward
0 new messages