gpt4 book ai didi

graphql - Apollo GraphQL - 将 .graphql 模式导入为 typeDefs

转载 作者:行者123 更新时间:2023-12-03 15:44:58 27 4
gpt4 key购买 nike

使用 graphql-yoga,您可以通过执行以下操作简单地导入您的模式:typeDefs: './src/schema.graphql' . apollo-server-express 是否有类似的方法?

如果没有,如何从外部导入 typeDefs .graphql文件?

最佳答案

我通过使用 grahpql-import 找到了一种方法这正是我需要的。请参阅下面的示例代码:

import { ApolloServer } from 'apollo-server-express'
import { importSchema } from 'graphql-import'
import Query from './resolvers/Query'

const typeDefs = importSchema('./src/schema.graphql')
const server = new ApolloServer({
typeDefs,
resolvers: {
Query
}
})

const app = express()
server.applyMiddleware({ app })

app.listen({ port: 4000 })

**

更新:graphql-import v0.7+

**
importSchema现在是异步的,应该作为 promise 处理。只需将其包裹在 async 中功能和简单 await它。
async function start() {
const typeDefs = await importSchema(".src/schema.graphql")
}

关于graphql - Apollo GraphQL - 将 .graphql 模式导入为 typeDefs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53385525/

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