как объединить другой тип объекта в GraphQLObjectType? - PullRequest
0 голосов
/ 17 марта 2020

создавая простое приложение чата с mon goose и GraphQL, я пытаюсь создать конечную точку, которая будет извлекать сообщения вместе с объектом пользователя, которому принадлежит это указанное c сообщение, однако я не могу ' Не понимаю, как мне сделать какое-то соединение в GraphQLObjectType Типе:

const MessageType = new GraphQLObjectType({
    name: "Message",
    fields: () => ({
        id: {type: GraphQLString},
        text: {type: GraphQLString},
        user: {type: userType} // here I want to fetch the user which is relevant to message's model userId property
    })
});


const MessageSchema = new Schema({
    userId: {type: Schema.Types.ObjectId, ref: 'User'},
    chatId: {type: Schema.Types.ObjectId, ref: 'Chat'},
    createTime: {type: Date, default: Date.now},
    updateTime: {type: Date, default: Date.now},
    text: String,
});

Схема MongoDB для сущности Сообщение:

const MessageSchema = new Schema({
    userId: {type: Schema.Types.ObjectId, ref: 'User'},
    chatId: {type: Schema.Types.ObjectId, ref: 'Chat'},
    createTime: {type: Date, default: Date.now},
    updateTime: {type: Date, default: Date.now},
    text: String,
});

и декларация RootQuery:

const RootQuery = new GraphQLObjectType({

    name: "RootQueryType",
    fields: {
        user: {
            ...
        },
        chats: {
            ...
        },
        messages: {
            type: GraphQLList(messageGraphQLType),
            args: {
                chatId: {type: GraphQLString}
            },
            async resolve(parent, args) {
                return Message.find({chatId: args.chatId});
            }
        }
    }
});
...