gpt4 book ai didi

javascript - 从 .graphql 文件导入构建架构

转载 作者:行者123 更新时间:2023-12-02 21:35:43 29 4
gpt4 key购买 nike

我想创建具有以下架构的 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 文件中。一种方法是我们可以读取文件,但我认为效率不高。

你能告诉我该怎么做吗?

最佳答案

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

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

然后,在 JS 中:

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

或者在 TypeScript 中:

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

关于javascript - 从 .graphql 文件导入构建架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60499865/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com