我正在尝试创建一个自定义graphql架构,以便在我的graphql yoga服务器上使用 . graphql yoga服务器只是另一个graphql API的代理,我已经设法从 JSON
格式检索一个模式 . 以下是该架构的预览:
{
"data": {
"__schema": {
"queryType": {
"name": "Query"
},
"mutationType": null,
"subscriptionType": null,
"types": [
{
"kind": "OBJECT",
"name": "Core",
"description": null,
"fields": [
{
"name": "_meta",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "Meta",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "_linkType",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
{
我现在想要使用这个生成的JSON模式并使用它来创建graphql模式以在我的graphql瑜伽服务器中使用 . 我相信正确的方法是使用graphql中的 new GraphQLSchema
方法和根查询 . 这是我的代码尝试这个:
schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: schema.data.__schema
})
});
上面的代码给出了以下错误:
错误:Query.mutationType字段配置必须是对象
不完全确定哪里出错或者这是从生成的JSON创建graphql架构的正确方法?
1 回答
你拥有的JSON是introspection query的结果 . 不幸的是,内省不允许您复制远程模式 . 那是因为虽然它确定了模式中存在哪些字段,但它并没有告诉你应该如何执行它们 . 例如,根据您发布的代码段,我们知道远程服务器公开了一个返回
Meta
类型的_meta
查询 - 但是我们不知道要运行什么代码来解析查询返回的值 .从技术上讲,可以将内省查询的结果从
graphql/utilities
模块传递给buildClientSchema
. 但是,正如文档所指出的那样,架构将不可执行:如果要为另一个GraphQL endpoints 创建代理,最简单的方法是使用
makeRemoteExecutableSchema
来自graphql-tools
.这是基于the docs的示例:
生成的模式是
GraphQLSchema
对象,可以像平常一样使用:graphql-tools
还允许您stitch schemas together,如果您不仅想要代理现有 endpoints ,还想添加它 .