Skip to content

graphql-python/graphene-pydantic

Repository files navigation

Graphene Logo graphene-pydantic Build status PyPI version Coverage Status

A Pydantic integration for Graphene.

Installation

pip install "graphene-pydantic"

Examples

Here is a simple Pydantic model:

import pydantic class PersonModel(pydantic.BaseModel): id: uuid.UUID first_name: str last_name: str

To create a GraphQL schema for it you simply have to write the following:

import graphene from graphene_pydantic import PydanticObjectType class Person(PydanticObjectType): class Meta: model = PersonModel # only return specified fields only_fields = ("name",) # exclude specified fields exclude_fields = ("id",) class Query(graphene.ObjectType): people = graphene.List(User) def resolve_people(self, info): return get_people() # function returning `PersonModel`s schema = graphene.Schema(query=Query)

Then you can simply query the schema:

query = '''  query {  people {  firstName,  lastName  }  } ''' result = schema.execute(query)

Full Examples

Please see the examples directory for more.

About

Integrate GraphQL with your Pydantic models

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 20

Languages