A collection of custom extensions for Django GraphQL
Project description
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
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):
me = graphene.Field(UserType)
users = graphene.List(UserType)
@login_required
def resolve_me(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
raise exceptions.GraphQLError()
raise exceptions.NotAuthenticated()
raise exceptions.PermissionDenied()
raise exceptions.ValidationError()
raise 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)
Testing
Helper classes to improve support for testing.
GraphQLTestCase
from graphql_extensions.testcases import GraphQLTestCase
class UsersTests(GraphQLTestCase):
def test_create_user(self):
query = '''
mutation CreateUser($username: String!, $password: String!) {
createUser(username: $username, password: $password) {
user {
id
}
}
}'''
username = 'test'
password = 'dolphins'
response = self.client.execute(query, {
'username': username,
'password': password,
})
self.assertFalse(response.errors)
self.assertTrue(response.data['user'])
self.client.login(username=username, password=password)
query = '''
{
me {
username
}
}'''
response = self.client.execute(query)
self.assertEqual(response.data['me']['username'], username)
Types
Custom Graphene types.
Email
Timestamp
Choices
CamelJSON
…
Relay
Complete support for Relay.
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
Built Distribution
File details
Details for the file django-graphql-extensions-0.0.1.tar.gz
.
File metadata
- Download URL: django-graphql-extensions-0.0.1.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 853d36a89bcb832701438701ee705b74c796280ef0033341e7c3b9810b200a81 |
|
MD5 | af567af74fd47462e138bee5bd6dd181 |
|
BLAKE2b-256 | 88e3e8fbad65f1d7b068050b7efea7539d6cef969daa483c791474960dd89a31 |
Provenance
File details
Details for the file django_graphql_extensions-0.0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: django_graphql_extensions-0.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e17ebe95b3c441048576c64f5f9a5a073bb8e4cdf1b1fecc73874f4783f9456 |
|
MD5 | 3bbfc2d4c7b6e193a86794a7a38c697b |
|
BLAKE2b-256 | 5ced69445e1513187f886b5c460a1377dfa9c5745101fffdeecc9deda88fb2b4 |