如何使用GraphQL实现将任何数据库连接到GraphQL应用程序?演示的方法将会受到赞赏。
发布于 2017-03-13 05:18:46
有几种可能的解决方案:
A)使用数据对象调用GraphQL查询:
Foo data = perhapsFromDatabase();
ExecutionResult executionResult = schemaFactory.getGraphQL().execute(query, data, variables);如果数据具有getTitle()方法,则可以直接在GraphQL查询中查询该属性。
B)使用DataFetcher:
来自GraphQL docs的示例代码
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();发布于 2017-12-30 01:01:00
这就是我最喜欢GraphQL的地方,它是一个概念,就像REST一样。所以它不依赖于技术。我已经使用gradle、spring-jpa、spring-boot和mongo-db创建了一个应用程序。
PS:我还附加了邮递员API调用,以使其更容易测试。
https://stackoverflow.com/questions/42643634
复制相似问题