graphql - 在 type-graphql 突变中传递多个参数以及 `GraphQLUpload`

标签 graphql apollo-server graphql-js express-graphql typegraphql

我正在实现一个使用 type-graphql 上传照片的突变,并且我还想获​​得另一个参数,例如 和 ID:

  @Mutation(() => GraphQLJSON)
  async userUploadPostPhoto(
    @Arg("photo", () => GraphQLUpload) photo: Upload,
    @Arg("id") id: string,
    @Ctx() context: TContext
  ) {
    return userUploadPostPhoto(photo, context);
  }

这是我尝试在 postman 中测试 API 的方法:

Screenshot from 2021-09-15 13-10-50

我从 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?

最佳答案

您可以尝试使用Altair

我认为这比 postman 更好,您可以在这里查看如何 Upload a file whit altair 的示例。

您的 GraphQL 查询应如下所示

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

关于graphql - 在 type-graphql 突变中传递多个参数以及 `GraphQLUpload`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69190077/

相关文章:

sql - 如何以及何时为 graphql 生成 ID?

websocket - 使用来自 apollo-link-ws 的 WebSocketLink 检查互联网连接

javascript - `{where: ' 原始查询'}` 的支持已被删除

javascript - 使用 GraphQL 查询从多语言模式获取本地化数据

graphql - 在 Apollo Server 中调用另一个解析器的最佳方法是什么?

react-apollo - 使用 Postman 在 AWS 中使用 GraphQL 测试服务(AppSync、Apollo)

graphql - GraphQLScalarType 中的 parseValue 和 parseLiteral 有什么区别

scala - 如何创建一个可以在不同字段上搜索的graphql schema?

reactjs - relay 或 Apollo-react 如何解决涉及多重关系的突变

apollo - 设置带有subscriptions-transport-ws的Apollo服务器?