Skip to content
A modern Pythonic GraphQL implementation.
Python HTML Makefile
Branch: master
Clone or download
ethe Merge pull request #14 from grvhi/master
Fix for #13: UnboundLocalError in introspection.py
Latest commit a3f553b Sep 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
benchmark Longer testing time Jul 25, 2019
docs Add docs Jul 20, 2019
examples Add docs Jul 20, 2019
pygraphy Sticky-keys-typo fix Sep 17, 2019
tests Support alias field Jul 25, 2019
.gitignore Add docs Jul 20, 2019
.travis.yml Debug travis ci config Jul 7, 2019
LICENSE Initial commit Jul 3, 2019
Makefile Add document site into README Jul 20, 2019
README.md Update README Jul 26, 2019
mkdocs.yml Add docs Jul 20, 2019
setup.cfg Clear up setup config Jul 13, 2019
setup.py Bump to 0.1.2 Jul 25, 2019
tox.ini Add subscription & optional install starlette & fix some bugs Jul 19, 2019

README.md

Pygraphy

A modern pythonic GraphQL implementation, painless GraphQL developing experience for Pythonista.

Build Status codecov pypi PyPI - Python Version

Document

See official docs.

Quick Review

All the behaviors of Pygraphy are no difference from your intuition.

import asyncio
import pygraphy
from typing import List, Optional
from starlette.applications import Starlette
import uvicorn


app = Starlette(debug=True)


class Episode(pygraphy.Enum):
    NEWHOPE = 4
    EMPIRE = 5
    JEDI = 6


class Character(pygraphy.Interface):
    """
    Character interface contains human and droid
    """
    id: str
    name: str
    appears_in: List[Episode]

    @pygraphy.field
    def friends(self) -> Optional[List['Character']]:
        return []


class Human(pygraphy.Object, Character):
    """
    Human object
    """
    home_planet: str


class Droid(pygraphy.Object, Character):
    """
    Driod object
    """
    primary_function: str


class Query(pygraphy.Query):

    @pygraphy.field
    async def hero(self, episode: Episode) -> Optional[Character]:
        await asyncio.sleep(1)
        return Droid(
            id="2001",
            name="R2-D2",
            appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
            primary_function="Astromech",
        )


@app.route('/')
class Schema(pygraphy.Schema):
    query: Optional[Query]


if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8000)

Installation

Web Server Required

pip install 'pygraphy[web]'

Standalone Model and Query Handler

pip install 'pygraphy'

Features

  • Clean room Pythonic schema definition system
  • Model definition bases on Python Dataclass
  • Python Asyncio support
  • Context management bases on Python Context Variables
  • Introspection and GraphQL Playground support

Comparation with GraphQL-Core(-Next)

Advantages

GraphQL-Core-Next is the official supporting implementation of GraphQL, and it is only a basic library. Generally, you should use Graphene or other wrapper libraries bases on it. Pygraphy is an integrated library that includes data mapping and model definition.

GraphQL-Core-Next is directly translated from GraphQL.js, this leads to some weird behaviors such as graphql-core-next/issues/37, and it is too tough to make a wrapper for walking around. Pygraphy rewrites the schema definition system in a more pythonic way. By using Python Metaclass, Pygraphy supports class-style schema definition naturally. There is no more inharmony between lambda function resolver (ugly Js style) and instance method resolver.

By using Context Variables which is added into Python in version 3.7, Pygraphy does not need to pass context through the call chain like graphql-core-next.

Also, Pygraphy is faster than graphql-core-next, you can check benchmark results as below.

And more, Pygraphy clearly support stateful subscription method with Python Asynchronous Generators, which is not elaborate in graphql-core-next.

Disadvantages

Pygraphy is still in pre-alpha version, and need stable, welcome feedback.

Pygraphy does not support full features of GraphQL according to Spec right now, the rest part of Spec will be integrated literally in the future, it contains

  • Derectives
  • ID Scalar
  • Type Extensions
  • Some Validation Check

Most of features are already implemented so do not panic.

Benchmark

Compare with Strawberry / graphql-core-next, Pygraphy is 4.5 times faster than it.

↳ uname -a
Darwin Workstation.local 19.0.0 Darwin Kernel Version 19.0.0: Thu Jul 11 18:37:36 PDT 2019; root:xnu-6153.0.59.141.4~1/RELEASE_X86_64 x86_64
↳ python -V
Python 3.7.2
↳ time python benchmark/test_pygraphy.py
python benchmark/test_pygraphy.py  3.48s user 0.10s system 99% cpu 3.596 total
↳ time python benchmark/test_strawberry.py
python benchmark/test_strawberry.py  16.05s user 0.16s system 99% cpu 16.257 total
You can’t perform that action at this time.