如何分离架构和解析器并对其进行奖励apollo-server-express

时间:2018-09-22 13:55:02

标签: graphql apollo-server

我有User和Post typeDefs /解析器,我想将它们分开以便获取

User.schema.js 
User.resolver.js

Post.schema.js 
Post.resolver.js

link.Schema.js
link.resolvers.js

如何执行此操作并将其合并为仅一个typeDefs /解析器,以将其传递给

const server = new ApolloServer({
  typeDefs,
  resolvers
});

2 个答案:

答案 0 :(得分:2)

我一直在使用merge-graphql-schemas包进行类型定义,并为解析器使用lodash深度对象合并功能,如下所示:

import merge from "lodash/merge"
import { mergeTypes } from "merge-graphql-schemas"
import UserSchema from "./User.schema"
import UserResolvers from "./User.resolvers"
import PostSchema from "./Post.schema"
import PostResolvers from "./Post.resolvers"
import LinkSchema from "./Link.schema"
import LinkResolvers from "./Link.resolvers"

const typeDefs = mergeTypes([UserSchema, PostSchema, LinkSchema])
const resolvers = merge(UserResolvers, PostResolvers, LinkResolvers)

const server = new ApolloServer({
  typeDefs,
  resolvers
});

编辑:请注意,graphql-tools's mergeSchemas现在是现在建议使用apollo服务器进行模式拼接的方法。

答案 1 :(得分:0)

并不需要更多的依赖来合并类型,因为 graphql-tools 非常适合它们。 如果有帮助,请看看https://github.com/techyaura/graphqlNodeMongodb-server/tree/master/src/gql

我在仓库https://github.com/techyaura/graphqlNodeMongodb-server/tree/master/src/gql/types中有两个文件todo.types.jsuser.type.js,我只是将它们串联起来,效果很好。

注意:我不是在使用 APOLLO GRAPHQL ,而是在简单地使用express-graphql