2017-03-07 50 views

回答

2

有幾種可能的解決方案:

A)調用GraphQL查詢與數據對象:

Foo data = perhapsFromDatabase(); 
ExecutionResult executionResult = schemaFactory.getGraphQL().execute(query, data, variables); 

如果數據有一個方法的getTitle(),您可以直接在您查詢該屬性GraphQL查詢。從GraphQL docs

示例代碼:

B)使用DataFetcher

DataFetcher<Foo> fooDataFetcher = new DataFetcher<Foo>() { 
    @Override 
    public Foo get(DataFetchingEnvironment environment) { 
     // environment.getSource() is the value of the surrounding 
     // object. In this case described by objectType 
     Foo value = perhapsFromDatabase(); // Perhaps getting from a DB or whatever 
     return value; 
    } 
}; 

GraphQLObjectType objectType = newObject() 
     .name("ObjectType") 
     .field(newFieldDefinition() 
       .name("foo") 
       .type(GraphQLString) 
       .dataFetcher(fooDataFetcher)) 
     .build(); 
0

這是我喜歡關於GraphQL最好的,這是一個概念,比如REST。所以它與技術無關。我使用gradle,spring-jpa,spring-boot和mongo-db創建了一個micro-blogging應用程序。

PS:我也附加postman API調用,使測試更容易。