jaydenseric / graphql-upload

Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 131 forks source link

Error: "Upload literal unsupported" when define GraphQLUpload as Type #269

Closed nullRefErr closed 2 years ago

nullRefErr commented 2 years ago

I'm using nestjs framework with graphql and got stuck when uploading a file.

This is the example

import { GraphQLUpload, FileUpload } from 'graphql-upload';
  @Mutation(() => Boolean)
  async docUpload(
    @Args('file', { type: () => GraphQLUpload })
    file: FileUpload,
  ) {
    const { filename, createReadStream } = file;
    console.log(file, filename, createReadStream);
    return true;
  }

This is the error

{
    "errors": [
        {
            "message": "Upload literal unsupported.",
            "locations": [
                {
                    "line": 2,
                    "column": 19
                }
            ],
            "extensions": {
                "code": "GRAPHQL_VALIDATION_FAILED",
                "exception": {
                    "stacktrace": [
                        "GraphQLError: Upload literal unsupported.",
                        "    at GraphQLScalarType.parseLiteral (/usr/src/app/node_modules/graphql-upload/public/GraphQLUpload.js:85:11)",
                        "    at isValidValueNode (/usr/src/app/node_modules/graphql/validation/rules/ValuesOfCorrectTypeRule.js:125:28)",
                        "    at Object.EnumValue (/usr/src/app/node_modules/graphql/validation/rules/ValuesOfCorrectTypeRule.js:84:14)",
                        "    at Object.enter (/usr/src/app/node_modules/graphql/language/visitor.js:323:29)",
                        "    at Object.enter (/usr/src/app/node_modules/graphql/utilities/TypeInfo.js:370:25)",
                        "    at visit (/usr/src/app/node_modules/graphql/language/visitor.js:243:26)",
                        "    at validate (/usr/src/app/node_modules/graphql/validation/validate.js:69:24)",
                        "    at validate (/usr/src/app/node_modules/apollo-server-core/dist/requestPipeline.js:185:39)",
                        "    at processGraphQLRequest (/usr/src/app/node_modules/apollo-server-core/dist/requestPipeline.js:90:34)",
                        "    at processTicksAndRejections (internal/process/task_queues.js:95:5)"
                    ]
                }
            }
        }
    ]
}
jaydenseric commented 2 years ago

What if the error is correct? Are you incorrectly putting a literal value where an Upload scalar type is expected in your query?

nullRefErr commented 2 years ago

Hmm, I'm not an expert in GraphQL, can you be more specific ? I was using firecamp to send request. I can share bit more if you want.

Found this in generated schema.graphql

"""The `Upload` scalar type represents a file upload."""
scalar Upload

Thanks a lot.

Edit: How can I find that I put literal value ?

Edit: I think I found the solution.

I was sending request like below from firecamp.


mutation Mutation {
  uploadFile(upload: file)
}

Then I changed to this.


mutation Mutation($upload:Upload!) {
  uploadFile(upload: $upload)
}

Now I can see the file name in the mutation function.

nullRefErr commented 2 years ago

Solved.

UTC1992 commented 2 years ago

I have the same problem with this error, help me please with more information

"errors": [
    {
        "message": "Upload serialization unsupported.",
        "locations": [
            {
                "line": 2,
                "column": 3
            }
        ],
        "path": [
            "singleUpload"
        ],
        "extensions": {
            "code": "INTERNAL_SERVER_ERROR",
            "exception": {
                "message": "Upload serialization unsupported.",
                "stacktrace": [
                    "GraphQLError: Upload serialization unsupported.",
                    "    at GraphQLScalarType.serialize (/Users/omar-imac/Desktop/DesarrolloProyectos/1.-1Mentor/pilot-backend/node_modules/graphql-upload/public/GraphQLUpload.js:88:11)",
                    "    at /Users/omar-imac/Desktop/DesarrolloProyectos/1.-1Mentor/pilot-backend/node_modules/@graphql-tools/delegate/dist/utils/src/transformInputValue.js:27:57",
                    "    at transformInputValue (/Users/omar-imac/Desktop/DesarrolloProyectos/1.-1Mentor/pilot-backend/node_modules/@graphql-tools/delegate/dist/utils/src/transformInputValue.js:8:52)",
                    "    at Object.serializeInputValue (/Users/omar-imac/Desktop/DesarrolloProyectos/1.-1Mentor/pilot-backend/node_modules/@graphql-tools/delegate/dist/utils/src/transformInputValue.js:27:12)",
                    "    at /Users/omar-imac/Desktop/DesarrolloProyectos/1.-1Mentor/pilot-backend/dist/delegate/src/createRequest.js:59:37",
                    "    at Array.forEach (<anonymous>)",
                    "    at createRequest (/Users/omar-imac/Desktop/DesarrolloProyectos/1.-1Mentor/pilot-backend/dist/delegate/src/createRequest.js:55:29)",
                    "    at createRequestFromInfo (/Users/omar-imac/Desktop/DesarrolloProyectos/1.-1Mentor/pilot-backend/dist/delegate/src/createRequest.js:13:12)",
                    "    at Object.delegateToSchema (/Users/omar-imac/Desktop/DesarrolloProyectos/1.-1Mentor/pilot-backend/dist/delegate/src/delegateToSchema.js:12:21)",
                    "    at /Users/omar-imac/Desktop/DesarrolloProyectos/1.-1Mentor/pilot-backend/dist/wrap/src/generateProxyingResolvers.js:64:47"
                ]
            }
        }
    }
],