jaydenseric / graphql-multipart-request-spec

A spec for GraphQL multipart form requests (file uploads).
993 stars 54 forks source link

Is it possible to not use variables? #32

Closed FluorescentHallucinogen closed 4 years ago

FluorescentHallucinogen commented 4 years ago

All examples in the spec use variables. Is it possible to upload file(s) without using variables (pass file(s) directly as mutation argument value)?

mike-marcacci commented 4 years ago

Hi @FluorescentHallucinogen, it is not, as the spec provides a mechanism for mapping GraphQL variables to multipart fields.

Out of curiosity since you're asking, if this were to be possible, how would you want to specify the file's contents?

onzag commented 4 years ago

I can understand the reason why he is asking it, and I can understand why it works the way it does (as for some reasons I have to implement a client for this by hand); I think something like:

`--------------------------ec62457de6331cad Content-Disposition: form-data; name="operations"

{ "query": "mutation { multipleUpload(files: $files) { id } }" } --------------------------ec62457de6331cad Content-Disposition: form-data; name="map"

{ "files": ["0", "1"] } --------------------------ec62457de6331cad Content-Disposition: form-data; name="0"; filename="b.txt" Content-Type: text/plain

Bravo file content.

--------------------------ec62457de6331cad Content-Disposition: form-data; name="1"; filename="c.txt" Content-Type: text/plain

Charlie file content.

--------------------------ec62457de6331cad--`

This is how I would envision it, but I can truly understand why it would be more of a headache to implement it this way rather than using variables; as there's no way to map the same way, it could break the graphql schema if we specify a wrong type, it need to run async, it would be a headache to implement, so the variables solution is optimal and ensures a better check.

The spec is fine overall, while this might look a lot cleaner, I can see the problems, it breaks the standard and it just gives possible issues :) so it's just fine the way the spec has been written.

jaydenseric commented 4 years ago

In the introduction of the spec:

It’s possible to implement:

  • Nesting files anywhere within operations (usually in variables).

Files in operation variables is the most common use case, but you can have files anywhere in the operations.

@FluorescentHallucinogen I think this is not exactly your question tho, as you seem to be getting at placing files directly in the query itself? This is impossible, as file upload streams can't be represented as strings.