【问题标题】:Import build schema from .graphql file从 .graphql 文件导入构建模式
【发布时间】:2022-02-17 06:00:06
【问题描述】:

我想创建一个具有以下架构的 GraphQL API

app.use(
  "/graphql",
  graphQlHttp({
    schema: buildSchema(`
    type Event {
        _id: ID!
        title: String!
        description: String!
        price: Float!
    }

    input EventInput {
        title: String!
        description: String!
        price: Float!
    }

    type QueryResolver {
        getEvents: [Event!]!
    }

    type MutationResolver {
        createEvent(eventInput: EventInput): [Event!]!
    }

    schema {
        query: QueryResolver
        mutation: MutationResolver
    }
    `),
    rootValue: {}
  })
);

目前我在我的主文件中使用它作为字符串。我想将它分离到一个 graphql 文件中。一种方法是我们可以读取文件,但我认为它效率不高。

您能告诉我该怎么做吗?

【问题讨论】:

    标签: javascript graphql


    【解决方案1】:

    您可以添加此节点模块: https://github.com/ardatan/graphql-import-node

    然后将您的架构移动到 mySchema.graphql 之类的位置。

    然后,在 JS 中:

    const mySchema = require('./mySchema.graphql');
    

    或在 TypeScript 中:

    import * as mySchema from './mySchema.graphql';
    

    【讨论】:

    • 你能解释一下吗
    • 它只是将字符串模式抽象到不同的文件中。您在示例中定义的字符串模式(buildSchema() 的参数)可以放在单独的 .graphql 文件中,并作为 JS 模块导入。然后您可以将其用作buildSchema(myImportedSchema)。这里的好处是您可以为这些 .graphql 文件类型在 IDE 中添加 GraphQL 语法支持。
    • 不工作!显示标识符错误,可能是将其解释为 js 文件
    • 您能否添加更多关于您的错误以及如何导入错误的详细信息?你使用的是 TypeScript 还是 vanilla JS?
    【解决方案2】:

    只需使用gql 模块导入所有架构。安装方式:

    npm i graphql-tag
    

    【讨论】:

      【解决方案3】:

      您可以使用 graphql 工具。

      const { loadSchemaSync } = require("@graphql-tools/load");
      const { GraphQLFileLoader } = require("@graphql-tools/graphql-file-loader");
      const { addResolversToSchema } = require("@graphql-tools/schema");
      const { join } = require("path");
      
      const schemaWithResolvers = addResolversToSchema({
        schema: loadSchemaSync(join(__dirname, "./(your graphql file).graphql"), {
          loaders: [new GraphQLFileLoader()],
        }),
        resolvers: {},
      });
      

      而且,你的 graphql 文件是

      type Event {
          _id: ID!
          title: String!
          description: String!
          price: Float!
      }
      
      input EventInput {
          title: String!
          description: String!
          price: Float!
      }
      
      type QueryResolver {
          getEvents: [Event!]!
      }
      
      type MutationResolver {
          createEvent(eventInput: EventInput): [Event!]!
      }
      
      schema {
          query: QueryResolver
          mutation: MutationResolver
      }
      

      那么,你只需要使用 schemaWithResolvers.builldSchema 是不必要的。

      app.use(
        "/graphql",
        graphQlHttp({
          schema: schemaWithResolvers 
        })
      );
      

      【讨论】:

        【解决方案4】:

        我没有评论的声誉,所以我会把它放在这里。

        如果有人关注@Tim O'Connell 的回答,想知道如何将导入的 Schema 传递给 BuildSchema(),因为它不再是字符串而是 DocumentNode,您实际上需要使用函数 BuildASTSchema()

        请注意,BuildSchema() 只是 BuildASTSchema() 的包装器,它将给定的字符串解析为 DocumentNode。 (source)

        所以回顾一下,做这样的事情:

        import 'graphql-import-node';
        import { buildASTSchema } from 'graphql';
        import * as mySchema from './schema.graphql';
        
        export = buildASTSchema(mySchema);
        

        【讨论】:

          猜你喜欢
          • 2020-12-19
          • 2019-04-22
          • 2019-05-11
          • 2018-05-21
          • 2023-04-01
          • 1970-01-01
          • 2016-01-29
          • 1970-01-01
          • 2018-10-23
          相关资源
          最近更新 更多