GraphQLError: Syntax Error: Cannot parse the unexpected character ";"

user8817816 picture user8817816 · Jun 14, 2018 · Viewed 7.2k times · Source

I'm a beginner in GraphQL. While setting the server, connecting to mongodb database and doing a mutation, I'm getting following error after running node index.js.

GraphQLError: Syntax Error: Cannot parse the unexpected character ";".

Here's my index.js....

const { GraphQLServer } = require('graphql-yoga');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/todo');

const Todo = mongoose.model('Todo',{
    text: String,
    complete: Boolean
});

const typeDefs = `
  type Query {
    hello(name: String): String!
  }

  type Todo{
    id: ID!
    text: String!
    complete: Boolean!
  }

  type Mutation{
    createTodo(text: String!): Todo;
  }
;

const resolvers = {
  Query: {
    hello: (_, { name }) => `Hello ${name || 'World'}`,
  },

  Mutation: {
    createTodo: async (_, { text }) => {
        const todo = new Todo({ text, complete: false });
        await todo.save();
        return todo;
    }
  }
};

const server = new GraphQLServer({ typeDefs, resolvers });
mongoose.connection.once('open', function() {
    server.start(() => console.log('Server is running on localhost:4000'))
  });

How to resolve this error!?

Answer

loelsonk picture loelsonk · Jun 14, 2018

Remove semicolon and add closing template string `

const typeDefs = `
  type Query {
    hello(name: String): String!
  }

  type Todo{
    id: ID!
    text: String!
    complete: Boolean!
  }

  type Mutation{
    createTodo(text: String!): Todo;
  }

; // <= remove
` // <= add