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:
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 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
graphql-dsl-0.2.0.tar.gz
(10.4 kB
view hashes)
Built Distribution
Close
Hashes for graphql_dsl-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab3b609bf69a64a832ce21f946b8df61c940ae8e0004bf0cc820be2b4dc6dee1 |
|
MD5 | 6d704001e35feee44dce38fa79ee3fe4 |
|
BLAKE2b-256 | 0921f39695cf3bfc140470fa06692ec1daf274278cf3b4aa357b259c7e4fad99 |