Compose GraphQL queries by composing Python types!
Project description
Compose GraphQL queries by composing Python types
pip install graphql-dsl
Let’s take a manually written GraphQL query from the official docs:
query {
hero {
name
}
droid(id: "2000") {
name
}
}
With graphql-dsl you can construct a similar query with the following Python snippet:
from typing import NamedTuple
from graphql_dsl import *
class Hero(NamedTuple):
name: str
class Droid(NamedTuple):
name: str
class HeroAndDroid(NamedTuple):
hero: Hero
droid: Droid
class Input(NamedTuple):
droid_id: ID
q = GQL( QUERY | HeroAndDroid
| WITH | Input
| PASS | Input.droid_id * TO * HeroAndDroid.droid * AS * 'id'
)
print(q.query)
and the output will be:
query HeroAndDroid($droidId:ID!){hero{name}droid(id:$droidId){name}}
The value of q.query is a GraphQL query template that should be used with instances of Input to call servers with GraphQL API
import requests
q = GQL(...)
def call_server(droid_id: ID) -> HeroAndDroid:
response = requests.post(
url='https://<graphql-server-url>/',
json=q.request_payload(Input(droid_id=droid_id)),
headers={ 'Content-Type': 'application/json'
, 'Accept': 'application/json'
}
)
response.raise_for_status()
return q.get_result(response.json())
Note that the query q resides at the top-level module scope, as the query constructor doesn’t depend on the query’s input values, it only needs to know about the shapes of input and output data.
The query builder supports both NamedTuple and @dataclass types, yet the latter has a slightly different field reference syntax (because dataclasses don’t define class-level field getters):
from dataclasses import dataclass
from graphql_dsl import *
@dataclass
class Hero:
name: str
@dataclass
class Droid:
name: str
@dataclass
class HeroAndDroid:
hero: Hero
droid: Droid
@dataclass
class Input:
droid_id: ID
q = GQL( QUERY | HeroAndDroid
| WITH | Input
| PASS | (Input, 'droid_id') * TO * (HeroAndDroid, 'droid') * AS * 'id'
)
Find out more from Official Documentation.
Test Suite
Test environment is based on Nix.
nix-shell
pytest
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file graphql-dsl-0.2.1.tar.gz
.
File metadata
- Download URL: graphql-dsl-0.2.1.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1.post0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eae08494220b2e63b723d46169ffaf6dd43b71a493b62b84becfca2e591e2359 |
|
MD5 | 5588035963bc133f0585677bc1ed2a74 |
|
BLAKE2b-256 | d0acf443500f75559a62647ac103bb5ddc442ea3ada01b2749ebe2e294952675 |
File details
Details for the file graphql_dsl-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: graphql_dsl-0.2.1-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1.post0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15132d2f84df3e78b0b4e6afb3d2f7a3842dfff9d2731f470200aa404d6d569e |
|
MD5 | f84303e5689e8d29d323f177a0fff329 |
|
BLAKE2b-256 | d14330f1266289396baa9b89721a5bb93c6fd562d83725e0964f032109d2c989 |