-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of File Upload feature. Using the graphql-multipart-request-spec
1 parent
dad37ac
commit 92c8d70
Showing
8 changed files
with
567 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.. _aiohttp_transport: | ||
|
||
AIOHTTPTransport | ||
================ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
File uploads | ||
============ | ||
|
||
GQL supports file uploads with the :ref:`aiohttp transport <aiohttp_transport>` | ||
using the `GraphQL multipart request spec`_. | ||
|
||
.. _GraphQL multipart request spec: https://github.com/jaydenseric/graphql-multipart-request-spec | ||
|
||
Single File | ||
----------- | ||
|
||
In order to upload a single file, you need to: | ||
|
||
* set the file as a variable value in the mutation | ||
* provide the opened file to the `variable_values` argument of `execute` | ||
* set the `upload_files` argument to True | ||
|
||
.. code-block:: python | ||
transport = AIOHTTPTransport(url='YOUR_URL') | ||
client = Client(transport=sample_transport) | ||
query = gql(''' | ||
mutation($file: Upload!) { | ||
singleUpload(file: $file) { | ||
id | ||
} | ||
} | ||
''') | ||
with open("YOUR_FILE_PATH", "rb") as f: | ||
params = {"file": f} | ||
result = client.execute( | ||
query, variable_values=params, upload_files=True | ||
) | ||
File list | ||
--------- | ||
|
||
It is also possible to upload multiple files using a list. | ||
|
||
.. code-block:: python | ||
transport = AIOHTTPTransport(url='YOUR_URL') | ||
client = Client(transport=sample_transport) | ||
query = gql(''' | ||
mutation($files: [Upload!]!) { | ||
multipleUpload(files: $files) { | ||
id | ||
} | ||
} | ||
''') | ||
f1 = open("YOUR_FILE_PATH_1", "rb") | ||
f2 = open("YOUR_FILE_PATH_1", "rb") | ||
params = {"files": [f1, f2]} | ||
result = client.execute( | ||
query, variable_values=params, upload_files=True | ||
) | ||
f1.close() | ||
f2.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ Usage | |
subscriptions | ||
variables | ||
headers | ||
file_upload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters