2016-04-13 11 views
4

GraphQL Java(https://github.com/andimarek/graphql-java)のプロトタイプを作成し、hello worldの例からビルドを開始しようとしています。 GraphiQLを使用して、schemaQLを使用してgraphQLサービスを呼び出すと、スキーマ{hello1}で動作していますが、スキーマ{testPojo}では動作していません。私が実行しているコードを見つけてください。いくつかの人が私に以下のコードの問題点を教えてくれますか?Graphql Java hello world:親スキーマへのサブスキーマの追加が失敗しています

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(); 
} 

答えて

0

私はちょうどあなたのコードをテストしたところ、うまくいきました。あなたは、あなたが間違っているものとしてより具体的にする必要があります。

質問{testPojo}が無効である理由がある場合は、入手したエラーを読んでください。サブ選択が必要です。 GraphQLで複雑なオブジェクト全体を選択することはできません。必要なサブフィールドを指定する必要があります。 {testPojo {id, name}}は有効なクエリであり、スキーマで正常に動作します。

関連する問題