GraphQLError:语法错误:无法解析意外字符";"

时间:2018-06-14 08:33:29

标签: javascript graphql express-graphql

我是GraphQL的初学者。在设置服务器,连接到mongodb数据库并进行变异时,我在运行node index.js后收到了以下错误。

  

GraphQLError:语法错误:无法解析意外字符";"。

这是我的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'))
  });

如何解决此错误!?

1 个答案:

答案 0 :(得分:1)

删除分号并添加结束模板字符串`

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

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

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

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