|
| 1 | +With gql |
| 2 | +======== |
| 3 | + |
| 4 | +It is the simple example for usage of **graphql-query** with `gql 3 <https://gql.readthedocs.io/en/stable/index.html>`_. |
| 5 | +How to install **gql 3** you can see `here <https://gql.readthedocs.io/en/stable/intro.html#installation>`_. |
| 6 | + |
| 7 | +Basic Usage |
| 8 | +----------- |
| 9 | + |
| 10 | +For the Basic usage of **gql 3** with **graphql-query** you can running the following code |
| 11 | + |
| 12 | +.. code-block:: python |
| 13 | +
|
| 14 | + from gql import Client, gql |
| 15 | + from gql.transport.aiohttp import AIOHTTPTransport |
| 16 | + from graphql_query import Operation, Query |
| 17 | +
|
| 18 | + # Select your transport with a defined url endpoint |
| 19 | + transport = AIOHTTPTransport(url="https://countries.trevorblades.com/") |
| 20 | +
|
| 21 | + # Create a GraphQL client using the defined transport |
| 22 | + client = Client(transport=transport, fetch_schema_from_transport=True) |
| 23 | +
|
| 24 | + # generating of GraphQL query string with graphql_query |
| 25 | + getContinents = Operation( |
| 26 | + type="query", |
| 27 | + name="getContinents", |
| 28 | + queries=[Query(name="continents", fields=["code", "name"])] |
| 29 | + ) |
| 30 | +
|
| 31 | + # Provide a GraphQL query |
| 32 | + query = gql(getContinents.render()) |
| 33 | +
|
| 34 | + # Execute the query on the transport |
| 35 | + result = client.execute(query) |
| 36 | + print(result) |
| 37 | + # {'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'}]} |
| 38 | +
|
| 39 | +Using variables |
| 40 | +--------------- |
| 41 | + |
| 42 | +GraphQL variables it is easy |
| 43 | + |
| 44 | +.. code-block:: python |
| 45 | +
|
| 46 | + from gql import Client, gql |
| 47 | + from gql.transport.aiohttp import AIOHTTPTransport |
| 48 | + from graphql_query import Argument, Operation, Query, Variable |
| 49 | +
|
| 50 | + # Select your transport with a defined url endpoint |
| 51 | + transport = AIOHTTPTransport(url="https://countries.trevorblades.com/") |
| 52 | +
|
| 53 | + # Create a GraphQL client using the defined transport |
| 54 | + client = Client(transport=transport, fetch_schema_from_transport=True) |
| 55 | +
|
| 56 | + # generating of GraphQL query string with graphql_query |
| 57 | + code = Variable(name="code", type="ID!") |
| 58 | +
|
| 59 | + getContinentName = Operation( |
| 60 | + type="query", |
| 61 | + name="getContinentName", |
| 62 | + variables=[code], |
| 63 | + queries=[ |
| 64 | + Query( |
| 65 | + name="continent", |
| 66 | + arguments=[Argument(name="code", value=code)], |
| 67 | + fields=["name"] |
| 68 | + ) |
| 69 | + ] |
| 70 | + ) |
| 71 | +
|
| 72 | + query = gql(getContinentName.render()) |
| 73 | +
|
| 74 | + params = {"code": "EU"} |
| 75 | +
|
| 76 | + # Get name of continent with code "EU" |
| 77 | + result = client.execute(query, variable_values=params) |
| 78 | + print(result) |
| 79 | + # {'continent': {'name': 'Europe'}} |
| 80 | +
|
| 81 | + params = {"code": "AF"} |
| 82 | +
|
| 83 | + # Get name of continent with code "AF" |
| 84 | + result = client.execute(query, variable_values=params) |
| 85 | + print(result) |
| 86 | + # {'continent': {'name': 'Africa'}} |
0 commit comments