GraphQL 如何从文件分隔的突变和查询创建模式

GraphQL how to create a schema from file separated mutations and queries

假设我有这样的结构:

src/
├── user/
│   ├── mutation.js
│   ├── query.js
├── cars/
│   ├── mutation.js
│   ├── query.js
└── schema.js

如何根据用户的 mutation.js 和 query.js 以及汽车的 mutation.js 和 query.js 在 schema.js 中组合创建架构?

@Hangindev 是对的,为了连接我需要导出 GraphQLObjectType 字段的所有突变和查询,如下所示:

const userMutation = {
    addUser: {
        type: UserType,
        args: {
            username: {type: GraphQLString},
            email: {type: GraphQLString},
        },
        resolve(parent, args) {
            let author = new User({
                username: args.username,
                email: args.email,
            });
            return author.save();
        }
    },
}

module.exports = userMutation

稍后将它们添加到架构中:

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        ...userMutation,
        ...foodPostMutation
    }
})

const Query = new GraphQLObjectType({
    name: 'Query',
    fields: {
        ...userQuery,
        ...foodPostQuery
    }
})

module.exports = new GraphQLSchema({
    query: Query,
    mutation: Mutation
})