From b91ae4b0fb8cee3265967953464f46041ebd76bd Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2015 08:37:15 -0800 Subject: [PATCH] Added subscription support. Fixed #35 --- README.md | 2 +- README.rst | 4 +--- graphene/core/schema.py | 9 +++++--- graphene/core/tests/test_subscription.py | 29 ++++++++++++++++++++++++ setup.py | 2 +- tox.ini | 2 +- 6 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 graphene/core/tests/test_subscription.py diff --git a/README.md b/README.md index ba255c61..115d4a14 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Graphene is a Python library for building GraphQL schemas/types fast and easily. - **Django:** Automatic *Django model* mapping to Graphene Types. Check a fully working [Django](http://github.com/graphql-python/swapi-graphene) implementation -*What is supported in this Python version?* **Everything**: Interfaces, ObjectTypes, Mutations, Scalars, Unions and Relay (Nodes, Connections and Mutations). +*What is supported in this Python version?* **Everything**: Interfaces, ObjectTypes, Scalars, Unions and Relay (Nodes, Connections), in addition to queries, mutations and subscriptions. ## Installation diff --git a/README.rst b/README.rst index d0dc86a0..b8afcf8d 100644 --- a/README.rst +++ b/README.rst @@ -12,9 +12,7 @@ easily. `Django `__ implementation -*What is supported in this Python version?* **Everything**: Interfaces, -ObjectTypes, Mutations, Scalars, Unions and Relay (Nodes, Connections -and Mutations). +*What is supported in this Python version?* **Everything**: Interfaces, ObjectTypes, Scalars, Unions and Relay (Nodes, Connections and Mutations), in addition to queries, mutations and subscriptions. Installation ------------ diff --git a/graphene/core/schema.py b/graphene/core/schema.py index 5846cf2f..eef79678 100644 --- a/graphene/core/schema.py +++ b/graphene/core/schema.py @@ -24,12 +24,13 @@ class GraphQLSchema(_GraphQLSchema): class Schema(object): _executor = None - def __init__(self, query=None, mutation=None, + def __init__(self, query=None, mutation=None, subscription=None, name='Schema', executor=None): self._types_names = {} self._types = {} self.mutation = mutation self.query = query + self.subscription = subscription self.name = name self.executor = executor signals.init_schema.send(self) @@ -70,8 +71,10 @@ class Schema(object): if not self.query: raise Exception('You have to define a base query type') return GraphQLSchema( - self, query=self.T(self.query), - mutation=self.T(self.mutation)) + self, + query=self.T(self.query), + mutation=self.T(self.mutation), + subscription=self.T(self.subscription)) def register(self, object_type): type_name = object_type._meta.type_name diff --git a/graphene/core/tests/test_subscription.py b/graphene/core/tests/test_subscription.py new file mode 100644 index 00000000..8f46b04d --- /dev/null +++ b/graphene/core/tests/test_subscription.py @@ -0,0 +1,29 @@ +import graphene + + +class Query(graphene.ObjectType): + base = graphene.String() + + +class Subscription(graphene.ObjectType): + subscribe_to_foo = graphene.Boolean(id=graphene.Int()) + + def resolve_subscribe_to_foo(self, args, info): + return args.get('id') == 1 + + +schema = graphene.Schema(query=Query, subscription=Subscription) + + +def test_execute_subscription(): + query = ''' + subscription { + subscribeToFoo(id: 1) + } + ''' + expected = { + 'subscribeToFoo': True + } + result = schema.execute(query) + assert not result.errors + assert result.data == expected diff --git a/setup.py b/setup.py index 77b2e296..6b0d1458 100644 --- a/setup.py +++ b/setup.py @@ -56,7 +56,7 @@ setup( install_requires=[ 'six>=1.10.0', 'blinker', - 'graphql-core==0.4.7b2', + 'graphql-core==0.4.9', 'graphql-relay==0.3.3' ], tests_require=[ diff --git a/tox.ini b/tox.ini index 45cf7fdd..a3a5b251 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ deps= django>=1.8.0,<1.9 pytest-django graphql-django-view>=1.0.0 - graphql-core==0.4.7b2 + graphql-core==0.4.9 graphql-relay==0.3.3 six blinker