Skip to main content
https://github.com/avanov/graphql-dsl/workflows/GitHub%20CI/badge.svg?branch=develop https://travis-ci.org/avanov/graphql-dsl.svg?branch=develop https://circleci.com/gh/avanov/graphql-dsl/tree/develop.svg?style=svg https://coveralls.io/repos/github/avanov/graphql-dsl/badge.svg?branch=develop Requirements Status Documentation Status Latest PyPI Release

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

Release files for graphql-dsl 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graphql-dsl 0.2.1
File Size Uploaded
graphql-dsl-0.2.1.tar.gz 11.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for graphql-dsl 0.2.1
File Interpreter ABI Platform
graphql_dsl-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / graphql-dsl-0.2.1.tar.gz

Download URL graphql-dsl-0.2.1.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
eae08494220b2e63b723d46169ffaf6dd43b71a493b62b84becfca2e591e2359
BLAKE2b-256 checksum
How to use checksums
d0acf443500f75559a62647ac103bb5ddc442ea3ada01b2749ebe2e294952675
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / graphql_dsl-0.2.1-py3-none-any.whl

Download URL graphql_dsl-0.2.1-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
15132d2f84df3e78b0b4e6afb3d2f7a3842dfff9d2731f470200aa404d6d569e
BLAKE2b-256 checksum
How to use checksums
d14330f1266289396baa9b89721a5bb93c6fd562d83725e0964f032109d2c989
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page