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.2.tar.gz
(7.4 kB
view hashes)
Built Distribution
Close
Hashes for graphql_dsl-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eab9f8f390bde0d3877a0e5116ee04059bc4478287b514983e026884fde3af26 |
|
MD5 | a28838fe77ba66436d300a3f5c290694 |
|
BLAKE2b-256 | 1b0f3eb7275a4d9b6a357174e0d1baebab0331cbb0bb726f1d9a41ffeb031b79 |