我正在尝试将数据写入我的GQL模式,以便能够查询它。我有json的数据。这是我的第一个GQL项目,关于突变的文档让我很困惑。一切正常,我只需查询即可,但当我添加突变以插入数据时,一切都会中断。任何帮助都是非常感谢的。
下面是我的模式的代码:
const typeDefs = gql`
type MastodonStatus {
status_id: String!
user_id: String!
user_url: String
acct_name: String!
disp_name: String
status_content: String!
status_url: String
}
# queries
type Query {
getAllStatus: [MastodonStatus!]
}
type Mutation {
insertStatus(status_id: String!, user_id: String!, user_url: String, acct_name: String!, disp_name: String, status_content: String!, status_url: String)
}
`;
const resolvers = {
Query: {
getAllStatus() {
return;
}
},
Mutation: {
insertStatus: (parent, args) => {
return {status_id:args.status_id,
user_id:args.user_id,
user_url:args.user_url,
acct_name:args.acct_name,
disp_name:args.disp_name,
status_content:args.status_content,
status_url:args.status_url
}
}
}
}
这将引发以下错误:
./node_modules/graphql/language/parser.js:1397
throw (0, _syntaxError.syntaxError)(
^
GraphQLError: Syntax Error: Expected ":", found "}".
at syntaxError (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/error/syntaxError.js:15:10)
at Parser.expectToken (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:1397:40)
at Parser.parseFieldDefinition (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:838:10)
at Parser.optionalMany (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:1492:28)
at Parser.parseFieldsDefinition (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:822:17)
at Parser.parseObjectTypeDefinition (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:794:25)
at Parser.parseDefinition (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:172:23)
at Parser.many (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:1511:26)
at Parser.parseDocument (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:122:25)
at Object.parse (/Users/zach/code/csc557_web/grad_project/node_modules/graphql/language/parser.js:32:17) {
path: undefined,
locations: [ { line: 21, column: 4 } ],
extensions: [Object: null prototype] {}
}
我在Node 18.11,这是我的package.json
{
"dependencies": {
"apollo-server": "^3.11.1",
"express": "^4.18.2",
"graphql": "^16.6.0",
"mysql": "^2.18.1"
}
}
发布于 2022-11-14 10:37:56
你需要从你的突变中返回一个类型。解析器期望的内容如下:
insertStatus(status_id: String!, …remaining args): SomeType
在您的情况下,由于要插入MastodonStatus
对象,我建议返回您插入的内容:
insertStatus(status_id: String!, …remaining args): MastodonStatus
https://stackoverflow.com/questions/74436172
复制相似问题