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
--------------
Graphene-Django-Extras
===============================================================================
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:
.. code:: bash
pip install "graphene-django-extras"
Documentation
~~~~~~~~~~~~~
Extra functionalities:
Fields:
1. DjangoListField
2. DjangoFilterListField
3. DjangoFilterPaginateListField
4. DjangoListObjectField
Mutations:
1. DjangoSerializerMutation
Types:
1. DjangoObjectTypeExtra
2. DjangoInputObjectType
3. DjangoPaginatedObjectListType
Pagination:
1. LimitOffsetGraphqlPagination
2. PageGraphqlPagination
3. CursosGraphqlPagination (cooming soon)
Examples:
~~~~~~~~
Here is a simple use of graphene-django-extras:
1- Types Definition:
.. code:: python
from django.contrib.auth.models import User
from graphene_django_extras import DjangoObjectType, DjangoPaginatedObjectListType
from graphene_django_extras.pagination import LimitOffsetGraphqlPagination
class UserType(DjangoObjectType):
"""
This 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:
.. code:: python
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:
.. code:: python
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:
.. code:: python
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:
.. code:: javascript
{
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:
.. code:: javascript
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
}
}
}
Graphene-Django-Extras
===============================================================================
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:
.. code:: bash
pip install "graphene-django-extras"
Documentation
~~~~~~~~~~~~~
Extra functionalities:
Fields:
1. DjangoListField
2. DjangoFilterListField
3. DjangoFilterPaginateListField
4. DjangoListObjectField
Mutations:
1. DjangoSerializerMutation
Types:
1. DjangoObjectTypeExtra
2. DjangoInputObjectType
3. DjangoPaginatedObjectListType
Pagination:
1. LimitOffsetGraphqlPagination
2. PageGraphqlPagination
3. CursosGraphqlPagination (cooming soon)
Examples:
~~~~~~~~
Here is a simple use of graphene-django-extras:
1- Types Definition:
.. code:: python
from django.contrib.auth.models import User
from graphene_django_extras import DjangoObjectType, DjangoPaginatedObjectListType
from graphene_django_extras.pagination import LimitOffsetGraphqlPagination
class UserType(DjangoObjectType):
"""
This 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:
.. code:: python
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:
.. code:: python
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:
.. code:: python
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:
.. code:: javascript
{
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:
.. code:: javascript
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
Close
Hashes for graphene-django-extras-0.0.1b2.zip
Algorithm | Hash digest | |
---|---|---|
SHA256 | c635de50b52d66ef39595dc6b4b58e5c24839bbe992fc89de73f3959a546d4d5 |
|
MD5 | 48e2822e7fcb1b958cca47f6af80cfc1 |
|
BLAKE2b-256 | bdde09d5323afb99d3b00a85b2948bd94aa887eebea05ec4889e1e501786e394 |
Close
Hashes for graphene_django_extras-0.0.1b2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6166a758d86ef81af53ecb385cfe8657d8c42a272b108b47211d072be050d1a3 |
|
MD5 | b5aef4a5c6909e8904b18ef5d437f34f |
|
BLAKE2b-256 | 780617f768d6c0fb46581f4fd03eb40b0ed661418290682d950817e7dd711c2a |