How to stream file upload using python gql client for GraphQL

How to stream file upload using python gql client for GraphQL


0

I am using python gql client to upload files using GraphQL request as below:
https://gql.readthedocs.io/en/latest/usage/file_upload.html

I am using asynchronous transport

  transport = AIOHTTPTransport(url='YOUR_URL')
  async with Client(transport=transport) as client:
    query = gql('''
      mutation singleUpload($file: Upload!) {
        singleUpload(file: $file) {
          id
        }
      }
    ''')
    
    async def file_sender(file_name):
        async with aiofiles.open(file_name, 'rb') as f:
            chunk = await f.read(64*1024)
                while chunk:
                    yield chunk
                    chunk = await f.read(64*1024)
    
    params = {"file": file_sender(file_name='YOUR_FILE_PATH')}        
    result = await client.execute(query, operation_name="singleUpload", variable_values=params, upload_files=True)

The above code gives error Exception: Failed to upload {'message': 'Must provide query string.'}.

However, the same query works when I use the params as below

with open(filepath, "rb") as f:
   params = {"file": f}


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *