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

时间:2018-11-20 02:52:08

标签: graphql apollo apollo-server

使用graphql-yoga,您只需执行以下操作即可导入架构:typeDefs: './src/schema.graphql'。使用apollo-server-express是否有类似的方法?

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

2 个答案:

答案 0 :(得分:1)

您可以使用函数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 })
);

答案 1 :(得分:0)

我找到了一种使用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 })