Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full Serializer Mutation with DRF permissions #136

Open
wants to merge 67 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
fcb05f7
created DRFSerializerMutation solely based on serializers mutation ar…
eadwinCode May 9, 2020
41f0465
updated ReadME docs and decorators functions doc
eadwinCode May 9, 2020
adc109a
Update README.md
eadwinCode May 9, 2020
4871374
fixed missing order_by implementation in graphene-django lib.
eadwinCode May 9, 2020
266f036
Merge branch 'master' of https://github.com/eadwinCode/graphene-djang…
eadwinCode May 9, 2020
81ce02d
fixed missing order_by implementation in graphene-django lib.
eadwinCode May 9, 2020
b33c870
Update README.md
eadwinCode May 10, 2020
8d83c43
fixed Type reuse for serializers and Models mutations
eadwinCode May 10, 2020
95827bb
Merge branch 'master' of https://github.com/eadwinCode/graphene-djang…
eadwinCode May 10, 2020
9639270
fixed missing order_by implementation in graphene-django lib.
eadwinCode May 10, 2020
38180c5
added helpful checks to mixins and Model mutations
eadwinCode May 11, 2020
89cbf5d
Added mutation output coustomisation and option to destory object as …
eadwinCode May 22, 2020
85cee77
enhance error display for ModelMutations
eadwinCode May 25, 2020
56b5dff
enhance error display for ModelMutations
eadwinCode May 25, 2020
28201aa
fixed deleteMutation typo
eadwinCode May 25, 2020
63ec4c4
fixed paginationListField automatically adding id field to filter
eadwinCode May 25, 2020
e75e82f
fixed paginationListField automatically adding id field to filter
eadwinCode May 27, 2020
b72ec88
fixed paginationListField automatically adding id field to filter
eadwinCode May 27, 2020
41eb047
fixing DjangoListObjectField not queryset result
eadwinCode May 27, 2020
1a8daaa
fixing DjangoListObjectField not queryset result
eadwinCode May 27, 2020
5c198f4
Update decorators.py
eadwinCode May 27, 2020
d9da620
fixed decorators mixins
eadwinCode May 27, 2020
a63733a
Update decorators.py
eadwinCode May 27, 2020
5131b70
Update decorators.py
eadwinCode May 28, 2020
c9d6551
Update mixins.py
eadwinCode May 28, 2020
fadf648
fixed serializer enum fields types duplations
eadwinCode May 29, 2020
93f7da6
fixed serializer enum fields types duplations
eadwinCode May 29, 2020
78d586e
fixed serializer enum fields types duplations
eadwinCode May 29, 2020
0bd715c
fixed serializer enum fields types duplations
eadwinCode May 29, 2020
4a13a44
fixed createModelMixin and updateModelMixin error handling
eadwinCode May 30, 2020
44bbc85
fixed all mutation lookup fields
eadwinCode May 30, 2020
10a8ea1
fixed all mutation lookup fields
eadwinCode May 30, 2020
f4c954a
fixed all mutation lookup fields
eadwinCode May 30, 2020
b2920cf
fixed all mutation lookup fields
eadwinCode May 30, 2020
5438571
fixed ordering and pagelimit for graphene_django djangofilterfield an…
eadwinCode Jun 1, 2020
475c62a
fixed query_factor for all djangolist djangolistfilter and list pagin…
eadwinCode Jun 1, 2020
f6e108a
renamed input fields types variable to more intuitive name
eadwinCode Jun 1, 2020
4d164c7
added getobject mixin for node types
eadwinCode Jun 2, 2020
cd357ad
added getobject mixin for node types
eadwinCode Jun 2, 2020
94e37c3
pagination default settings
eadwinCode Jun 2, 2020
9f4586f
pagination default settings
eadwinCode Jun 2, 2020
b3badd9
pagination default settings
eadwinCode Jun 2, 2020
7537e4d
made provision for manual resolving of list fields types
eadwinCode Jun 2, 2020
3c55b51
made provision for manual resolving of list fields types
eadwinCode Jun 2, 2020
db17613
Added a more robust query builder
eadwinCode Jun 3, 2020
b22abad
method refactoring
eadwinCode Jun 4, 2020
41fbc28
method refactoring
eadwinCode Jun 4, 2020
9be7d15
method refactoring
eadwinCode Jun 4, 2020
21e6e58
method refactoring
eadwinCode Jun 4, 2020
ced7a46
method refactoring
eadwinCode Jun 4, 2020
935eafe
method refactoring
eadwinCode Jun 4, 2020
94a7240
method refactoring
eadwinCode Jun 5, 2020
ede57d3
method refactoring
eadwinCode Jun 5, 2020
c9a86f4
method refactoring
eadwinCode Jun 5, 2020
eb20055
method refactoring
eadwinCode Jun 5, 2020
057532b
method refactoring
eadwinCode Jun 5, 2020
0e307b1
method refactoring finale
eadwinCode Jun 6, 2020
2455377
added test for all features
eadwinCode Jun 7, 2020
a0c2b36
updated Readme files
eadwinCode Jun 7, 2020
71418dc
fixed conflicts
eadwinCode Jun 7, 2020
4248cad
fixed conflicts
eadwinCode Jun 7, 2020
8c02b18
fixed conflicts
eadwinCode Jun 7, 2020
024101d
Update pyproject.toml
eadwinCode Jun 7, 2020
fd91de6
fixed library versioning
eadwinCode Jun 7, 2020
aea7ed5
Update mutation.py
eadwinCode Jul 14, 2020
8675410
Update mutation.py
eadwinCode Jul 14, 2020
2d0f2b8
Added more flexibility to field list types
eadwinCode Jul 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 81 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This package adds some extra functionalities to graphene-django to facilitate th
1. Allow pagination and filtering on Queries.
2. Allow defining DjangoRestFramework serializers based on Mutations.
3. Allow using Directives on Queries and Fragments.
4. Allow defining Permissions for mutations and Queries

