Skip to main content

Pypi Wheel Build Status Codecov Code Climate

A collection of custom extensions for Django GraphQL

Dependencies

  • Python ≥ 3.4

  • Django ≥ 1.11

Installation

Install last stable version from Pypi.

pip install django-graphql-extensions

Authentication

  • @login_required

  • @staff_member_required

  • @permission_required

  • @user_passes_test

See the documentation to know the full list of decorators.

from django.contrib.auth import get_user_model

import graphene
from graphql_extensions.auth.decorators import (
    login_required, staff_member_required,
)


class Query(graphene.ObjectType):
    viewer = graphene.Field(UserType)
    users = graphene.List(UserType)

    @login_required
    def resolve_viewer(self, info, **kwargs):
        return info.context.user

    @staff_member_required
    def resolve_users(self, info, **kwargs):
        return get_user_model().objects.all()

Errors

Returning appropriate error responses and masking error messages sent to the client.

Configure your GraphQLView.

from django.urls import include, path

from graphql_extensions.views import GraphQLView

urlpatterns = [
    path('', GraphQLView.as_view(), name='index'),
]

Exceptions

from graphql_extensions import exceptions
  • exceptions.GraphQLError

  • exceptions.PermissionDenied

  • exceptions.ValidationError

  • exceptions.NotFound

Payload

{
  "errors": [
    {
      "type": "NotFound",
      "message": "GraphQL object not found",
      "code": "notFound",
      "data": {
        "id": 1
      },
      "path": ["updateGroup"],
      "operation": "mutation",
      "trace": [
        "  File \"/app/schema.py\", line 30, in mutate\n    group = cls.update(info, **kwargs)\n",
        "  File \"/graphql_extensions/mixins.py\", line 32, in update\n    instance = cls.get_object(context, id=id)\n",
        "  File \"/graphql_extensions/mixins.py\", line 21, in get_object\n    raise exceptions.NotFound(**kwargs)\n"
      ]
    }
  ],
  "data": {
    "updateGroup": null
  }
}

Mixins

Pre-built mutations that provide for commonly used patterns.

  • RetrieveMixin

  • UpdateMixin

from django.contrib.auth.models import Group

import graphene
from graphene_django import DjangoObjectType
from graphql_extensions import mixins
from graphql_extensions.auth.decorators import login_required


class GroupType(DjangoObjectType):

    class Meta:
        model = Group


class UpdateGroup(mixins.UpdateMixin, graphene.Mutation):
    group = graphene.Field(GroupType)

    class Arguments:
        id = graphene.Int(required=True)
        name = graphene.String()

    @classmethod
    def get_queryset(cls, info, **kwargs):
        return info.context.user.groups.all()

    @classmethod
    @login_required
    def mutate(cls, root, info, **kwargs):
        group = cls.update(info, **kwargs)
        return cls(group=group)

Writing tests

This package includes a subclass of unittest.TestCase SchemaTestCase and improve support for making GraphQL queries.

from django.contrib.auth import get_user_model

from graphql_extensions.testcases import SchemaTestCase


class UsersTests(SchemaTestCase):

    def test_create_user(self):
        query = '''
        mutation CreateUser($username: String!, $password: String!) {
          createUser(username: $username, password: $password) {
            user {
              id
            }
          }
        }'''

        response = self.client.execute(query, {
            'username': 'test',
            'password': 'dolphins',
        })

        self.assertFalse(response.errors)
        self.assertTrue(response.data['user'])

    def test_get_viewer(self):
        user = get_user_model().objects.create_user(
            username='test',
            password='dolphins')

        self.client.force_login(self.user)

        query = '''
        {
          viewer {
            username
          }
        }'''

        response = self.client.execute(query)
        data = response.data['viewer']

        self.assertEqual(data['username'], user.username)

Types

Custom Graphene types.

  • Email

  • Timestamp

  • Choices

  • CamelJSON

  • …

Relay

Complete support for Relay.

Metadata

Release files for django-graphql-extensions 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-graphql-extensions 0.0.7
File Size Uploaded
django-graphql-extensions-0.0.7.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-graphql-extensions 0.0.7
File Interpreter ABI Platform
django_graphql_extensions-0.0.7-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 25.8 kB

Release files / django-graphql-extensions-0.0.7.tar.gz

Download URL django-graphql-extensions-0.0.7.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3f5b4c9fc08cb50eaa54fee7bc04944236d8011303368b6fbf728e29ac3b93fa
BLAKE2b-256 checksum
How to use checksums
833b81e56ec743dfda248d31f593050fddeebba7ee7d0fc85fabce401dc9c7b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.8.0

Release files / django_graphql_extensions-0.0.7-py2.py3-none-any.whl

Download URL django_graphql_extensions-0.0.7-py2.py3-none-any.whl
Size 14.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6c182b2bc4562c9234aa219e80f690b8f5deb34e5f55e108a4e9c9827f44bb33
BLAKE2b-256 checksum
How to use checksums
0552b6324a0e93861508311e208dff238f5f4c93ad2bb5deec4385d675215b03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.8.0

Release history Release notifications | RSS feed

0.1.0

2 release files

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page