【发布时间】:2018-06-26 01:30:50
【问题描述】:
如果客户不知道架构并且想自省和理解 GraphQL API,那么 GraphQL 似乎无法支持递归自省。关于我的观点,请参见以下示例
首先,以下是我在高层的架构定义:
// schema.js
...
...
const AuthorType = new GraphQLObjectType({
name: "Author",
description: "This represent an author",
fields: () => ({
id: {type: new GraphQLNonNull(GraphQLString)},
name: {type: new GraphQLNonNull(GraphQLString)},
twitterHandle: {type: GraphQLString}
})
});
const PostType = new GraphQLObjectType({
name: "Post",
description: "This represent a Post",
fields: () => ({
id: {type: new GraphQLNonNull(GraphQLString)},
title: {type: new GraphQLNonNull(GraphQLString)},
body: {type: GraphQLString},
author: {
type: AuthorType,
resolve: function(post) {
return _.find(Authors, a => a.id == post.author_id);
}
}
})
});
// This is the Root Query
const BlogQueryRootType = new GraphQLObjectType({
name: 'BlogAppSchema',
description: "Blog Application Schema Query Root",
fields: () => ({
authors: {
type: new GraphQLList(AuthorType),
description: "List of all Authors",
resolve: function() {
return Authors
}
},
posts: {
type: new GraphQLList(PostType),
description: "List of all Posts",
resolve: function() {
return Posts
}
}
})
});
当有人使用以下查询子句查询架构时:
{
__type(name: "BlogAppSchema") {
name
fields {
name
description
type {
name
}
}
}
}
她得到以下结果:
{
"data": {
"__type": {
"name": "BlogAppSchema",
"fields": [
{
"name": "authors",
"description": "List of all Authors",
"type": {
"name": null
}
},
{
"name": "posts",
"description": "List of all Posts",
"type": {
"name": null
}
}
]
}
}
}
阅读源码,我们知道作者是一个AuthorType的列表。但是没有访问源代码的用户如何从上面得到的结果中进一步内省“作者”字段(类型字段在这里显示“null”)?她似乎无法从上述结果中知道authors 是Author 的列表。有没有办法让她进一步反省?
【问题讨论】:
标签: graphql graphql-js