简体   繁体   中英

GraphQL : Query failed to validate

I'm testing graph-spqr librabry in a simple java program.

This is what I've done so far:

public class GraphQLResolver {
    
    private String status;
    private Integer price;
    
    @GraphQLMutation(name="updateStatusOrder")
    public void updateStatusOrder(@GraphQLArgument(name="id") String orderId,@GraphQLArgument(name="status") String status) {
       //to do
    }
    
    @GraphQLQuery(name="getStatus")
    public String getStatus(@GraphQLArgument(name="id") String orderId) {
        this.status="Example of status";
        return this.status;
    }

}

Then, I calling building GraphQL methods in main method

public class Main {

    public static void main(String[] args) {

        GraphQLSchema schema = new GraphQLSchemaGenerator()
        .withOperationsFromSingleton(new GraphQLResolver()) 
        .generate(); //done ;)

        GraphQL graphQL = new GraphQL.Builder(schema).build();  
        
        ExecutionResult result = null;
        result = graphQL.execute("{getstatus(id:123){status}}");
    }

}

I have error : Query failed to validate : '{status(id:123){status}}'

What is wrong?

I guess the correct query should be :

{
   getstatus(id:"123")
}

with the following reasons :

  1. You define the id argument as String , so need to use double quote around the argument value.

  2. The getStatus query is defined to return a String which is a scalar but not an object type in term of GraphQL. So you don't need to further define what of its fields to be returned as the scalar does not have any fields for you to pick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM