Closed
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