A Pydantic integration for Graphene.
pip install "graphene-pydantic"
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)
Please see the examples directory for more.
This project is under the Apache License.
This project depends on third-party code which is subject to the licenses set forth in Third Party Licenses.
Please see the Contributing Guide. Note that you must sign the CLA.