Hi I am making a booking system following the tutorial series https://www.youtube.com/watch?v=MRrcUQhZwBc&list=PL55RiY5tL51rG1x02Yyj93iypUuHYXcB_&index=8 And I am following them(while changing names of fields to suit my database more) Here is my User.js:(which defines user)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
username: {
type: String,
required: true
},
studentnumber: {
type: String,
required: true
},
createdEvents: [
{
type: Schema.Types.ObjectId,
ref: 'Event'
}
]
});
module.exports = mongoose.model('User', userSchema);
And here is my App.js(main file the one that I run for GraphiQL on localhost:3002/graphql)
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('bcryptjs');
const User = require('./models/user')
const Event = require('./models/events');
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!
}
type User {
_id: ID!
username: String!
studentnumber: String
}
input EventInput {
title: String!
description: String!
price: Float!
date: String!
}
type UserInput {
username: String!
studentnumber: 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()
.then(events => {
return events.map(event => {
return { ...event._doc, _id: event.id };
});
})
.catch(err => {
throw err;
});
},
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, _id: result._doc._id.toString() };
})
.catch(err => {
console.log(err);
throw err;
});
},
createUser: args => {
return bcrypt
.hash(args.userInput.studentnumber, 12)
.then(hashedNum => {
const user = new User({
username: args.userInput.username,
studentnumber: hashedNum,
});
return user.save();
})
.then(result => {
return {...result._doc, _id: result.id};
})
.catch(err => {
throw err;
});
}
},
graphiql: true
})
);
mongoose
.connect(
`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.scgam.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority`
)
.then(() => {
app.listen(3002);
})
.catch(err => {
console.log(err);
});
And running them gives me this error message when trying to createUser in GraphiQl
While I should get this(but with different field names)
Does anyone know how to fix this? Thanks
I figured it out it was because I put
type
instead ofinput
in front ofUserInput{}
so had it with the wrong type