查询不会从关系中返回数据

时间:2019-10-19 09:51:56

标签: mongodb express mongoose graphql

我正在观看this视频系列。

在这里,我无法获取有关在以下脚本中创建user的{​​{1}}的数据

这是我的event

app.js

const express = require('express'); const bodyParser = require('body-parser'); const graphqlHttp = require('express-graphql') const { buildSchema } = require('graphql'); const mongoose = require('mongoose'); const bcrypt = require('bcrypt'); const Event = require('./models/event'); const User = require('./models/user'); const app = express(); const conString = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@clusterpl-qiufl.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority` app.use(bodyParser.json()); app.use( '/graphql', graphqlHttp({ schema: buildSchema(` type Event { _id: ID! title: String! description: String! price: Float! date: String! creator: User! } type User { _id: ID! email: String! password: String! createdEvents: [Event!] } input UserInput { email: String! password: String! } input EventInput { title: String! description: String! price: Float! date: String! } type RootQuery { events: [Event!]! } type RootMutation { createEvent(eventInput: EventInput): Event createUser(userInput: UserInput): User } schema { query: RootQuery, mutation: RootMutation } `) , rootValue: { events: () => { return Event.find().populate('creator') .then(events => { console.log(events) return events.map(event => { console.log(event) return { ...event._doc, _id: event.id }; }); }) .catch(err => { throw err; }) }, .. }, graphiql: true }) ); mongoose.connect(conString, {useNewUrlParser: true}).then( () => {console.log('Success !')}, err => { console.log(err) } ) app.listen(3000); user.js

event.js

一旦我提交此graphql查询

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const userSchema = new Schema({
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },

    createdEvents: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Event'
        }
    ]
});

module.exports = mongoose.model('User', userSchema)


const mongoose = require('mongoose');

const Schema = mongoose.Schema

const eventSchema = new Schema({
    title: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    price: {
        type: Number,
        required: true
    },
    date: {
        type: Date,
        required: true
    },

    creator: [
        {
            type: Schema.Types.ObjectId,
            ref: 'User'
        }
    ]
});

module.exports = mongoose.model('Event', eventSchema);

返回query{ events { creator { email } } }

我对graphql完全陌生,任何答案将不胜感激。

1 个答案:

答案 0 :(得分:0)

也许这可能对其他人有帮助,我不知道那里发生了什么,但是通过添加

我获得了预期的结果
const user =  userId => {
    return User.findById(userId).then(user => {
        return { ...user._doc, _id: user.id };
    })
    .catch(err => {
        throw err;
    });
}

方法并将其用于这样的事件解析器功能

events: () => {


return Event.find()
                .populate('creator')
                .then(events => {
                    return events.map(event => {
                        console.log('ev',event._doc)
                        return { 
                            ...event._doc,
                            _id: event.id,
                            // creator: {
                            //     ...event._doc.creator._doc,
                            //     _id: event._doc.creator.id
                            // }
                            creator: user.bind(this, event._doc.creator)
                        };
                    })
                })
                .catch(err => {
                    throw err;
                })
        },