Skip to main content

GraphQL DSL

Project description

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 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)

Uploaded Source

Built Distribution

graphql_dsl-0.1.3-py3-none-any.whl (7.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page