2016-04-21 48 views
0

我執行下面的代碼,結果世界:您好與GraphQLObjectType

{TestPojo={id=null, name=null}} 

我期待的結果是{TestPojo={id="1", name="Jack"}}。我錯過了什麼?

import static graphql.Scalars.GraphQLString; 
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; 
import static graphql.schema.GraphQLObjectType.newObject; 

import java.util.Map; 

import graphql.GraphQL; 
import graphql.schema.DataFetcher; 
import graphql.schema.DataFetchingEnvironment; 
import graphql.schema.GraphQLObjectType; 
import graphql.schema.GraphQLSchema; 

public class HelloWorld { 
    public static void main(String[] args) { 
     // 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().name(testPojo.getName()) 
                      .type(testPojo) 
                      .dataFetcher(new DataFetcher() { 
                       @Override 
                       public Object get(DataFetchingEnvironment arg0) { 
                        Object a = new GrapgQLSampleController() 
                             .greeting2(); 
                        return a; 
                       } 
                      }) 
                      .build()) 
               .build(); 


     GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).build(); 
     Map<String, Object> result = (Map<String, Object>) new GraphQL(schema).execute("{TestPojo {id,name}}") 
                       .getData(); 
     System.out.println(result); 
     // Prints: {TestPojo={id=null, name=null}} 
    } 

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

    // inner pojo 
    class TestPojo { 
     public String id; 
     public String name; 

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

回答

-1

試圖通過修復代碼來教學。需要注意的是靜態內容來源於無論是在.staticValue( 「X」)

GraphQLObjectType testPojo = newObject().name("TestPojo") 
              .description("This is a test POJO") 
              .field(newFieldDefinition().type(GraphQLString).name("id").staticValue("Test1")) 
              .field(newFieldDefinition().type(GraphQLString).name("name").staticValue("pojo1")) 
              .build(); 

    GraphQLSchema schema = GraphQLSchema.newSchema().query(testPojo).build(); 
    Map<String, Object> result = (Map<String, Object>) new GraphQL(schema).execute("{id,name}") 
                      .getData(); 
    System.out.println(result); 

將打印{ID = Test1的,名稱= pojo1}

可以用DataFetcher更新的例子,當我有時間

+0

此答案未回答提問 –

0

嘗試在您的TestPojo類中添加getter方法,因爲這些類將在類中聲明時調用。否則在現場定義你自己的DataFetcher

class TestPojo { 
    public final String id; 
    public final String name; 

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

    String getId() { 
     return id; 
    } 

    String getName() { 
     return name; 
    } 
} 
相關問題