Skip to main content

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.0 library
  • 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

Release files for gqltype 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gqltype 0.1.2
File Size Uploaded
gqltype-0.1.2.tar.gz 22.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gqltype 0.1.2
File Interpreter ABI Platform
gqltype-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 52.6 kB

Release files / gqltype-0.1.2.tar.gz

Download URL gqltype-0.1.2.tar.gz
Size 22.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c1857da015974e3638a1f052063723480b7eea9066e98fa385a77faeffd7bae9
BLAKE2b-256 checksum
How to use checksums
3aa2356d0e4afbcb2bc189c193b1357582cbd25ec2a9132aa87e5cb2378e10aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.2 Linux/5.3.0-53-generic

Release files / gqltype-0.1.2-py3-none-any.whl

Download URL gqltype-0.1.2-py3-none-any.whl
Size 30.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2c28d265e14ced2e16e3fadd77122046bb688b505020d11fba60a913e626b01
BLAKE2b-256 checksum
How to use checksums
c5e78c07ecd07f5f5889216c4708a9d43ebc70ff201a0c99118d9e642d81cf99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.2 Linux/5.3.0-53-generic

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page