Graphql - 从生成的 JSON 文件创建模式

red*_* 87 0 javascript node.js graphql

我正在尝试创建一个自定义 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": [
            { 
Run Code Online (Sandbox Code Playgroud)

我现在想要获取这个生成的 JSON 模式并使用它来创建一个 graphql 模式以在我的 graphql Yoga 服务器中使用。我相信正确的方法是使用new GraphQLSchemagraphql 中的方法以及根查询。这是我尝试这样做的代码:

schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: schema.data.__schema
  })
});
Run Code Online (Sandbox Code Playgroud)

上面的代码给我以下错误:

错误:Query.mutationType 字段配置必须是对象

不完全确定哪里出了问题,或者这是否是从生成的 JSON 创建 graphql 模式的正确方法?

Dan*_*den 5

您拥有的 JSON 是内省查询的结果。不幸的是,内省不允许您复制远程模式。这是因为,虽然它确实识别了架构中存在哪些字段,但它并没有告诉您有关如何执行它们的任何信息。例如,根据您发布的代码片段,我们知道远程服务器公开了一个_meta返回Meta类型的查询,但我们不知道要运行哪些代码来解析查询返回的值。

从技术上讲,可以将内省查询的结果传递给buildClientSchema模块graphql/utilities。但是,正如文档指出的那样,该架构将不可执行:

给定客户端运行内省查询的结果,创建并返回一个 GraphQLSchema 实例,然后该实例可以与所有 GraphQL.js 工具一起使用,但不能用于执行查询,因为内省并不代表“解析器”、“解析器” ”或“序列化”函数或任何其他服务器内部机制。

如果您想创建另一个 GraphQL 端点的代理,最简单的方法是使用makeRemoteExecutableSchemafrom graphql-tools

这是基于文档的示例:

import { HttpLink } from 'apollo-link-http';
import fetch from 'node-fetch';

const link = new HttpLink({ uri: 'http://your-endpoint-url/graphql', fetch });
async function getRemoteSchema () {
  const schema = await introspectSchema(link);
  return makeRemoteExecutableSchema({
    schema,
    link,
  });
}
Run Code Online (Sandbox Code Playgroud)

生成的模式是一个GraphQLSchema可以像平常一样使用的对象:

import { GraphQLServer } from 'graphql-yoga'

async function startServer () {
  const schema = await introspectSchema(link);
  const executableSchema = makeRemoteExecutableSchema({
    schema,
    link,
  });
  const server = new GraphQLServer({ schema: executableSchema })
  server.start()
}

startServer()
Run Code Online (Sandbox Code Playgroud)

graphql-tools如果您不仅想代理现有端点,而且还想添加到它,还允许您将架构缝合在一起。