Simple way to define GraphQL schema
Project description
Description
Currently EXPERIMENTAL.
gqltype is a GraphQL schema generator from python type annotations.
Features
- simple definition of GraphQL schema via python type annotations
- builds schema based on
graphql-core>=3.0library - asgi friendly
Installation
Using pip
$ pip install gqltype
Using poetry
$ poetry add gqltype
Quick intro
Let's say we want to model the schema mentioned at the beginning of https://graphql.org/learn/schema/ tutorial.
from dataclasses import dataclass
from enum import Enum
from typing import List
import gqltype
class Episode(Enum):
"""Codename for the episodes"""
NEWHOPE = "new hope"
EMPIRE = "empire"
JEDI = "jedi"
@dataclass
class Character:
"""An individual person within the Star Wars universe"""
name: str
appears_in: List[Episode]
class LengthUnit(Enum):
"""Measure of length"""
METER = "meter"
INCH = "inch"
@dataclass
class Starship:
"""A single transport craft that has hyperdrive capability"""
id: str
name: str
length: float
def resolve_length(self, unit: LengthUnit = LengthUnit.METER) -> float:
if unit == LengthUnit.INCH:
return self.length / 0.0254
return self.length
def get_character() -> Character:
return Character(name="R2D2", appears_in=[Episode.JEDI, Episode.NEWHOPE])
async def get_starship() -> Starship:
return Starship(id="F1000", name="Millennium Falcon", length=34.75)
def add_character(name: str, appears_in: List[Episode]) -> Character:
return Character(name=name, appears_in=appears_in)
schema = gqltype.Schema(
queries=[get_character, get_starship],
mutations=[add_character]
)
from graphql.utilities import print_schema
print(print_schema(schema.build()))
It'll produce the following output
type Query {
getCharacter: Character!
getStarship: Starship!
}
"""An individual person within the Star Wars universe"""
type Character {
appearsIn: [Episode!]!
name: String!
}
"""Codename for the episodes"""
enum Episode {
NEWHOPE
EMPIRE
JEDI
}
"""A single transport craft that has hyperdrive capability"""
type Starship {
length(unit: LengthUnit! = METER): Float!
id: ID!
name: String!
}
"""Measure of length"""
enum LengthUnit {
METER
INCH
}
type Mutation {
addCharacter(name: String!, appearsIn: [Episode!]!): Character!
}
In order to run server with this schema we can use Starlette
if __name__ == "__main__":
import uvicorn
from gqltype.contrib.starlette import GraphQLApp
from starlette.applications import Starlette
from starlette.routing import Route
app = Starlette(routes=[Route("/graphql", GraphQLApp(schema=schema))])
uvicorn.run(app)
Executing
{
getCharacter {
name
appearsIn
}
getStarship {
id
name
length(unit: INCH)
}
}
gives
{
"data": {
"getCharacter": {
"name": "R2D2",
"appearsIn": [
"JEDI",
"NEWHOPE"
]
},
"getStarship": {
"id": "F1000",
"name": "Millennium Falcon",
"length": 1368.1102362204724
}
}
}
TODO
-
sanity checks
- warn if class and resolve method specify different types
-
generic resolvers for certain types?
-
gqltype.F ? -- field definition
-
core part and high level part
-
business level
- validation for input values
- serialization of output values
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gqltype-0.1.2.tar.gz.
File metadata
- Download URL: gqltype-0.1.2.tar.gz
- Upload date:
- Size: 22.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.3.0-53-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1857da015974e3638a1f052063723480b7eea9066e98fa385a77faeffd7bae9
|
|
| MD5 |
cc9925d3e3cd6e61df6a6a3d6f1f4210
|
|
| BLAKE2b-256 |
3aa2356d0e4afbcb2bc189c193b1357582cbd25ec2a9132aa87e5cb2378e10aa
|
File details
Details for the file gqltype-0.1.2-py3-none-any.whl.
File metadata
- Download URL: gqltype-0.1.2-py3-none-any.whl
- Upload date:
- Size: 30.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.3.0-53-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2c28d265e14ced2e16e3fadd77122046bb688b505020d11fba60a913e626b01
|
|
| MD5 |
bb590caa7e8835d49e6d3c2c161666e3
|
|
| BLAKE2b-256 |
c5e78c07ecd07f5f5889216c4708a9d43ebc70ff201a0c99118d9e642d81cf99
|