必须选择子字段。您是说\“ createEvent {...} \”吗?”,[graphql]

时间:2019-05-15 09:28:36

标签: mongodb mongoose graphql graphql-js express-graphql

我正在学习graphql,所以请耐心等待。

我的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 Event = require("./models/event");

const app = express();

app.use(bodyParser.json());

app.use('/graphql', graphqlHttp({

    schema: buildSchema(`
        type Event{
            _id: ID!
            title: String!
            description: String!
            price: Float!
            date: String!
        }

        input EventInput{
            title: String!
            description: String!
            price: Float!
            date: String!
        }

        type RootQuery{
            events: [Event!]!
        }

        type RootMutation {
            createEvent(eventInput: EventInput): Event
        }

        schema {
            query: RootQuery
            mutation: RootMutation
        }
    `),
    rootValue: {
        events: () => {
            return events;
        },
        createEvent: args => {
            const event = new Event({
                title: args.eventInput.title,
                description: args.eventInput.description,
                price: +args.eventInput.price,
                date: new Date(args.eventInput.date)
            });
            return event
                .save()
                .then(result => {
                    console.log(result);
                    return {...result._doc};
                })
                .catch(err => {
                    console.log(err);
                    throw err;
                });            
        }        
    },
    graphiql: true
}));


mongoose.connect(`mongodb://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@localhost:27017/graphqldb`, {useNewUrlParser: true});

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection errror:'));
db.once('open', function(){
    console.log('Connection open');
    app.listen(5000);
});

我的event.js看起来像这样:

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
    }
});

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

当我尝试访问URL http://localhost:5000/graphql

时,
mutation {
  createEvent(eventInput: {title: "title", description: "description", price: 9.99, date: "2019-05-15T08:04:26.461Z"})
}

我遇到与子字段相关的错误:

  

{“错误”:[       {         “ message”:类型为“ Event \”的“ Field \” createEvent \“必须具有子字段的选择。您是说\” createEvent {...} \“?”,         “位置”:[           {             “行”:3,             “栏”:3           }

请让我知道我在想什么。据我了解,我的字段不包含任何子字段。

谢谢。

更新:

这不是所发布问题的重复内容。您可以在我的突变中看到,我提供了所有必填字段,但仍然收到此错误。

仅当我尝试使用猫鼬时才发生错误,然后才按预期工作。

0 个答案:

没有答案