**NOTE:** Subscription support was moved to [graphene-django-subscriptions](https://github.com/eamigo86/graphene-django-subscriptions).

Expand All @@ -22,26 +23,40 @@ This package adds some extra functionalities to graphene-django to facilitate th
For installing graphene-django-extras, just run this command in your shell:

```
pip install graphene-django-extras
pip install git+https://github.com/eadwinCode/graphene-django-extras.git
```

## Documentation:

### Extra functionalities:
**Fields:**
**Fields: (*Now has permissions*)**
1. DjangoObjectField
2. DjangoFilterListField
3. DjangoFilterPaginateListField
4. DjangoListObjectField (*Recommended for Queries definition*)

**Mutations:**
1. DjangoSerializerMutation (*Recommended for Mutations definition*)
2. *DRFSerializerMutation (*Recommended for Mutations definition Strictly for Serializers*)
3. *DjangoModelMutation (*Recommended for Mutations definition Strictly for Django Models*)
4. *UpdateSerializerMutation (*Update Mutation definition with Serializer class*)
5. *DeleteSerializerMutation (*Delete Mutation definition with Serializer class*)
6. *CreateSerializerMutation (*Create Mutation definition with Serializer class*)
7. *CreateModelMutation (*Create Mutation definition with Django Model*)
8. *UpdateModelMutation (*Update Mutation definition with Django Model*)
9. *DeleteModelMutation (*Delete Mutation definition with Django Model*)

**Types:**
1. DjangoListObjectType (*Recommended for Types definition*)
2. DjangoInputObjectType
3. DjangoSerializerType (*Recommended for quick queries and mutations definitions*)

**Decorators: For resolver functions**
1. login_required (*Checks for authentication in request context*)
2. permission_required (*Checks for 'access permissions' not object permissions*)
3. is_super_user_required(*Checks if authenticated user is a superuser*)
4. is_staff_required(*Checks if authenticated user is a staff*)

**Paginations:**
1. LimitOffsetGraphqlPagination
2. PageGraphqlPagination
Expand Down Expand Up @@ -126,20 +141,53 @@ class UserInput(DjangoInputObjectType):

```python
import graphene
from graphene_django_extras import DjangoSerializerMutation
from graphene_django_extras.rest_framework import DjangoModelMutation, DRFSerializerMutation

from .serializers import UserSerializer
from .serializers import UserSerializer, UpdateUserSerializer
from .types import UserType
from .input_types import UserInputType
from .models import User


class UserSerializerMutation(DjangoSerializerMutation):
class UserSerializerMutation(DRFSerializerMutation):
"""
DjangoSerializerMutation auto implement Create, Delete and Update functions
for more customisation use CreateSerializerMutation, UpdateSerializerMutation,
DeleteSerializerMutation
"""

permission_classes = []

mutation_serializers = { # optional if you want to be specific
'create': UserSerializer,
'update': UpdateUserSerializer
}

class Meta:
description = " DRF serializer based Mutation for Users "
serializer_class = UserSerializer
serializer_class = UserSerializer # compulsory global serializer_class


class UserModelMutation(DjangoModelMutation):
"""
DjangoModelMutation auto implement Create, Delete and Update functions
for more customisation use CreateModelMutation, UpdateModelMutation,
DeleteModelMutation
"""
permission_classes = []

create_fields = dict(
only_fields=('username','password', 'email'),
)

update_fields = dict(
exclude_fields=('username', 'password', 'email')
)

class Meta:
description = "Model based Mutation for Users "
model = User
only_fields = ('username', 'password', 'email' )


class UserMutation(graphene.Mutation):
Expand All @@ -151,7 +199,7 @@ class UserMutation(graphene.Mutation):
user = graphene.Field(UserType, required=False)

class Arguments:
new_user = graphene.Argument(UserInput)
new_user = graphene.Argument(UserInputType)

class Meta:
description = " Graphene traditional mutation for Users "
Expand All @@ -168,14 +216,18 @@ import graphene
from graphene_django_extras import DjangoObjectField, DjangoListObjectField, DjangoFilterPaginateListField,
DjangoFilterListField, LimitOffsetGraphqlPagination
from .types import UserType, UserListType, UserModelType
from .mutations import UserMutation, UserSerializerMutation
from .mutations import UserMutation, UserSerializerMutation, UserModelMutation


class Queries(graphene.ObjectType):
# Possible User list queries definitions
users = DjangoListObjectField(UserListType, description='All Users query')
users1 = DjangoFilterPaginateListField(UserType, pagination=LimitOffsetGraphqlPagination())
users2 = DjangoFilterListField(UserType)
users = DjangoListObjectField(
UserListType, description='All Users query', permission_classes=[IsAuthenticated,]
)
users1 = DjangoFilterPaginateListField(
UserType, pagination=LimitOffsetGraphqlPagination(), permission_classes=[IsAuthenticated,]
)
users2 = DjangoFilterListField(UserType, permission_classes=[IsAuthenticated,])
users3 = DjangoListObjectField(UserListType, filterset_class=UserFilter, description='All Users query')

# Defining a query for a single user
Expand Down Expand Up @@ -203,6 +255,10 @@ class Mutations(graphene.ObjectType):
user_create = UserSerializerMutation.CreateField(deprecation_reason='Some one deprecation message')
user_delete = UserSerializerMutation.DeleteField()
user_update = UserSerializerMutation.UpdateField()

user_model_create = UserModelMutation.CreateField(deprecation_reason='Some one deprecation message')
user_model_delete = UserModelMutation.DeleteField()
user_model_update = UserModelMutation.UpdateField()

# Exist two ways to define mutations with DjangoSerializerType
user_create1, user_delete1, user_update1 = UserModelType.MutationFields(
Expand Down Expand Up @@ -493,6 +549,20 @@ You can use this shortcuts too:


## Change Log:
#### v0.5:
1. List and Paginated fields can be resolved manually
2. Fixed fields compatibility issues graphene_django DjangoObjectType
3. Use graphene_django DjangoObjectType or graphene_django_extra DjangoObjectType
4. Base class for ListFields
5. Improved query builder for ListFields
6. DRF Permission implementation on ListFields

#### v0.4.8-beta.1:
1. Added DRFSerializerMutation, another Mutation to properly manager mutation from serializer class
2. Added Permission to DRFSerializerMutations and DjangoModelMutations
3. Improved on ListField classes for easy customisation and
4. Added permission to all ListField components

#### v0.4.8:
1. Upgrade graphene-django dependency to version == 2.6.0.

Expand Down
93 changes: 82 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This package add some extra functionalities to **graphene-django** to facilitate
1. Allows pagination and filtering on Queries.
2. Allows to define DjangoRestFramework serializers based Mutations.
3. Allows use Directives on Queries and Fragments.
4. Allow defining Permissions for mutations and Queries

**NOTE:** Subscription support was moved to `graphene-django-subscriptions <https://github.com/eamigo86/graphene-django-subscriptions>`_

Expand All @@ -17,29 +18,43 @@ For installing graphene-django-extras, just run this command in your shell:

.. code:: python

pip install "graphene-django-extras"
pip install "git+https://github.com/eadwinCode/graphene-django-extras.git"

Documentation:
--------------

**********************
Extra functionalities:
**********************
**Fields:**
**Fields(Now has permissions):**
1. DjangoObjectField
2. DjangoFilterListField
3. DjangoFilterPaginateListField
4. DjangoListObjectField (*Recommended for Queries definition*)

**Mutations:**
1. DjangoSerializerMutation (*Recommended for Mutations definition*)
2. *DRFSerializerMutation (*Recommended for Mutations definition Strictly for Serializers*)
3. *DjangoModelMutation (*Recommended for Mutations definition Strictly for Django Models*)
4. *UpdateSerializerMutation (*Update Mutation definition with Serializer class*)
5. *DeleteSerializerMutation (*Delete Mutation definition with Serializer class*)
6. *CreateSerializerMutation (*Create Mutation definition with Serializer class*)
7. *CreateModelMutation (*Create Mutation definition with Django Model*)
8. *UpdateModelMutation (*Update Mutation definition with Django Model*)
9. *DeleteModelMutation (*Delete Mutation definition with Django Model*)

**Types:**
1. DjangoListObjectType (*Recommended for Types definition*)
2. DjangoInputObjectType
3. DjangoSerializerType (*Recommended for quick queries and mutations definitions*)

**Paginations:**
**Decorators: For resolver functions**
1. login_required (*Checks for authentication in request context*)
2. permission_required (*Checks for 'access permissions' not object permissions*)
3. is_super_user_required(*Checks if authenticated user is a superuser*)
4. is_staff_required(*Checks if authenticated user is a staff*)

**Paginations:**
1. LimitOffsetGraphqlPagination
2. PageGraphqlPagination

Expand Down Expand Up @@ -136,19 +151,49 @@ You can define traditional mutations that use InputTypes or Mutations based on D
.. code:: python

import graphene
from .serializers import UserSerializer
from graphene_django_extras import DjangoSerializerMutation
from .serializers import UserSerializer, UpdateUserSerializer
from graphene_django_extras.rest_framework import DjangoModelMutation, DRFSerializerMutation
from .types import UserType
from .input_types import UserInputType


class UserSerializerMutation(DjangoSerializerMutation):
class UserSerializerMutation(DRFSerializerMutation):
"""
DjangoSerializerMutation auto implement Create, Delete and Update functions
for more customisation use CreateSerializerMutation, UpdateSerializerMutation, DeleteSerializerMutation
"""

permission_classes = []

mutation_serializers = { # optional if you want to be specific
'create': UserSerializer,
'update': UpdateUserSerializer
}

class Meta:
description = " DRF serializer based Mutation for Users "
serializer_class = UserSerializer
serializer_class = UserSerializer # compulsory global serializer_class


class UserModelMutation(DjangoModelMutation):
"""
DjangoModelMutation auto implement Create, Delete and Update functions
for more customisation use CreateModelMutation, UpdateModelMutation, DeleteModelMutation
"""
permission_classes = []

create_fields = dict(
only_fields=('username','password', 'email'),
)

update_fields = dict(
exclude_fields=('username', 'password', 'email')
)

class Meta:
description = "Model based Mutation for Users "
model = User
only_fields = ('username', 'password', 'email' )


class UserMutation(graphene.Mutation):
Expand Down Expand Up @@ -178,14 +223,18 @@ You can define traditional mutations that use InputTypes or Mutations based on D
import graphene
from graphene_django_extras import DjangoObjectField, DjangoListObjectField, DjangoFilterPaginateListField, DjangoFilterListField, LimitOffsetGraphqlPagination
from .types import UserType, UserListType, UserModelType
from .mutations import UserMutation, UserSerializerMutation
from .mutations import UserMutation, UserSerializerMutation, UserModelMutation


class Queries(graphene.ObjectType):
# Possible User list queries definitions
users = DjangoListObjectField(UserListType, description='All Users query')
users1 = DjangoFilterPaginateListField(UserType, pagination=LimitOffsetGraphqlPagination())
users2 = DjangoFilterListField(UserType)
users = DjangoListObjectField(
UserListType, description='All Users query', permission_classes=[IsAuthenticated,]
)
users1 = DjangoFilterPaginateListField(
UserType, pagination=LimitOffsetGraphqlPagination(), permission_classes=[IsAuthenticated,]
)
users2 = DjangoFilterListField(UserType, permission_classes=[IsAuthenticated,])
users3 = DjangoListObjectField(UserListType, filterset_class=UserFilter, description='All Users query')

# Defining a query for a single user
Expand All @@ -209,6 +258,10 @@ You can define traditional mutations that use InputTypes or Mutations based on D
user_delete = UserSerializerMutation.DeleteField()
user_update = UserSerializerMutation.UpdateField()

user_model_create = UserModelMutation.CreateField(deprecation_reason='Some one deprecation message')
user_model_delete = UserModelMutation.DeleteField()
user_model_update = UserModelMutation.UpdateField()

# Exist two ways to define mutations with DjangoSerializerType
user_create1, user_delete1, user_update1 = UserModelType.MutationFields(
description='Some description message for create, delete and update mutations',
Expand Down Expand Up @@ -511,6 +564,24 @@ You can use this shortcuts too:

Change Log:
-----------
*******
v0.5
*******
1. List and Paginated fields can be resolved manually
2. Fixed fields compatibility issues graphene_django DjangoObjectType
3. Use graphene_django DjangoObjectType or graphene_django_extra DjangoObjectType
4. Base class for ListFields
5. Improved query builder for ListFields
6. DRF Permission implementation on ListFields

*******
v0.4.8-beta.1
*******
1. Added DRFSerializerMutation, another Mutation to properly manager mutation from serializer class
2. Added Permission to DRFSerializerMutations and DjangoModelMutations
3. Improved on ListField classes for easy customisation and
4. Added permission to all ListField components

*******
v0.4.8:
*******
Expand Down
Empty file added __init__.py
Empty file.
2 changes: 2 additions & 0 deletions graphene_django_extras/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"DjangoListObjectField",
# MUTATIONS
"DjangoSerializerMutation",

# PAGINATIONS
"LimitOffsetGraphqlPagination",
"PageGraphqlPagination",
Expand All @@ -43,4 +44,5 @@
# DIRECTIVES
"all_directives",
"ExtraGraphQLDirectiveMiddleware",

)
Loading