所以我有一个graphQL userType
和EventType
,在我的userType
上,我必须使用EventType
进行嵌套查询,但是我还需要返回用户信息,但是EventType
没有与用户信息相同的字段,因此要通过测试代码,我不得不手动将UserType
复制粘贴到EventType
,但是我不希望这样解决。有没有办法喜欢合并类型?我使用mongoDB作为数据库,因此在尝试进行POPULATE
时,用户信息会返回,但是Graphql使其变得困难。抱歉,我对graphql还是陌生的。
这就是我对EventType
所做的通过测试的结果。在修复程序下,我复制UserType
module.exports = {
EventType: new GraphQLObjectType({
name: 'Event',
fields: () => ({
id: {type: GraphQLID},
organizerId: {type: new GraphQLList(GraphQLID)},
title: {type: GraphQLString},
image: {type: GraphQLString},
description: {type: GraphQLString},
location: {type: GraphQLString},
items: {type: GraphQLInt},
date: {type: GraphQLString},
attendeesId: {type: new GraphQLList(GraphQLID)},
supplies: {type: new GraphQLList(GraphQLString)},
test: {type: GraphQLString},
// FIXME: USER TYPES!
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
// ! TYPE RELATION
userRelatedToEvent: {
type: UserType,
resolve: async (parent, args) => {
const id = {
id: parent.organizerId
};
return await getCurrentUser(id);
}
}
})
})
};
这是我在UserType
上执行的代码,它在EventType
上进行类型关系,请参见resolve
prop
module.exports = {
UserType: new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
eventsId: {type: new GraphQLList(GraphQLID)}, // <-- this will be the query for userRelatedToUser
statusCode: {type: GraphQLInt},
isSuccess: {type: GraphQLBoolean},
msg: {type: GraphQLString},
test: {type: GraphQLString}, // testing query
// errors: { type: GraphQ LString },
eventRelatedToUser: {
type: require('./eventTypeDef').EventType,
resolve: async (parent, args) => {
const event = {
event: parent.eventsId
};
const test = await getEventWithEventId(event); // <-- queries event
const id = {
id: test.attendeesId
};
const users = await getCurrentUser(id); // <-- user info
console.log(users);
return {...users, ...test};
}
}
})
})
};