从 .graphql 文件导入构建架构

Yog*_*wal 7 javascript graphql

我想使用以下架构创建 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: {}
  })
);
Run Code Online (Sandbox Code Playgroud)

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

你能告诉我该怎么做吗?

小智 1

只需使用该模块导入所有模式即可gql。通过以下方式安装:

npm i graphql-tag
Run Code Online (Sandbox Code Playgroud)