Package: pygraphy
PygraphyA modern pythonic GraphQL implementation, painless GraphQL developing experience for Pythonista.DocumentSeeofficial docs.Quick ReviewAll the behaviors of Pygraphy are no difference from your intuition.importasyncioimportpygraphyfromtypingimportList,Optionalfromstarlette.applicationsimportStarletteimportuvicornapp=Starlette(debug=True)classEpisode(pygraphy.Enum):NEWHOPE=4EMPIRE=5JEDI=6classCharacter(pygraphy.Interface):"""Character interface contains human and droid"""id:strname:strappears_in:List[Episode]@pygraphy.fielddeffriends(self)->Optional[List['Character']]:return[]classHuman(pygraphy.Object,Character):"""Human object"""home_planet:strclassDroid(pygraphy.Object,Character):"""Driod object"""primary_function:strclassQuery(pygraphy.Query):@pygraphy.fieldasyncdefhero(self,episode:Episode)->Optional[Character]:awaitasyncio.sleep(1)returnDroid(id="2001",name="R2-D2",appears_in=[Episode.NEWHOPE,Episode.EMPIRE,Episode.JEDI],primary_function="Astromech",)@app.route('/')classSchema(pygraphy.Schema):query:Optional[Query]if__name__=='__main__':uvicorn.run(app,host='0.0.0.0',port=8000)InstallationWeb Server Requiredpip install 'pygraphy[web]'Standalone Model and Query Handlerpip install 'pygraphy'FeaturesClean room Pythonic schema definition systemModel definition bases on Python DataclassPython Asyncio supportContext management bases on Python Context VariablesIntrospection and GraphQL Playground supportComparation with GraphQL-Core(-Next)AdvantagesGraphQL-Core-Nextis 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 asgraphql-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.DisadvantagesPygraphy is still in pre-alpha version, and need stable, welcome feedback.Pygraphydoes not supportfull features of GraphQL according to Spec right now, the rest part of Spec will be integrated literally in the future, it containsDerectivesID ScalarType ExtensionsSome Validation CheckMost of features are already implemented so do not panic.BenchmarkCompare 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