Skip to content

Commit c6236f9

Browse files
author
Ernesto Perez Amigo
committed
Fixed bug on subscriptions when not specified any field in "data" parameter to bean return on notification message.
1 parent 7d15655 commit c6236f9

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ subscription{
445445

446446
## Change Log:
447447

448+
#### v0.1.0-alpha3:
449+
1. Fixed bug on subscriptions when not specified any field in "data" parameter to bean return on notification message.
450+
448451
#### v0.1.0-alpha2:
449452
1. Fixed bug when subscribing to a given action (create, update pr delete).
450453
2. Added intuitive and simple web tool to test notifications of graphene-django-extras subscription.

README.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,17 @@ For unsubscribe you must send a graphql subscription request like this:
478478
}
479479
480480
481-
NOTE: Each time than the Graphql server restart, you must to reestablish the websocket's connection and resend the subscription graphql request with a new websocket connection id.
481+
*NOTE*: Each time than the Graphql server restart, you must to reestablish the websocket's connection and resend the subscription graphql request with a new websocket connection id.
482482

483483

484484
Change Log:
485485
-----------
486486

487+
**************
488+
v0.1.0-alpha3:
489+
**************
490+
1. Fixed bug on subscriptions when not specified any field in "data" parameter to bean return on notification message.
491+
487492
**************
488493
v0.1.0-alpha2:
489494
**************

graphene_django_extras/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .types import DjangoObjectType, DjangoInputObjectType, DjangoListObjectType
99
from .subscriptions import *
1010

11-
VERSION = (0, 1, 0, 'alpha', '2')
11+
VERSION = (0, 1, 0, 'alpha', '3')
1212

1313
__version__ = get_version(VERSION)
1414

graphene_django_extras/subscriptions/mixins.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def serialize(self, instance, action):
3939

4040
def serialize_data(self, instance):
4141
data = self.get_serializer(instance).data
42-
if hasattr(self.get_serializer_class().Meta, 'only_fields'):
43-
data = {k: v for k, v in data.items() if k in self.get_serializer_class().Meta.only_fields}
42+
only_fields = hasattr(self.get_serializer_class().Meta, 'only_fields')
43+
if only_fields:
44+
if self.get_serializer_class().Meta.only_fields != ['all_fields']:
45+
data = {k: v for k, v in data.items() if k in self.get_serializer_class().Meta.only_fields}
4446
return data

graphene_django_extras/subscriptions/subscription.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,20 @@ def __init_subclass_with_meta__(cls, mutation_class=None, stream=None, queryset=
7575
_meta.serializer_class = copy.deepcopy(mutation_class._meta.serializer_class)
7676
_meta.mutation_class = mutation_class
7777

78-
model_fields = Enum('{}Fields'.format(mutation_class._meta.model.__name__),
79-
[(to_snake_case(field.strip('_')).upper(), to_snake_case(field))
80-
for field in _meta.serializer_class.Meta.fields])
78+
serializer_fields = [(to_snake_case(field.strip('_')).upper(), to_snake_case(field))
79+
for field in _meta.serializer_class.Meta.fields]
80+
model_fields_enum = Enum('{}Fields'.format(mutation_class._meta.model.__name__),
81+
serializer_fields + [('ALL_FIELDS', 'all_fields')],
82+
description=_('Desired {} fields in subscription\'s notification. You can specify '
83+
'"all_fields" value.').format(mutation_class._meta.model.__name__))
8184

8285
arguments = {
8386
'channel_id': Argument(String, required=True, description=_('Channel connection identification')),
8487
'action': Argument(ActionSubscriptionEnum, required=True,
8588
description=_('Action to subscribe or to unsubscribe: (create, update or delete)')),
8689
'operation': Argument(OperationSubscriptionEnum, required=True, description=_('Operation to do')),
8790
'id': Argument(ID, description=_('ID value that the object to watch must have')),
88-
'data': List(model_fields, required=False,
89-
description=_('Desired {} fields in subscription\'s notification')
90-
.format(mutation_class._meta.model.__name__))
91+
'data': List(model_fields_enum, required=False)
9192
}
9293

9394
_meta.arguments = arguments
@@ -113,7 +114,7 @@ def subscription_resolver(cls, root, info, **kwargs):
113114
# Manage the subscribe or unsubscribe operations
114115
action = kwargs.get('action')
115116
operation = kwargs.get('operation')
116-
data = kwargs.get('data', [])
117+
data = kwargs.get('data', None)
117118
obj_id = kwargs.get('id', None)
118119

119120
response = {
@@ -142,7 +143,8 @@ def subscription_resolver(cls, root, info, **kwargs):
142143
elif operation == 'unsubscribe':
143144
Group(group_name).discard(channel)
144145

145-
setattr(cls._meta.serializer_class.Meta, 'only_fields', data)
146+
if data is not None:
147+
setattr(cls._meta.serializer_class.Meta, 'only_fields', data)
146148

147149
response.update(dict(ok=True, error=None))
148150
channel.send({'text': json.dumps(response)})

0 commit comments

Comments
 (0)