Skip to content

Commit

Permalink
Slight change in subscribe_fields() (graphql-python#242)
Browse files Browse the repository at this point in the history
Allow merging multiple fields into one result.
  • Loading branch information
Cito committed Jan 24, 2020
1 parent baf5fe4 commit 42dbced
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
3 changes: 1 addition & 2 deletions graphql/execution/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,9 @@ def catch_error(error):
observable = result.catch_exception(catch_error).map(
lambda data: map_result({response_name: data})
)
return observable
observables.append(observable)

return Observable.merge(observables)
return observables[0] if len(observables) == 1 else Observable.merge(observables)


def resolve_field(
Expand Down
18 changes: 9 additions & 9 deletions graphql/execution/tests/test_subscribe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# type: ignore
from collections import OrderedDict, namedtuple
from rx import Observable, Observer
from rx.subjects import Subject
Expand Down Expand Up @@ -142,6 +141,7 @@ class inbox(object):
)
]

@staticmethod
def importantEmail():
return stream

Expand Down Expand Up @@ -209,10 +209,11 @@ def test_accepts_multiple_subscription_fields_defined_in_schema():
message="Tests are good",
unread=True,
)
l = []
stream.subscribe(l.append)
inbox = []
stream.subscribe(inbox.append)
send_important_email(email)
assert l[0][0] == email
assert len(inbox) == 1
assert inbox[0][0] == email


def test_accepts_type_definition_with_sync_subscribe_function():
Expand Down Expand Up @@ -241,11 +242,11 @@ def test_accepts_type_definition_with_sync_subscribe_function():
message="Tests are good",
unread=True,
)
l = []
subscription.subscribe(l.append)
inbox = []
subscription.subscribe(inbox.append)
send_important_email(email)

assert l # [0].data == {'importantEmail': None}
assert len(inbox) == 1
assert inbox[0].data == {"importantEmail": None}


def test_throws_an_error_if_subscribe_does_not_return_an_iterator():
Expand Down Expand Up @@ -422,6 +423,5 @@ def test_event_order_is_correct_for_multiple_publishes():
}

assert len(payload) == 2
print(payload)
assert payload[0].data == expected_payload1
assert payload[1].data == expected_payload2

0 comments on commit 42dbced

Please sign in to comment.