假设我有这样的结构:
src/
├── user/
│ ├── mutation.js
│ ├── query.js
├── cars/
│ ├── mutation.js
│ ├── query.js
└── schema.js
如何从用户的mutation.js 和query.js 以及汽车的mutation.js 和query.js 组合在schema.js 中创建模式?
假设我有这样的结构:
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
})