Skip to main content

Type-safe interface for graphene-python.

Project description

typed-graphene

typed-graphene is a library that provides a type-safe interface for graphene-python.

Examples

Type-Safe Query

from graphene import ObjectType, Field, String

class ExampleQuery(ObjectType):
	foo = Field(FooType, required=True, input_a=String(), input_b=String(required=True))

	def resolve_foo(self, info, **data) -> FooType:
		input_a = data.get("input_a") # any
		input_b = data["input_b"] # any

graphene example

from typing import TypedDict, NotRequired

from graphene import Field
from typed_graphene import TypedField

class FooFieldArguments(TypedDict):
	input_a: NotRequired[str]
	input_b: str

class ExampleQuery(ObjectType):
	# `required=True` by default
	foo = TypedField(FooType, **FooFieldArguments.__annotations__)

	def resolve_foo(self, info, **data: Unpack[FooFieldArguments]) -> FooType:
		input_a = data.get("input_a") # str | None
		input_b = data["input_b"] # str

typed-graphene exmaple

Type-Safe Mutation

from graphene import Mutation, Field, String, Boolean

class Example(Mutation):
	class Arguments:
		input_a = String()
		input_b = String(required=True)

    ok = Field(Boolean, required=True)
	errors = Field(ExampleErrors)

	@classmethod
	def mutate(cls, root, info, **data) -> Self:
		input_a = data.get("input_a") # any
		input_b = data["input_b"] # any

	return cls(ok=True)

graphene example

from dataclasses import dataclass
from typing import TypedDict, NotRequired

from typed_graphene import TypedBaseMutation, TypedField

class Example(TypedBaseMutation):
	class TypedArguments(TypedBaseMutation.TypedArguments):
		input_a: NotRequired[str]
		input_b: str

    ok = TypedField(bool)
	# concat type with ` | None` for `required=False`
	errors = TypedField(ExampleErrors | None)

	@classmethod
	def validate(cls, root, info, **data: Unpack[TypedArguments]) -> ExampleErrors:
		errors = ExampleErrors()

		errors.input_a = "error" # no error

		return errors

	@classmethod
	def execute(cls, root, info, **data: Unpack[TypedArguments]) -> Self:
		input_a = data.get("input_a") # str | None
		input_b = data["input_b"] # str

	return cls(ok=True)

typed-graphene example

Topics

Defining custom types

You can define custom types by inheriting from BaseTransformer and registering it with register.

from graphene import ID
from typed_graphene import BaseTransformer, register

class IDStr(str):
    pass

@register
class IDStrTransformer(BaseTransformer):
    python_type = IDStr
    graphene_type = ID

You can also override check_type and transform_type to define custom type checking and transformation. It is recommended to add @cache to the check_type and transform_type methods for performance and type-consistency.

from typing import Literal, get_origin

from graphene import Literal
from typed_graphene import BaseTransformer, register

@register
class LiteralTransformer(BaseTransformer[Literal, Enum]):
    python_type = Literal
    graphene_type = Enum

    @classmethod
    @cache
    def check_type(cls, T):
        return get_origin(T) == Literal

    @classmethod
    @cache
    def transform_type(cls, T: type[Literal]) -> type[Enum]:
        """Transform the type into the graphene type."""
        return Enum.from_enum(literal_to_enum(T))

Author

Jeong Yeon Nam(tonynamy@apperz.co.kr)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typed_graphene-0.0.2.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typed_graphene-0.0.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file typed_graphene-0.0.2.tar.gz.

File metadata

  • Download URL: typed_graphene-0.0.2.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for typed_graphene-0.0.2.tar.gz
Algorithm Hash digest
SHA256 9f559abfe9734c81f60d636a3cb7bf91b763dda875dc69b2a42078fd9db3f4b1
MD5 d8d8eb398b039836d0db88a7b5cf753c
BLAKE2b-256 98966806bd1b223f26cdff359a5c375f808a17c36c686a5e2bd4f90be9cae029

See more details on using hashes here.

File details

Details for the file typed_graphene-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: typed_graphene-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for typed_graphene-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d8eecfafa29abd2a1a8fdfccad79d2a236e353cdc70cf7234b2a5879249a76c4
MD5 4db26555457ba8376917bd24fc74d9b5
BLAKE2b-256 ccee8ca7562c0f8f2f23b11fe975e6f230bf123f47288396b14df36b1716e8bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page