Skip to main content

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:
  1. DjangoListField

  2. DjangoFilterListField

  3. DjangoFilterPaginateListField

  4. DjangoListObjectField

Mutations:
  1. DjangoSerializerMutation

Types:
  1. DjangoObjectTypeExtra

  2. DjangoListObjectType

  3. DjangoInputObjectType

Pagination:
  1. LimitOffsetGraphqlPagination

  2. PageGraphqlPagination

  3. CursorGraphqlPagination (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, DjangoListObjectType
from graphene_django_extras.pagination import LimitOffsetGraphqlPagination

class UserType(DjangoObjectType):
    """
        This DjangoObjectType have a ID field, that allow filter by id and resolve method definition on Queries is not necesary
    """

    class Meta:
        model = User
        description = " Type definition for single User model object "
        filter_fields = {
            'id': ['exact', ],
            'first_name': ['icontains', 'iexact'],
            'last_name': ['icontains', 'iexact'],
            'username': ['icontains', 'iexact'],
            'email': ['icontains', 'iexact']
        }

class UserListType(DjangoListObjectType):
    class Meta:
        description = " Type definition for List of users "
        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 = " User Input Type for used as input on Argumments classes on traditional Mutations "
        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):
    """
        On traditional graphene mutation classes definition you must implement the mutate function
    """

    user = graphene.Field(UserType, required=False)

    class Arguments:
        new_user = graphene.Argument(UserInput)

    class Meta:
        description = " Traditional graphene 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 Users query'))
    all_users1 = DjangoFilterPaginateListField(UserType, pagination=LimitOffsetGraphqlPagination())
    all_users2 = DjangoFilterListField(UserType)
    all_users3 = DjangoListObjectField(UserListType, filterset_class=UserFilter, description=_('All Users query'))

    # Defining the petition to a user
    user = DjangoObjectField(UserType, description=_('Only one user'))

    # Another way to define a single user query
    other_way_user = DjangoObjectField(UserListType.getOne(), description=_('User List with pagination and filtering'))

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

graphene-django-extras-0.0.1b6.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphene_django_extras-0.0.1b6-py2.py3-none-any.whl (22.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file graphene-django-extras-0.0.1b6.tar.gz.

File metadata

File hashes

Hashes for graphene-django-extras-0.0.1b6.tar.gz
Algorithm Hash digest
SHA256 a43415e417a6fb7da79297bc589f83f00f0dc2a39318d59cd06ad19f4eea986d
MD5 4c469cc7a4e650d144f3df7b6f534d5c
BLAKE2b-256 1640c13f892fff967753a4b6723c0d5b614f4060ccec345337fdb0e08626e1f3

See more details on using hashes here.

File details

Details for the file graphene_django_extras-0.0.1b6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for graphene_django_extras-0.0.1b6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 14f9146f8bf6943bee59cc08a8ab78c5a363b7f6d141a6f828b2ab6c0e233bc8
MD5 825ede1223eb53a9086919fefef3ab39
BLAKE2b-256 c6c26ae9ef976fc18cd25ee4658b0502cfde2aa7b97921ac6e425ba9b344fdf9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page