简体   繁体   English

GraphQL:查询未能验证

[英]GraphQL : Query failed to validate

I'm testing graph-spqr librabry in a simple java program.我正在一个简单的 java 程序中测试 graph-spqr 库。

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然后,我在 main 方法中调用构建 GraphQL 方法

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}}'我有错误:查询无法验证:'{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.您将 id 参数定义为 String ,因此需要在参数值周围使用双引号。

  2. The getStatus query is defined to return a String which is a scalar but not an object type in term of GraphQL. getStatus查询被定义为返回一个String ,它是一个标量但不是 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.因此,您无需进一步定义要返回的字段,因为标量没有可供您选择的任何字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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