ниже - моя схема:
module.exports = buildSchema(`
type Booking {
_id: ID!
event: Event!
user: User!
createdAt: String!
updatedAt: String!
}
type Event {
_id: ID!
title: String!
description: String!
price: Float!
date: String!
creator: User!
}
type User {
_id: ID!
email: String!
password: String!
createdEvents: [Event!]
}
type authData {
userId:ID!
token:String!
tokenExpiriation: Int!
}
input EventInput {
title: String,
description: String!,
date: String,
price:Float!
}
input UserInput {
email: String!
password: String!
}
type RootQuery {
events: [Event!]!
bookings: [Booking!]!
login(email:String!, password:String!): authData!
}
type RootMutation {
createEvent(eventInput:EventInput): Event
createUser(userInput:UserInput): User
bookEvent(eventId: ID!): Booking!
cancelBooking(bookingId: ID!): Event!
}
schema {
query: RootQuery
mutation: RootMutation
}
`)
Events.js
module.exports = {
events: async () => {
try {
const events = await Event.find();
return events.map((event) => {
return eventsTemplate(event);
});
} 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),
creator:req.userId,
});
let createdEvent;
try {
const result = await event.save();
createdEvent = eventsTemplate(result);
const creator = await User.findById(req.userId);
if (!creator) {
throw new Error("User not found.");
}
creator.createdEvents.push(event);
await creator.save();
return createdEvent;
} catch (err) {
console.log(err);
throw err;
}
},
};
мой шаблон событий ниже
const eventsTemplate = (event) => {
return {
...event._doc,
_id: event.id,
date: dateToString(event._doc.date),
creator: user.bind(this, event.creator),
};
};