我的GraphQL API出现语法错误,我不知道它为什么发生。错误日志没有显示此语法错误的位置。我浏览了所有模式,它们都有右括号。
C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\error\syntaxError.js:24
return new _GraphQLError.GraphQLError("Syntax Error: ".concat(description), undefined, source, [position]);
^
Syntax Error: Expected Name, found }
GraphQL request (53:1)
52:
53: }
^
54:
at syntaxError (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\error\syntaxError.js:24:10)
at expect (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:1463:32)
at parseName (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:96:15)
at parseInputValueDef (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:920:14)
at many (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:1520:16)
at parseInputFieldsDefinition (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:1083:50)
at parseInputObjectTypeDefinition (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:1067:16)
at parseTypeSystemDefinition (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:736:16)
at parseDefinition (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:142:16)
at many (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:1523:16)
at parseDocument (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:113:18)
at Object.parse (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql\language\parser.js:48:10)
at Object.buildSchemaFromTypeDefinitions (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql-tools\src\generate\buildSchemaFromTypeDefinitions.ts:37:19)
at Object.makeExecutableSchema (C:\Users\Administrator\Desktop\PayMe\api\node_modules\graphql-tools\src\makeExecutableSchema.ts:52:18)
at new ApolloServerBase (C:\Users\Administrator\Desktop\PayMe\api\node_modules\apollo-server-core\src\ApolloServer.ts:195:21)
at new ApolloServer (C:\Users\Administrator\Desktop\PayMe\api\node_modules\apollo-server-express\src\ApolloServer.ts:68:1)
at Object.<anonymous> (C:\Users\Administrator\Desktop\PayMe\api\src\app.ts:31:16)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Module.m._compile (C:\Users\Administrator\Desktop\PayMe\api\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\Administrator\Desktop\PayMe\api\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! type@1.0.0 start: `ts-node ./src/app.ts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the type@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2019-07-04T21_23_11_333Z-debug.log
用户架构:
type User {
id: Int!
email: String!
password: String!
firstName: String
lastName: String
avatar: String
accessToken: String
itemID: String
jwt: String @isAuthUser
groups: [Group]
}
input UserInput {
email: String
password: String
firstName: String
lastName: String
avatar: String
accessToken: String
itemID: String
}
type Query {
getUser: User @isAuth
loginUser(email: String!, password: String!): User
logoutUser(email: String!): Boolean
}
type Mutation {
createUser(data: UserInput): User
updateAccessToken(accessToken: String): User @isAuth
}
组架构:
type Group {
groupID: Int!
name: String!
description: String
avatar: String
}
input GroupInput {
name: String
description: String
avatar: String
userID: String
}
type Query {
groups: [Group]
getGroup(groupID: Int!): Group @isAuth
getUserGroups(userID: Int!): [Group] @isAuth
}
type Mutation {
createGroup(data: GroupInput): Group @isAuth
addMemberToGroup(userID: Int!, groupID: Int!): Group @isAuth
}
MemberOf模式:
type MemberOf {
id: Int!
groupID: Int
userID: Int
isOwner: boolean
isAdmin: boolean
}
type Query {
}
type Mutation {
}
我不确定这些模式的编译顺序。在尝试在组和用户之间建立关系之前,该模式用于编译和运行。我已将@BelongsToMany添加到User and Group,并将@ForiegnKey添加到MemberOf。
答案 0 :(得分:0)
在GraphQL文档中,括号用于三个目的。在可执行文档中,它们标识选择集或所请求的一组字段。在类型定义中,它们要么标识一组字段(用于对象类型,输入对象类型或接口),要么标识一组枚举的值。 在所有三种情况下,提供的集合都不能为空。也就是说,如果使用大括号,则必须在大括号内提供至少一项。
一对空的大括号({}
)将始终提供语法错误。
如果您不需要提供选择集/字段集/枚举值集,只需省略大括号即可:
# Invalid
type Query {
}
# Valid (at least syntactically)
type Query