首页 文章

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

提问于
浏览
0

我正在尝试创建一个自定义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 回答

  • 3

    你拥有的JSON是introspection query的结果 . 不幸的是,内省不允许您复制远程模式 . 那是因为虽然它确定了模式中存在哪些字段,但它并没有告诉你应该如何执行它们 . 例如,根据您发布的代码段,我们知道远程服务器公开了一个返回 Meta 类型的 _meta 查询 - 但是我们不知道要运行什么代码来解析查询返回的值 .

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

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

    如果要为另一个GraphQL endpoints 创建代理,最简单的方法是使用 makeRemoteExecutableSchema 来自 graphql-tools .

    这是基于the docs的示例:

    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,
      });
    }
    

    生成的模式是 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()
    

    graphql-tools 还允许您stitch schemas together,如果您不仅想要代理现有 endpoints ,还想添加它 .

相关问题