Graphene Pydantic integration
Project description
graphene-pydantic
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(Person)
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)
Forward declarations and circular references
graphene_pydantic
supports forward declarations and circular references, but you will need to call the resolve_placeholders()
method to ensure the types are fully updated before you execute a GraphQL query. For instance:
class NodeModel(BaseModel):
id: int
name: str
labels: 'LabelsModel'
class LabelsModel(BaseModel):
node: NodeModel
labels: typing.List[str]
class Node(PydanticObjectType):
class Meta:
model = NodeModel
class Labels(PydanticObjectType):
class Meta:
model = LabelsModel
Node.resolve_placeholders() # make the `labels` field work
Labels.resolve_placeholders() # make the `node` field work
Full Examples
Please see the examples directory for more.
License
This project is under the Apache License.
Third Party Code
This project depends on third-party code which is subject to the licenses set forth in Third Party Licenses.
Contributing
Please see the Contributing Guide. Note that you must sign the CLA.
Caveats
Note that even though Pydantic is perfectly happy with fields that hold mappings (e.g. dictionaries), because GraphQL's type system doesn't have them those fields can't be exported to Graphene types. For instance, this will fail with an error Don't know how to handle mappings in Graphene
:
import typing
from graphene_pydantic import PydanticObjectType
class Pet:
pass
class Person:
name: str
pets_by_name: typing.Dict[str, Pet]
class GraphQLPerson(PydanticObjectType):
class Meta:
model = Person
However, note that if you use exclude_fields
or only_fields
to exclude those values, there won't be a problem:
class GraphQLPerson(PydanticObjectType):
class Meta:
model = Person
exclude_fields = ("pets_by_name",)
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
File details
Details for the file graphene_pydantic-0.0.4.tar.gz
.
File metadata
- Download URL: graphene_pydantic-0.0.4.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.17 CPython/3.7.3 Linux/4.15.0-1043-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26b55796c2a50e31fd79d5900b12d6b3e1bfdfbfe78aaa01238cab6b2c8bf044 |
|
MD5 | cbc248f598fd6d062abc5ee8e8f23903 |
|
BLAKE2b-256 | 036c85cfd9c07451f35c88560f0c0c0815ccec3de627595a9774f744c0497b27 |
File details
Details for the file graphene_pydantic-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: graphene_pydantic-0.0.4-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.17 CPython/3.7.3 Linux/4.15.0-1043-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cd58a3e5041096eb5d94a5a7079c975e595486f9a20a343eef553888b4e6d69 |
|
MD5 | 5177511b9553e6c42d07a0fd04fb698b |
|
BLAKE2b-256 | c9e375654bb4815d1ee5dec92fa6766e1a4ab1e4493e2770ad759675ef8e8a33 |