Repository navigation
Closed
Labels
type: question or discussionIssue discussing or asking a question about gql
Description
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
import aiofiles
gql_query = gql('''
mutation create($input: ProductDocumentCreateMutationInput) {
createProductDocument(input: $input) {
productDocument{
id
}
}
}
query getProductDoc($id: ID!) {
productDocument(id: $id) {
id
file_category
file
}
}
''')
async def stream_file(filepath):
async with aiofiles.open(filepath, "rb") as f:
while True:
chunk = await f.read(64 * 1024)
if not chunk:
break
yield chunk
transport = AIOHTTPTransport(
url='url',
headers={"Authorization": f"Bearer {auth_token}"},
)
async with Client(transport=transport, fetch_schema_from_transport=True) as gql_session
filepath = 'path/to/product_doc.pdf'
data = {
"user": 'user_id',
"file_category": 'product_doc'
"file": stream_file(filepath),
}
result = await gql_session.execute(
gql_query,
operation_name="create",
variable_values={"input": data},
upload_files=True,
)
In above I am trying to stream file upload as shown in the gql documentation. But I get following error
[ERROR] Exception: Failed to upload {'message': 'Must provide query string.'}
Traceback (most recent call last):
File "file_upload.py", line 143, in main
result = await gql_session.execute(
File "/usr/local/lib/python3.10/site-packages/gql/client.py", line 1231, in execute
raise TransportQueryError(
gql.transport.exceptions.TransportQueryError: {'message': 'Must provide query string.'}
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "file_upload.py", line 148, in main
raise Exception(f"Failed to upload {exp}")
Exception: Failed to upload {'message': 'Must provide query string.'}
System info:
- OS: Ubuntu 22.04
- Python version: 3.10
- gql version: 3.4
- graphql-core version: 3.2.3
Metadata
Metadata
Assignees
Labels
type: question or discussionIssue discussing or asking a question about gql
Activity
leszekhanusz commentedon Sep 7, 2023
createmutation inside the gql method callmyscfwork commentedon Sep 8, 2023
I have enabled bebug logs. Expanding the input argument does not work because create mutation only accepts input as ProductDocumentCreateMutationInput. I have shared the schema below. Also file upload works when only
createmutation is used inside gql method without streaming. But it fails when multiple queries are used inside the gql call or when streaming is used.So I tried following 3 things:
Relevant Schema:
leszekhanusz commentedon Sep 8, 2023
It should always be possible to expand input types into GraphQL basic types and scalars.
I don't think it's going to solve your problem but you should be able to use a query like this:
with the
variable_valuescontaining now directly the variables instead of the singleinputvariable.That being said, your problem seems quite strange.
myscfwork commentedon Sep 12, 2023
Could you share a working code for streaming file upload if you have?
aiohttphas the possibility to use FormData() and set filename for the uploaded file:https://docs.aiohttp.org/en/stable/client_quickstart.html#post-a-multipart-encoded-file
With
gql, following format works but the filename is set as the whole file path i.e/home/username/.... Is it possible to set the filename in the following request?leszekhanusz commentedon Sep 12, 2023
You can find some examples in the tests/test_aiohttp.py file. Search for
upload_filesto find the relevant tests.You can run specific tests by running a pytest command like this:
pytest tests/test_aiohttp.py::test_aiohttp_file_upload -sCheck out the test_aiohttp_async_generator_upload test.
That is what we are doing. gql is open-source you know, you can check the code.
gql uses the
nameparameter of the provided file object if it is present for thefilenameparameter.I thought you could do something like:
but in that case I got the error:
AttributeError: attribute 'name' of '_io.BufferedReader' objects is not writableSo one way to change the filename would be what you did above, even if it's a bit inefficient:
In the case of the streaming uploads, we have the same kind of problem.
Doing something like this:
would generate the following error:
AttributeError: 'async_generator' object has no attribute 'name'We can get around by making a new class inheriting
AsyncGeneratorthis but it's kind of hackish:that you would use like this:
I agree that's it's not really clean and we should consider changing the interface to make this simpler.
myscfwork commentedon Sep 18, 2023
@leszekhanusz FYI, your workaround gives error
gql.transport.exceptions.TransportQueryError: {'message': 'Must provide query string.'}.Whenever I add the generator
"file": named_async_generatorin the input parameter, it gives this error. But it works when I use it like this"file": io.BytesIO(open(filepath, "rb").read()). Unfortunately I upload large files and need to use the generator which is not working.leszekhanusz commentedon Sep 18, 2023
That error message is coming from the backend.
If you still think there is a problem with gql, either provide a public backend showing the issue, or add another test to gql showing the issue.
RainerHausdorf commentedon Apr 17, 2025
I wanted to open an issue regarding the filename but than I found this discussion. You already pointed to the line where you could fix it by removing the explicit setting of
filename=name. Without this parameter theadd_fieldmethod will callguess_filenameand this will be the name of the file instead of the full path. Wouldn't this be an easy quickwin?leszekhanusz commentedon May 22, 2025
@RainerHausdorf It's a bit off-topic in this issue but you should check the PR #549 which should set the
filenameattribute by default toNoneforadd_fieldwith the new implementation.