Added subscription support. Fixed #35

This commit is contained in:
Syrus Akbary 2015-11-19 08:37:15 -08:00
parent 4a591354f1
commit b91ae4b0fb
6 changed files with 39 additions and 9 deletions

View File

@ -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 - **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 ## Installation

View File

@ -12,9 +12,7 @@ easily.
`Django <http://github.com/graphql-python/swapi-graphene>`__ `Django <http://github.com/graphql-python/swapi-graphene>`__
implementation implementation
*What is supported in this Python version?* **Everything**: Interfaces, *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.
ObjectTypes, Mutations, Scalars, Unions and Relay (Nodes, Connections
and Mutations).
Installation Installation
------------ ------------

View File

@ -24,12 +24,13 @@ class GraphQLSchema(_GraphQLSchema):
class Schema(object): class Schema(object):
_executor = None _executor = None
def __init__(self, query=None, mutation=None, def __init__(self, query=None, mutation=None, subscription=None,
name='Schema', executor=None): name='Schema', executor=None):
self._types_names = {} self._types_names = {}
self._types = {} self._types = {}
self.mutation = mutation self.mutation = mutation
self.query = query self.query = query
self.subscription = subscription
self.name = name self.name = name
self.executor = executor self.executor = executor
signals.init_schema.send(self) signals.init_schema.send(self)
@ -70,8 +71,10 @@ class Schema(object):
if not self.query: if not self.query:
raise Exception('You have to define a base query type') raise Exception('You have to define a base query type')
return GraphQLSchema( return GraphQLSchema(
self, query=self.T(self.query), self,
mutation=self.T(self.mutation)) query=self.T(self.query),
mutation=self.T(self.mutation),
subscription=self.T(self.subscription))
def register(self, object_type): def register(self, object_type):
type_name = object_type._meta.type_name type_name = object_type._meta.type_name

View File

@ -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

View File

@ -56,7 +56,7 @@ setup(
install_requires=[ install_requires=[
'six>=1.10.0', 'six>=1.10.0',
'blinker', 'blinker',
'graphql-core==0.4.7b2', 'graphql-core==0.4.9',
'graphql-relay==0.3.3' 'graphql-relay==0.3.3'
], ],
tests_require=[ tests_require=[

View File

@ -8,7 +8,7 @@ deps=
django>=1.8.0,<1.9 django>=1.8.0,<1.9
pytest-django pytest-django
graphql-django-view>=1.0.0 graphql-django-view>=1.0.0
graphql-core==0.4.7b2 graphql-core==0.4.9
graphql-relay==0.3.3 graphql-relay==0.3.3
six six
blinker blinker