Skip to main content

Compose GraphQL queries by composing Python types!

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

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.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

graphql_dsl-0.2.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file graphql-dsl-0.2.1.tar.gz.

File metadata

  • Download URL: graphql-dsl-0.2.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for graphql-dsl-0.2.1.tar.gz
Algorithm Hash digest
SHA256 eae08494220b2e63b723d46169ffaf6dd43b71a493b62b84becfca2e591e2359
MD5 5588035963bc133f0585677bc1ed2a74
BLAKE2b-256 d0acf443500f75559a62647ac103bb5ddc442ea3ada01b2749ebe2e294952675

See more details on using hashes here.

File details

Details for the file graphql_dsl-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: graphql_dsl-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for graphql_dsl-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 15132d2f84df3e78b0b4e6afb3d2f7a3842dfff9d2731f470200aa404d6d569e
MD5 f84303e5689e8d29d323f177a0fff329
BLAKE2b-256 d14330f1266289396baa9b89721a5bb93c6fd562d83725e0964f032109d2c989

See more details on using hashes here.

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