Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/avanov/graphql-dsl
Compose GraphQL queries by composing Python types!
https://github.com/avanov/graphql-dsl
dsl edsl graphql python query-builder type-hints typing
Last synced: 18 days ago
JSON representation
Compose GraphQL queries by composing Python types!
- Host: GitHub
- URL: https://github.com/avanov/graphql-dsl
- Owner: avanov
- License: mit
- Created: 2020-04-17T16:09:55.000Z (over 4 years ago)
- Default Branch: develop
- Last Pushed: 2021-05-31T23:10:51.000Z (over 3 years ago)
- Last Synced: 2024-10-06T06:04:26.502Z (about 2 months ago)
- Topics: dsl, edsl, graphql, python, query-builder, type-hints, typing
- Language: Python
- Homepage: https://graphql-dsl.rtfd.io/
- Size: 50.8 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES.rst
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
.. _badges:
.. image:: https://github.com/avanov/graphql-dsl/workflows/GitHub%20CI/badge.svg?branch=develop
:target: https://github.com/avanov/graphql-dsl/actions?query=workflow%3A%22GitHub+CI%22.. image:: https://travis-ci.org/avanov/graphql-dsl.svg?branch=develop
:target: https://travis-ci.org/avanov/graphql-dsl.. image:: https://circleci.com/gh/avanov/graphql-dsl/tree/develop.svg?style=svg
:target: https://circleci.com/gh/avanov/graphql-dsl/tree/develop.. image:: https://coveralls.io/repos/github/avanov/graphql-dsl/badge.svg?branch=develop
:target: https://coveralls.io/github/avanov/graphql-dsl?branch=develop.. image:: https://requires.io/github/avanov/graphql-dsl/requirements.svg?branch=develop
:target: https://requires.io/github/avanov/graphql-dsl/requirements/?branch=develop
:alt: Requirements Status.. image:: https://readthedocs.org/projects/graphql-dsl/badge/?version=develop
:target: http://graphql-dsl.readthedocs.org/en/develop/
:alt: Documentation Status.. image:: http://img.shields.io/pypi/v/graphql-dsl.svg
:target: https://pypi.python.org/pypi/graphql-dsl
:alt: Latest PyPI ReleaseCompose GraphQL queries by composing Python types
=================================================.. code-block:: bash
pip install graphql-dsl
Let's take a manually written `GraphQL query from the official docs `_:
.. code-block::
query {
hero {
name
}
droid(id: "2000") {
name
}
}With ``graphql-dsl`` you can construct a similar query with the following Python snippet:
.. code-block:: python
from typing import NamedTuple
from graphql_dsl import *class Hero(NamedTuple):
name: strclass Droid(NamedTuple):
name: strclass HeroAndDroid(NamedTuple):
hero: Hero
droid: Droidclass Input(NamedTuple):
droid_id: IDq = 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.. code-block:: python
import requests
q = GQL(...)
def call_server(droid_id: ID) -> HeroAndDroid:
response = requests.post(
url='https:///',
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):.. code-block:: python
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: IDq = 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 `_.
.. code-block:: bash
nix-shell
pytest