【问题标题】:Graphql Java hello world: Adding a sub-schema to parent schema is failingGraphql Java hello world:向父模式添加子模式失败
【发布时间】:2016-08-04 20:29:19
【问题描述】:

我正在尝试对 GraphQL Java(https://github.com/andimarek/graphql-java) 进行原型设计,并开始从 hello world 示例构建它。我正在使用 GraphiQL 调用带有模式的 graphQL 服务,它适用于模式 {hello1},但不适用于模式 {testPojo}。请在下面找到我正在运行的代码。有人可以告诉我以下代码有什么问题吗?

static GraphQLSchema schema = null;
 /**
  * POJO to be returned for service method 2
  * @author 
  *
  */
 private class TestPojo {
     String id;
     String name;

     TestPojo(String id, String name) {
         this.id = id;
         this.name = name;
     }

     public String getId() {
         return id;
     }

     public void setId(String id) {
         this.id = id;
     }

     public String getName() {
        return name;
     }

     public void setName(String name) {
         this.name = name;
     }
 }

/**
 * service method 1
 * @return
 */
public String greeting1() {
    return "Hello123";
 }

/**
 * service method 2
 * @return
 */
public TestPojo greeting2() {
    return new TestPojo("1","Jack");
}


/**
 * GraphQl endpoint invoked using GraphiQl
 * @param query
 * @return
 */
@RequestMapping("/query")
public Object testGraphQLWithQuery(@RequestParam("query") String query) {
    return new GraphQL(schema).execute(query).getData();
}

// Schema definition for graphQL
static {

    // sub schema to be added to parent  schema
     GraphQLObjectType testPojo = newObject().name("TestPojo").description("This is a test POJO")
            .field(newFieldDefinition().name("id").type(GraphQLString).build())
            .field(newFieldDefinition().name("name").type(GraphQLString).build())
            .build();


    // parent schema
    GraphQLObjectType queryType = newObject().name("helloWorldQuery")
            .field(newFieldDefinition().type(GraphQLString).name("hello1").dataFetcher(new DataFetcher() {

                @Override
                public Object get(DataFetchingEnvironment arg0) {
                    Object a = new GrapgQLSampleController().greeting1();
                    return a;
                }
            }).build())
            .field(newFieldDefinition().type(testPojo).name("testPojo").dataFetcher(new DataFetcher() {

                @Override
                public Object get(DataFetchingEnvironment arg0) {
                    Object a = new GrapgQLSampleController().greeting2();
                    return a;
                }
            }).build())
            .build();

     schema = GraphQLSchema.newSchema().query(queryType).build();
}

【问题讨论】:

    标签: java graphql graphql-java


    【解决方案1】:

    我刚刚测试了您的代码,它似乎工作得很好。您必须更具体地说明您认为错误的地方。

    如果您的问题是为什么查询 {testPojo} 无效,那么您应该已经阅读了您收到的错误:需要子选择。您不能在 GraphQL 中选择整个复杂对象,您必须指定所需的子字段,例如{testPojo {id, name}} 是一个有效的查询,适用于您的架构。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2022-08-16
      • 2023-04-11
      相关资源
      最近更新 更多