-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ Usage Examples | |
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
examples/list_of_objects | ||
examples/list_of_objects | ||
examples/with_gql |
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,86 @@ | ||
With gql | ||
======== | ||
|
||
It is the simple example for usage of **graphql-query** with `gql 3 <https://gql.readthedocs.io/en/stable/index.html>`_. | ||
How to install **gql 3** you can see `here <https://gql.readthedocs.io/en/stable/intro.html#installation>`_. | ||
|
||
Basic Usage | ||
----------- | ||
|
||
For the Basic usage of **gql 3** with **graphql-query** you can running the following code | ||
|
||
.. code-block:: python | ||
from gql import Client, gql | ||
from gql.transport.aiohttp import AIOHTTPTransport | ||
from graphql_query import Operation, Query | ||
# Select your transport with a defined url endpoint | ||
transport = AIOHTTPTransport(url="https://countries.trevorblades.com/") | ||
# Create a GraphQL client using the defined transport | ||
client = Client(transport=transport, fetch_schema_from_transport=True) | ||
# generating of GraphQL query string with graphql_query | ||
getContinents = Operation( | ||
type="query", | ||
name="getContinents", | ||
queries=[Query(name="continents", fields=["code", "name"])] | ||
) | ||
# Provide a GraphQL query | ||
query = gql(getContinents.render()) | ||
# Execute the query on the transport | ||
result = client.execute(query) | ||
print(result) | ||
# {'continents': [{'code': 'AF', 'name': 'Africa'}, {'code': 'AN', 'name': 'Antarctica'}, {'code': 'AS', 'name': 'Asia'}, {'code': 'EU', 'name': 'Europe'}, {'code': 'NA', 'name': 'North America'}, {'code': 'OC', 'name': 'Oceania'}, {'code': 'SA', 'name': 'South America'}]} | ||
Using variables | ||
--------------- | ||
|
||
GraphQL variables it is easy | ||
|
||
.. code-block:: python | ||
from gql import Client, gql | ||
from gql.transport.aiohttp import AIOHTTPTransport | ||
from graphql_query import Argument, Operation, Query, Variable | ||
# Select your transport with a defined url endpoint | ||
transport = AIOHTTPTransport(url="https://countries.trevorblades.com/") | ||
# Create a GraphQL client using the defined transport | ||
client = Client(transport=transport, fetch_schema_from_transport=True) | ||
# generating of GraphQL query string with graphql_query | ||
code = Variable(name="code", type="ID!") | ||
getContinentName = Operation( | ||
type="query", | ||
name="getContinentName", | ||
variables=[code], | ||
queries=[ | ||
Query( | ||
name="continent", | ||
arguments=[Argument(name="code", value=code)], | ||
fields=["name"] | ||
) | ||
] | ||
) | ||
query = gql(getContinentName.render()) | ||
params = {"code": "EU"} | ||
# Get name of continent with code "EU" | ||
result = client.execute(query, variable_values=params) | ||
print(result) | ||
# {'continent': {'name': 'Europe'}} | ||
params = {"code": "AF"} | ||
# Get name of continent with code "AF" | ||
result = client.execute(query, variable_values=params) | ||
print(result) | ||
# {'continent': {'name': 'Africa'}} |