Graphene-Django-Extras add some extra funcionalities to graphene-django to facilitate the graphql use without Relay and allow pagination and filtering integration
Project description
This package add some extra funcionalities to graphene-django to facilitate the graphql use without Relay and allow pagination and filtering integration.
Installation:
For installing graphene-django-extras, just run this command in your shell:
pip install "graphene-django-extras"
Documentation:
- Extra functionalities:
- Fields:
DjangoListField
DjangoFilterListField
DjangoFilterPaginateListField
DjangoListObjectField
- Mutations:
DjangoSerializerMutation
- Types:
DjangoObjectTypeExtra
DjangoInputObjectType
DjangoPaginatedObjectListType
- Pagination:
LimitOffsetGraphqlPagination
PageGraphqlPagination
CursosGraphqlPagination (cooming soon)
Examples:
Here is a simple use of graphene-django-extras:
1- Types Definition:
from django.contrib.auth.models import User
from graphene_django_extras import DjangoObjectType, DjangoPaginatedObjectListType
from graphene_django_extras.pagination import LimitOffsetGraphqlPagination
class UserType(DjangoObjectType):
"""
The DjangoObjectType have a ID field to filter to avoid resolve method definition on Queries
"""
class Meta:
model = User
description = "Type for User Model"
filter_fields = {
'id': ['exact', ],
'first_name': ['icontains', 'iexact'],
'last_name': ['icontains', 'iexact'],
'username': ['icontains', 'iexact'],
'email': ['icontains', 'iexact']
}
class UserListType(DjangoPaginatedObjectListType):
class Meta:
description = "User list query definition"
model = User
pagination = LimitOffsetGraphqlPagination()
2- Input Types can be defined for use on mutations:
from graphene_django_extras import DjangoInputObjectType
class UserInput(DjangoInputObjectType):
class Meta:
description = " Input Type for User Model "
model = User
3- You can define traditional mutations that use Input Types or Mutations based on DRF SerializerClass:
import graphene
from .serializers import UserSerializer
from graphene_django_extras import DjangoSerializerMutation
from .types import UserType
from .input_types import UserInputType
class UserSerializerMutation(DjangoSerializerMutation):
class Meta:
description = " Serializer based Mutation for Users "
serializer_class = UserSerializer
class UserMutation(graphene.mutation):
"""
You must implement the mutate function
"""
user = graphene.Field(UserType, required=False)
class Arguments:
new_user = graphene.Argument(UserInput)
class Meta:
description = "Normal mutation for Users"
@classmethod
def mutate(cls, info, **kwargs):
...
4- Defining schemes:
import graphene
from graphene_django_extras import DjangoObjectField, DjangoListObjectField
from .types import UserType, UserListType
from .mutations import UserMutation, UserSerializerMutation
class Queries(graphene.ObjectType):
# Posible User list queries definitions
all_users = DjangoListObjectField(UserListType, description=_('All Usersquery'))
all_users1 = DjangoFilterPaginateListField(UserType, pagination=LimitOffsetGraphqlPagination())
all_users2 = DjangoFilterListField(UserType)
all_users3 = DjangoListObjectField(UserListType, filterset_class=UserFilter, description=_('All Users query'))
# Single user queries definitions
user = DjangoObjectField(UserType, description=_('Single User query'))
other_way_user = DjangoObjectField(UserListType.getOne(), description=_('Other way to query a single User query'))
class Mutations(graphene.ObjectType):
user_create = UserSerializerMutation.CreateField(deprecation_reason='Deprecation message')
user_delete = UserSerializerMutation.DeleteField()
user_update = UserSerializerMutation.UpdateField()
traditional_user_mutation = UserMutation.Field()
5- Examples of queries:
{
allUsers(username_Icontains:"john"){
results(limit:5, offset:5){
id
username
firstName
lastName
}
totalCount
}
allUsers1(lastName_Iexact:"Doe", limit:5, offset:0){
id
username
firstName
lastName
}
allUsers2(firstName_Icontains: "J"){
id
username
firstName
lastName
}
user(id:2){
id
username
firstName
}
}
6- Examples of Mutations:
mutation{
userCreate(newUser:{password:"test*123", email: "test@test.com", username:"test"}){
user{
id
username
firstName
lastName
}
ok
errors{
field
messages
}
}
userDelete(id:1){
ok
errors{
field
messages
}
}
userUpdate(newUser:{id:1, username:"John"}){
user{
id
username
}
ok
errors{
field
messages
}
}
}
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
Hashes for graphene-django-extras-0.0.1b4.zip
Algorithm | Hash digest | |
---|---|---|
SHA256 | 491815943e40bb1cbfc475fd0edd1d0da8ba3a86ddf1c671063a44b201f626b9 |
|
MD5 | 2521957dea1422ec485cac688ae9d608 |
|
BLAKE2b-256 | a9b5e29fd0e3086afe0102d1952ffc4c488a264d917ec3443b40fa9d3a8308f0 |
Hashes for graphene_django_extras-0.0.1b4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dd58c40f6ff9b4655b6a1b15de3eea858bd08597a5c7926623f6a5e170ed6fe |
|
MD5 | 23fcd264db08f565654c30bb195df1b2 |
|
BLAKE2b-256 | 9a850f31147589e8d31e4a05046adcf00d7174bb052ae174403541b7409078d5 |