'GraphQLError [Object]: Syntax Error: Expected ":", found Name "User". in express-graphql
I am trying to create a user using createUser mutation in express.js using express-graphql. But as I try to run the code using npm start I get the following error:
GraphQLError [Object]: Syntax Error: Expected ":", found Name "User".
My schema is:
const { buildSchema } = require("graphql");
module.exports = buildSchema(`
type Option {
value: String!
text: String!
key: String!
correct: Boolean
}
type FormData {
id: ID!
required: Boolean!
element: String!
text: String!
canHavePageBreakBefore: Boolean!
canHaveAlternateForm: Boolean!
canHaveDisplayHorizontal: Boolean!
canHaveOptionCorrect: Boolean!
canHaveOptionValue: Boolean!
canPopulateFromApi: Boolean!
canHaveAnswer: Boolean
href: String
src: String
dateFormat: String
timeFormat: String
showTimeSelect: Boolean
showTimeSelectOnly: Boolean
_href: String
file_path: String
step: Int
default_value: Int
min_value: Int
max_value: Int
min_label: String
max_label: String
field_name: String
label: String
dirty: Boolean
options: [Option!]
}
type Form {
_id: ID!
title: String!
creator: User!
description: String!
formData: [FormData!]
createdAt: String!
updatedAt: String!
type User {
_id: ID!
name: String!
email: String!
password: String
college: String
createdForms: [Form!]
bookmarks: [Form!]
dob: String
imageUrl: String
}
input UserInputData {
name: String!
email: String!
password: String!
college: String!
}
type RootMutation {
createUser(userInput: UserInputData): User!
}
schema {
mutation: RootMutation
}
`);
My resolver function is:
const User = require("../models/user");
const bcrypt = require("bcryptjs");
module.exports = {
createUser: async function ({ userInput }, req) {
const existingUser = await User.findOne({ email: userInput.email });
if (existingUser) {
const error = new Error("User exists already.");
throw error;
}
const hashedPassword = await bcrypt.hash(userInput.password, 12);
const user = new User({
name: userInput.name,
email: userInput.email,
password: hashedPassword,
college: userInput.college,
});
const createdUser = await user.save();
return { ...createdUser._doc, _id: createdUser._id.toString() };
},
};
The complete error is:
C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:1413
throw (0, _syntaxError.syntaxError)(this._lexer.source, token.start, "Expected ".concat(getTokenKindDesc(kind), ", found ").concat(getTokenDesc(token), "."));
^
GraphQLError [Object]: Syntax Error: Expected ":", found Name "User".
at syntaxError (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\error\syntaxError.js:15:10)
at Parser.expectToken (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:1413:40)
at Parser.parseFieldDefinition (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:881:10)
at Parser.optionalMany (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:1503:28)
at Parser.parseFieldsDefinition (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:868:17)
at Parser.parseObjectTypeDefinition (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:812:23)
at Parser.parseTypeSystemDefinition (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:708:23)
at Parser.parseDefinition (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:150:23)
at Parser.many (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:1523:26)
at Parser.parseDocument (C:\Users\Asus\Desktop\Project A\project-g\node_modules\graphql\language\parser.js:115:25) {
locations: [ { line: 51, column: 8 } ]
}
Can anyone please help me out? I can't figure it out. Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
