当我尝试在 localhost:3000 / graphql 中看到此消息时,我看不到数据,因为它给了我获取时间错误。我正在尝试获取类似:- 查询: *
mutation {createEvent(eventInput:{标题:“测试”,说明: “这是测试”,价格:9.99,日期:“ 2019-11-30T14:23:10.948Z”}){ 价格}}
获取时间错误即将到来
,我确实尝试更改了变量名,但没有帮助。 如果您需要查看其余的配置,这是github链接。 :- https://github.com/ThakurAseem/react-event-booking.git
从我观察到的主要原因是,这是createEvent:async args => {part以某种方式不返回任何内容。此数据未保存。
它可能卡在了await函数中:-在这里
- const result = await event.save(); *
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: async () => {
try {
const events = await Event.find();
return events.map(event => {
return { ...event._doc, _id: event.id };
});
}
catch (err) {
throw err;
}
},
createEvent: async args => {
const event = new Event({
title: args.eventInput.title,
description: args.eventInput.description,
price: +args.eventInput.price,
date: new Date(args.eventInput.date)
});
try {
const result = await event.save();
console.log(result);
return { ...result._doc, _id: event.id };
}
catch (err) {
console.log(err);
throw err;
}
}
},
graphiql: true
})
);
mongoose
.connect('mongodb+srv://ABC:1234@cluster0-yszlp.mongodb.net/test',{ useNewUrlParser: true },{ useUnifiedTopology: true })
.then('on',()=>{
app.listen(3000,()=>{
console.log("This is running..");
});
})
.catch('once',(err)=>{
console.log(err);
})