gqlgen
gqlgen copied to clipboard
assignment to entry in nil map when uploading a file
What happened?
If the operation field is incorrect during the file upload, a panic is triggered. assignment to entry in nil map
The panic occurs in https://github.com/99designs/gqlgen/blob/master/graphql/handler.go#L115
Request:
curl --location --request POST 'localhost/graphql' \
--form 'map="{\"1\": [\"variables.file\"]}"' \
--form '1=@"/Users/user_name/image_2021-03-17_14-22-00.png"' \
--form 'operations="{\"query\": \"mutation($file: Upload!) {imageLibraryUpload(input: {name: \\\"image_2021-03-17_14-22-00.png\\\",src: $file}){id}}\"}"'
If you add a variable object to the operations, everything will be successful.
curl --location --request POST 'localhost/graphql' \
--form 'map="{\"1\": [\"variables.file\"]}"' \
--form '1=@"/Users/user_name/image_2021-03-17_14-22-00.png"' \
--form 'operations="{\"variables\": {\"file\": null}, \"query\": \"mutation($file: Upload!) {imageLibraryUpload(input: {name: \\\"image_2021-03-17_14-22-00.png\\\",src: $file}){id}}\"}"'
What did you expect?
Correct error
Minimal graphql.schema and models to reproduce
type Mutation {
imageLibraryUpload(input: imageLibraryUploadInput!): UploadedImage!
}
type UploadedImage {
id: ID!
srcUrl: String!
}
input imageLibraryUploadInput {
name: String!
src: Upload!
}
versions
-
gqlgen version
?v0.9.3
-
go version
?go1.15.4 darwin/amd64
- dep or go modules?
go modules
Strange that you just created this ticket today, because I just encountered this issue today as well.
Any fix for this?
@salyakhov-art Any fix for this issue?
Same error
-
gqlgen v0.17.13
-
go version: go1.19.1 darwin/arm64
-
go modules
For my side, I check the code and find the params decode from the input, so I think need to declare like follow. https://github.com/99designs/gqlgen/blob/master/graphql/handler/transport/http_form.go#L81-L86
❌ {"query": "mutation uploadImage($image:Upload!) {uploadImage(image: $image){URL}}"} ✅ {"query": "mutation uploadImage($image:Upload!) {uploadImage(image: $image){URL}}", "variables": {"image": null}}
# https://gqlgen.com/getting-started/
scalar Upload
type Image {
URL: String!
}
extend type Mutation {
uploadImage(image: Upload!): Image
}