Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQLError: Syntax Error: Cannot parse the unexpected character ";" [closed]

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!?

like image 369
user8817816 Avatar asked Oct 20 '25 20:10

user8817816


1 Answers

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
like image 151
loelsonk Avatar answered Oct 22 '25 11:10

loelsonk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!