在 type-graphql 突变中将多个参数与 `GraphQLUpload` 一起传递

passing multiple arguments along with `GraphQLUpload` in type-graphql mutations

我正在使用 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?

我相信您将需要使用 variables 对象来传递其他输入,如@xadm 所述。使用 Postman,我测试了以下代码并且它有效:

{
  "query": "mutation userUploadPostPhoto($photo: Upload!, $id: String!) { userUploadPostPhoto(photo: $photo, id: $id) }",
  "variables": { "photo": null, "id": "10" }
}

您也可以删除 photo 属性,因为它不需要。

PS:请确保您已经安装了graphql-uploadapollo-server-express。另一个包(apollo-server)将不接受 express 作为中间件作为 applyMiddleware 方法的输入。

你可以试试Altair

我认为这比 postman 好你可以看到如何 Upload a file whit altair

您的 GraphQL 查询应如下所示

mutation ($Picture: Upload!)
{
  
  userUploadPostPhoto
  (
    id: 123123123,  
    photo: $Picture,
  )
}