GraphQL DSL
Project description
Compose GraphQL queries by defining 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.1.3.tar.gz
(7.6 kB
view hashes)
Built Distribution
Close
Hashes for graphql_dsl-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63a6335afe07b73ee233fcef63bb8777e9868a2e16f204ef67a8c18f64ce5bc0 |
|
MD5 | 88a20dc4f3b4410174637507a8586d21 |
|
BLAKE2b-256 | 1dde1fc1d16b2cf9289c40fbe406e3ea24d13d55556e64da35c59b3884d86330 |