使用 graphql-yoga,您可以通过执行以下操作简单地导入模式:typeDefs: './src/schema.graphql'
。 apollo-server-express 是否有类似的方法?
如果没有,如何从外部导入 typeDefs.graphql
file?
我找到了一种通过使用来做到这一点的方法graphql-导入 https://www.npmjs.com/package/graphql-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
现在是异步的,应该作为承诺来处理。只需将其包裹在一个async
功能简单await
it:
async function start() {
const typeDefs = await importSchema(".src/schema.graphql")
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)