Skip to content

Using GraphQLRequest instead of DocumentNode for gql, execute, subscribe methods #556

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced/batching_requests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To execute a batch of requests manually:

.. code-block:: python

request1 = GraphQLRequest("""
request1 = gql("""
query getContinents {
continents {
code
Expand Down
4 changes: 2 additions & 2 deletions docs/code_examples/appsync/mutation_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ async def main():
}"""
)

variable_values = {"message": "Hello world!"}
query.variable_values = {"message": "Hello world!"}

result = await session.execute(query, variable_values=variable_values)
result = await session.execute(query)
print(result)


Expand Down
4 changes: 2 additions & 2 deletions docs/code_examples/appsync/mutation_iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ async def main():
}"""
)

variable_values = {"message": "Hello world!"}
query.variable_values = {"message": "Hello world!"}

result = await session.execute(query, variable_values=variable_values)
result = await session.execute(query)
print(result)


Expand Down
6 changes: 2 additions & 4 deletions docs/code_examples/console_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ async def close(self):
await self._client.close_async()

async def get_continent_name(self, code):
params = {"code": code}
self.get_continent_name_query.variable_values = {"code": code}

assert self._session is not None

answer = await self._session.execute(
self.get_continent_name_query, variable_values=params
)
answer = await self._session.execute(self.get_continent_name_query)

return answer.get("continent").get("name") # type: ignore

Expand Down
5 changes: 2 additions & 3 deletions docs/code_examples/fastapi_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ async def get_continent(continent_code):

try:
assert isinstance(client.session, ReconnectingAsyncClientSession)
result = await client.session.execute(
query, variable_values={"code": continent_code}
)
query.variable_values = {"code": continent_code}
result = await client.session.execute(query)
except Exception as e:
log.debug(f"get_continent Error: {e}")
raise HTTPException(status_code=503, detail="GraphQL backend unavailable")
Expand Down
4 changes: 2 additions & 2 deletions docs/code_examples/reconnecting_mutation_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ async def main():
# Execute single query
query = gql("mutation ($message: String!) {sendMessage(message: $message)}")

params = {"message": f"test {num}"}
query.variable_values = {"message": f"test {num}"}

try:
result = await session.execute(query, variable_values=params)
result = await session.execute(query)
print(result)
except Exception as e:
print(f"Received exception {e}")
Expand Down
4 changes: 2 additions & 2 deletions docs/code_examples/reconnecting_mutation_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ async def main():
# Execute single query
query = gql("mutation ($message: String!) {sendMessage(message: $message)}")

params = {"message": f"test {num}"}
query.variable_values = {"message": f"test {num}"}

try:
result = await session.execute(query, variable_values=params)
result = await session.execute(query)
print(result)
except Exception as e:
print(f"Received exception {e}")
Expand Down
23 changes: 9 additions & 14 deletions docs/usage/custom_scalars_and_enums.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ In a variable

query = gql("query shift5days($time: Datetime) {shiftDays(time: $time, days: 5)}")

variable_values = {
query.variable_values = {
"time": "2021-11-12T11:58:13.461161",
}

result = client.execute(query, variable_values=variable_values)
result = client.execute(query)

- enum:

Expand All @@ -220,11 +220,11 @@ In a variable
}"""
)

variable_values = {
query.variable_values = {
"color": 'RED',
}

result = client.execute(query, variable_values=variable_values)
result = client.execute(query)

Automatically
^^^^^^^^^^^^^
Expand Down Expand Up @@ -256,12 +256,10 @@ Examples:
query = gql("query shift5days($time: Datetime) {shiftDays(time: $time, days: 5)}")

# the argument for time is a datetime instance
variable_values = {"time": datetime.now()}
query.variable_values = {"time": datetime.now()}

# we execute the query with serialize_variables set to True
result = await session.execute(
query, variable_values=variable_values, serialize_variables=True
)
result = await session.execute(query, serialize_variables=True)

- enums:

Expand All @@ -285,14 +283,12 @@ Examples:
)

# the argument for time is an instance of our Enum type
variable_values = {
query.variable_values = {
"color": Color.RED,
}

# we execute the query with serialize_variables set to True
result = client.execute(
query, variable_values=variable_values, serialize_variables=True
)
result = client.execute(query, serialize_variables=True)

Parsing output
--------------
Expand All @@ -319,11 +315,10 @@ Same example as above, with result parsing enabled:

query = gql("query shift5days($time: Datetime) {shiftDays(time: $time, days: 5)}")

variable_values = {"time": datetime.now()}
query.variable_values = {"time": datetime.now()}

result = await session.execute(
query,
variable_values=variable_values,
serialize_variables=True,
parse_result=True,
)
Expand Down
36 changes: 13 additions & 23 deletions docs/usage/file_upload.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ In order to upload a single file, you need to:

* set the file as a variable value in the mutation
* create a :class:`FileVar <gql.FileVar>` object with your file path
* provide the `FileVar` instance to the `variable_values` argument of `execute`
* provide the `FileVar` instance to the `variable_values` attribute of your query
* set the `upload_files` argument to True

.. code-block:: python
Expand All @@ -37,11 +37,9 @@ In order to upload a single file, you need to:
}
''')

params = {"file": FileVar("YOUR_FILE_PATH")}
query.variable_values = {"file": FileVar("YOUR_FILE_PATH")}

result = client.execute(
query, variable_values=params, upload_files=True
)
result = client.execute(query, upload_files=True)

Setting the content-type
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -97,11 +95,9 @@ It is also possible to upload multiple files using a list.
f1 = FileVar("YOUR_FILE_PATH_1")
f2 = FileVar("YOUR_FILE_PATH_2")

params = {"files": [f1, f2]}
query.variable_values = {"files": [f1, f2]}

result = client.execute(
query, variable_values=params, upload_files=True
)
result = client.execute(query, upload_files=True)


Streaming
Expand Down Expand Up @@ -150,11 +146,9 @@ setting the `streaming` argument of :class:`FileVar <gql.FileVar>` to `True`
streaming=True,
)

params = {"file": f1}
query.variable_values = {"file": f1}

result = client.execute(
query, variable_values=params, upload_files=True
)
result = client.execute(query, upload_files=True)

Another option is to use an async generator to provide parts of the file.

Expand All @@ -172,11 +166,9 @@ to read the files in chunks and create this asynchronous generator.
yield chunk

f1 = FileVar(file_sender(file_name='YOUR_FILE_PATH'))
params = {"file": f1}
query.variable_values = {"file": f1}

result = client.execute(
query, variable_values=params, upload_files=True
)
result = client.execute(query, upload_files=True)

Streaming downloaded files
^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -193,7 +185,7 @@ In order to do that, you need to:

* get the response from an aiohttp request and then get the StreamReader instance
from `resp.content`
* provide the StreamReader instance to the `variable_values` argument of `execute`
* provide the StreamReader instance to the `variable_values` attribute of your query

Example:

Expand All @@ -204,7 +196,7 @@ Example:
async with http_client.get('YOUR_DOWNLOAD_URL') as resp:

# We now have a StreamReader instance in resp.content
# and we provide it to the variable_values argument of execute
# and we provide it to the variable_values attribute of the query

transport = AIOHTTPTransport(url='YOUR_GRAPHQL_URL')

Expand All @@ -218,8 +210,6 @@ Example:
}
''')

params = {"file": FileVar(resp.content)}
query.variable_values = {"file": FileVar(resp.content)}

result = client.execute(
query, variable_values=params, upload_files=True
)
result = client.execute(query, upload_files=True)
10 changes: 5 additions & 5 deletions docs/usage/variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Using variables
===============

It is possible to provide variable values with your query by providing a Dict to
the variable_values argument of the `execute` or the `subscribe` methods.
the variable_values attribute of your query.

The variable values will be sent alongside the query in the transport message
(there is no local substitution).
Expand All @@ -19,14 +19,14 @@ The variable values will be sent alongside the query in the transport message
"""
)

params = {"code": "EU"}
query.variable_values = {"code": "EU"}

# Get name of continent with code "EU"
result = client.execute(query, variable_values=params)
result = client.execute(query)
print(result)

params = {"code": "AF"}
query.variable_values = {"code": "AF"}

# Get name of continent with code "AF"
result = client.execute(query, variable_values=params)
result = client.execute(query)
print(result)
Loading
Loading