2

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

如何解决这个错误!?

4

1 回答 1

4

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

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

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

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

; // <= remove
` // <= add
于 2018-06-14T09:31:23.560 回答