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.6

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.6
File Size Uploaded
django-graphql-extensions-0.0.6.tar.gz 10.2 kB Details

Built distribution (wheel)

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

Total release size: 26.9 kB

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

Download URL django-graphql-extensions-0.0.6.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0d0deeb20bcd8e8e8c48991eb0389dd84e1f88745d946f1f228fee6c03fe8ad5
BLAKE2b-256 checksum
How to use checksums
0897fd801365457b7cc114817f1d0a3548fc3eaa8c95cd96f3da12a05178d48b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

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

Download URL django_graphql_extensions-0.0.6-py2.py3-none-any.whl
Size 16.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
fc5704654930a362a5b3965115733f20b0536cc9de9f7669fa8f0e7de7f85fda
BLAKE2b-256 checksum
How to use checksums
3c170c87dabfc39245dd5649931b884b7a5d0e13c9fdafeb12620dc5d359b55e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release history Release notifications | RSS feed

0.1.0

2 release files

0.0.7

2 release files

This release

0.0.6 This release

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