Skip to main content

A collection of custom extensions for Django GraphQL

Project description

Pypi Build Status Codecov Codacy

A collection of custom extensions for Django GraphQL

Dependencies

  • Python ≥ 3.6

  • Django ≥ 2.0

  • Graphene-django ≥ 3.0.0b1

Installation

Install last stable version from Pypi.

pip install django-graphql-extensions

Authentication

  • @login_required

  • @staff_member_required

  • @superuser_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.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": [
    {
      "message": "You do not have permission to perform this action",
      "locations": [
        {
          "line": 3,
          "column": 13
        }
      ],
      "path": [
        "viewer"
      ],
      "extensions": {
        "type": "PermissionDenied",
        "code": "permissionDenied",
        "timestamp": 1622783872,
        "data": {},
        "operation": "QUERY",
        "trace": [
          "  File \"site-packages/graphql/execution/execute.py\", line 617, in resolve_field\n    result = resolve_fn(source, info, **args)\n",
          "  File \"graphql_extensions/decorators.py\", line 23, in wrapper\n    return func(info.context, *args, **kwargs)\n",
          "  File \"graphql_extensions/decorators.py\", line 35, in wrapper\n    raise exc\n"
        ]
      }
    }
  ],
  "data": {
    "viewer": null
  }
}

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.test 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_viewer(self):
        user = get_user_model().objects.create_user(
            username='test',
            password='dolphins',
        )

        self.client.authenticate(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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-graphql-extensions-0.1.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

django_graphql_extensions-0.1.0-py2.py3-none-any.whl (10.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-graphql-extensions-0.1.0.tar.gz.

File metadata

  • Download URL: django-graphql-extensions-0.1.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.0

File hashes

Hashes for django-graphql-extensions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eb997da2b56b934b78235d40935e6762828243e92e417a12e144e445a220bb04
MD5 06ce11b34420f04fcbc83a93da493566
BLAKE2b-256 bb00a3002adf9fb2b53cdf33b4bec98f9736f71c9010362353ec5b10b96fd1e0

See more details on using hashes here.

File details

Details for the file django_graphql_extensions-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_graphql_extensions-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.0

File hashes

Hashes for django_graphql_extensions-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7900d2f3b025fa3f8673f0373e4c97af0fc56681763407c13ed086a00bc735dc
MD5 17080ca668e5ddb34245259f955a715b
BLAKE2b-256 2e167cdbefb036a7c751cc754913cd6c0ddd6cf670b17ced9d5d50fbae598f76

See more details on using hashes here.

Supported by

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