makeExecutableSchema的语法错误:意外的名称“用户”(GraphQL)

时间:2018-09-30 15:01:29

标签: node.js graphql

我需要一些帮助来解决graphql上的错误,在这种情况下我什么都找不到。

我试图将我的不同架构和解析器分开以使其易于维护,但是当我使用makeExecutableSchema函数(来自graphql-tools程序包)时,抛出“ GraphQLError:语法错误:意外的名称“ User””错误。

这是错误:

/Users/thomasblanquet/EIP_Routs/API/routs-api/node_modules/graphql/language/parser.js:153
  throw unexpected(lexer);
  ^
GraphQLError: Syntax Error: Unexpected Name "User"
    at syntaxError (/Users/thomasblanquet/EIP_Routs/API/routs-api/node_modules/graphql/error/syntaxError.js:24:10)
    at unexpected (/Users/thomasblanquet/EIP_Routs/API/routs-api/node_modules/graphql/language/parser.js:1490:33)
    at parseDefinition (/Users/thomasblanquet/EIP_Routs/API/routs-api/node_modules/graphql/language/parser.js:153:9)
    at many (/Users/thomasblanquet/EIP_Routs/API/routs-api/node_modules/graphql/language/parser.js:1523:16)
    at parseDocument (/Users/thomasblanquet/EIP_Routs/API/routs-api/node_modules/graphql/language/parser.js:113:18)
    at Object.parse (/Users/thomasblanquet/EIP_Routs/API/routs-api/node_modules/graphql/language/parser.js:48:10)
    at Object.buildSchemaFromTypeDefinitions (/Users/thomasblanquet/EIP_Routs/API/routs-api/node_modules/graphql-tools/dist/generate/buildSchemaFromTypeDefinitions.js:19:33)
    at makeExecutableSchema (/Users/thomasblanquet/EIP_Routs/API/routs-api/node_modules/graphql-tools/dist/makeExecutableSchema.js:26:29)
    at Object.<anonymous> (/Users/thomasblanquet/EIP_Routs/API/routs-api/graphql/schema.js:15:18)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! routs-api@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the routs-api@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

这是我的代码:

对于用户架构:

const User = `
User {
  _id: Int!
  username: String!
  email: String!
}

extend type Query {
    users: [User]
}
`;

module.exports = User;

对于解析器(该模型是使用sequelize创建的,而我正在使用Apollo-Server-Express):

const User = require('../../server/models/user')
const Messages = require('../messages');

const resolvers = {
  Query: {
    users(parent, args, context, info) {
      parent.getUsers()
    },
};

module.exports = resolvers;

最后是将架构和解析器组合在一起的代码:

const { makeExecutableSchema } = require('graphql-tools');
const resolvers = require('./resolver');
const User = require('./user/schema');

const Query =
`type Query {
  _empty: String
}`;

const Mutation =
`type Mutation {
  _empty: String
}`;

module.exports = makeExecutableSchema({
  typeDefs: [Query, Mutation, User],
  resolvers,
});

对于这种问题,我真的找不到任何东西,谢谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

您看到此错误,是因为您在用户类型定义中未使用type关键字。应该是:

type User {
  _id: Int!
  username: String!
  email: String!
}

还请注意,您的解析器正在根对象上调用getUsers,这也可能会崩溃(我想您打算使用模型来代替)。