使用graphql-yoga,您只需通过执行以下操作即可导入模式: typeDefs: './src/schema.graphql' . 使用apollo-server-express有类似的方法吗?
typeDefs: './src/schema.graphql'
如果没有,如何从外部 .graphql 文件导入typeDef?
.graphql
您可以使用函数 makeExecutableSchema 传入 typeDefs . 像这样的东西:
makeExecutableSchema
typeDefs
import { makeExecutableSchema } from 'graphql-tools'; import mySchema from './src/schema.graphql'; const app = express(); const schema = makeExecutableSchema({ typeDefs: [mySchema], resolvers: { ... }, }); app.use( '/graphql', graphqlExpress({ schema }) );
我通过使用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 })
2 回答
您可以使用函数
makeExecutableSchema
传入typeDefs
. 像这样的东西:我通过使用grahpql-import找到了一种方法,它完全符合我的需要 . 请参阅以下示例代码: