【发布时间】:2021-11-10 08:43:21
【问题描述】:
我正在使用 type-graphql 实现上传照片的突变,我还想获得另一个参数,例如和 ID:
@Mutation(() => GraphQLJSON)
async userUploadPostPhoto(
@Arg("photo", () => GraphQLUpload) photo: Upload,
@Arg("id") id: string,
@Ctx() context: TContext
) {
return userUploadPostPhoto(photo, context);
}
这是我试图在邮递员中测试 API 的方法:
我从 API 得到的响应是这个 json 文件:
{
"errors": [
{
"message": "Variable \"$id\" got invalid value { resolve: [function], reject: [function], promise: {} }; String cannot represent a non string value: { resolve: [function], reject: [function], promise: {} }",
"locations": [
{
"line": 1,
"column": 47
}
],
"extensions": {
"code": "BAD_USER_INPUT",
"exception": {
"stacktrace": [
"GraphQLError: Variable \"$id\" got invalid value { resolve: [function], reject: [function], promise: {} }; String cannot represent a non string value: { resolve: [function], reject: [function], promise: {} }",
" at /home/ebaqeri/Projects/univarsity/server/node_modules/graphql/execution/values.js:116:15",
" at coerceInputValueImpl (/home/ebaqeri/Projects/univarsity/server/node_modules/graphql/utilities/coerceInputValue.js:131:9)",
" at coerceInputValueImpl (/home/ebaqeri/Projects/univarsity/server/node_modules/graphql/utilities/coerceInputValue.js:54:14)",
" at coerceInputValue (/home/ebaqeri/Projects/univarsity/server/node_modules/graphql/utilities/coerceInputValue.js:37:10)",
" at _loop (/home/ebaqeri/Projects/univarsity/server/node_modules/graphql/execution/values.js:109:69)",
" at coerceVariableValues (/home/ebaqeri/Projects/univarsity/server/node_modules/graphql/execution/values.js:121:16)",
" at getVariableValues (/home/ebaqeri/Projects/univarsity/server/node_modules/graphql/execution/values.js:50:19)",
" at buildExecutionContext (/home/ebaqeri/Projects/univarsity/server/node_modules/graphql/execution/execute.js:203:61)",
" at executeImpl (/home/ebaqeri/Projects/univarsity/server/node_modules/graphql/execution/execute.js:101:20)",
" at Object.execute (/home/ebaqeri/Projects/univarsity/server/node_modules/graphql/execution/execute.js:60:35)",
" at execute (/home/ebaqeri/Projects/univarsity/server/node_modules/apollo-server-core/src/requestPipeline.ts:480:20)",
" at Object.processGraphQLRequest (/home/ebaqeri/Projects/univarsity/server/node_modules/apollo-server-core/src/requestPipeline.ts:375:28)",
" at processTicksAndRejections (node:internal/process/task_queues:96:5)",
" at async processHTTPRequest (/home/ebaqeri/Projects/univarsity/server/node_modules/apollo-server-core/src/runHttpQuery.ts:331:24)"
]
}
}
}
]
}
`
Anyone has any Idea on this? Is this the problem from the API side or the problem is the method that I'm trying to fix it?
【问题讨论】:
-
恕我直言
map是/应该仅用于文件...id应该通过variables传递,第二个对象编码到操作中 -
你能给我举个例子吗?
-
github.com/jaydenseric/graphql-multipart-request-spec - 你应该已经编码了一个变量对象[文件 arg 为空] -
operations应该是query和variables的数组 ... 像往常一样在突变 [或带参数的查询] -
typescript @Mutation(() => GraphQLUpload) async userUploadPostPhoto( @Arg("photo", () => GraphQLUpload) photo: Upload, @Arg("id") id: string, @Ctx() context: TContext ) { return userUploadPostPhoto(photo, id, context); }这是我已经实现的突变,这是我在表单数据方法json {"0": ["variables.photo"], "1": ["variables.id"]}中传递给突变的映射对象我想我正在做同样的事情在您附加的链接中提到
标签: graphql apollo-server graphql-js express-graphql typegraphql