-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to use graphene.List(Upload) to upload multiple files? #14
Comments
I've figured out why it does not work. I have the following:
When I try to execute this mutation with wrong |
Oops man, when solving the problem of the field "expires" the upload with multiple files is working well? I am evaluating this library. I wanted to know if it is stable considering that my application will use a lot of image uploads. |
Do we have a solution for uploading multiple files together ? |
I spend sometime on this and this is definitely working fine. I read https://medium.com/@dilipkumar/graphql-and-file-upload-using-react-and-node-js-c1d629e1b86b to understand the payload structure and just did what they asked me to do. In summary, we have to put the following keys and values in operations:{"query": "mutation uploadFiles($files: [Upload]) {uploadFiles(files: $files) { success }", "variables": { "files": [null,null] }}
map:{ "0": ["variables.files.0"], "1": ["variables.files.1"]} Additionally |
Here's how to create a test for Django, combining all the bits of information from above + some figuring out:
import random
import pytest
from django.core.files.uploadedfile import SimpleUploadedFile
def get_bytes(size: int) -> bytes:
return bytes(bytearray(random.getrandbits(8) for _ in range(size)))
UPLOAD_DOCUMENTS = """mutation uploadDocuments($documents: [Upload]!) {
uploadDocuments(documents: $documents) {
success
}
}"""
def test_upload():
# Note the keys: variablename dot index_number
documents = {
"documents.0": SimpleUploadedFile(
name="scanned contract.png",
content=get_bytes(1024),
content_type="image/png",
),
"documents.1": SimpleUploadedFile(
name="Invoice.pdf",
content=get_bytes(2048),
content_type="application/pdf",
),
}
response = file_graphql_query(
UPLOAD_DOCUMENTS, files=documents, variables={"documents": [None, None]}
)
assert response.json()["data"]["uploadDocuments"]["success"] is True
class uploadDocuments(graphene.Mutation):
success = graphene.Boolean()
class Arguments:
documents = graphene.NonNull(graphene.List(Upload))
@classmethod
def mutate(
cls,
root,
info: td.ResolveInfo,
documents: t.List[SimpleUploadedFile],
):
if isinstance(documents[0], SimpleUploadedFile) and isinstance(
documents[1], SimpleUploadedFile
):
return cls(success=True)
return cls(success=False) |
The question in title ^^.
The text was updated successfully, but these errors were encountered: