Skip to content

Commit 00b69c9

Browse files
committed
Initial subscription commit.
Implement changes in graphql/graphql-js@8a589f6
1 parent 03e00bd commit 00b69c9

File tree

8 files changed

+55
-7
lines changed

8 files changed

+55
-7
lines changed

graphql/core/execution/base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,17 @@ def get_operation_root_type(schema, operation):
133133

134134
return mutation_type
135135

136+
elif op == 'subscription':
137+
subscription_type = schema.get_subscription_type()
138+
139+
if not subscription_type:
140+
raise GraphQLError(
141+
'Schema is not configured for subscriptions',
142+
[operation]
143+
)
144+
136145
raise GraphQLError(
137-
'Can only execute queries and mutations',
146+
'Can only execute queries, mutations and subscriptions',
138147
[operation]
139148
)
140149

graphql/core/type/introspection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
type=__Type,
4040
resolver=lambda schema, *_: schema.get_mutation_type(),
4141
)),
42+
('subscriptionType', GraphQLField(
43+
description='If this server support subscription, the type '
44+
'that subscription operations will be rooted at.',
45+
type=__Type,
46+
resolver=lambda schema, *_: schema.get_subscription_type(),
47+
)),
4248
('directives', GraphQLField(
4349
description='A list of all directives supported by this server.',
4450
type=GraphQLNonNull(GraphQLList(GraphQLNonNull(__Directive))),

graphql/core/type/schema.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@ class GraphQLSchema(object):
2424
mutation=MyAppMutationRootType
2525
)
2626
"""
27-
__slots__ = '_query', '_mutation', '_type_map', '_directives'
27+
__slots__ = '_query', '_mutation', '_subscription', '_type_map', '_directives',
2828

29-
def __init__(self, query, mutation=None):
29+
def __init__(self, query, mutation=None, subscription=None):
3030
assert isinstance(query, GraphQLObjectType), 'Schema query must be Object Type but got: {}.'.format(query)
3131
if mutation:
3232
assert isinstance(mutation, GraphQLObjectType), \
3333
'Schema mutation must be Object Type but got: {}.'.format(mutation)
3434

35+
if subscription:
36+
assert isinstance(subscription, GraphQLObjectType), \
37+
'Schema subscription must be Object Type but got: {}.'.format(subscription)
38+
3539
self._query = query
3640
self._mutation = mutation
41+
self._subscription = subscription
3742
self._type_map = self._build_type_map()
3843
self._directives = None
3944

@@ -48,6 +53,9 @@ def get_query_type(self):
4853
def get_mutation_type(self):
4954
return self._mutation
5055

56+
def get_subscription_type(self):
57+
return self._subscription
58+
5159
def get_type_map(self):
5260
return self._type_map
5361

@@ -72,7 +80,8 @@ def get_directive(self, name):
7280

7381
def _build_type_map(self):
7482
type_map = OrderedDict()
75-
for type in (self.get_query_type(), self.get_mutation_type(), IntrospectionSchema):
83+
types = (self.get_query_type(), self.get_mutation_type(), self.get_subscription_type(), IntrospectionSchema)
84+
for type in types:
7685
type_map = type_map_reducer(type_map, type)
7786

7887
return type_map

graphql/core/utils/build_ast_schema.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _get_inner_type_name(type_ast):
4545
_none = lambda *_: None
4646

4747

48-
def build_ast_schema(document, query_type_name, mutation_type_name=None):
48+
def build_ast_schema(document, query_type_name, mutation_type_name=None, subscription_type_name=None):
4949
assert isinstance(document, ast.Document), 'must pass in Document ast.'
5050
assert query_type_name, 'must pass in query type'
5151

@@ -58,6 +58,9 @@ def build_ast_schema(document, query_type_name, mutation_type_name=None):
5858
if mutation_type_name and mutation_type_name not in ast_map:
5959
raise Exception('Specified mutation type {} not found in document.'.format(mutation_type_name))
6060

61+
if subscription_type_name and subscription_type_name not in ast_map:
62+
raise Exception('Specified subscription type {} not found in document.'.format(subscription_type_name))
63+
6164
inner_type_map = OrderedDict([
6265
('String', GraphQLString),
6366
('Int', GraphQLInt),
@@ -175,4 +178,7 @@ def make_schema_def(definition):
175178
if mutation_type_name:
176179
schema_kwargs['mutation'] = produce_type_def(ast_map[mutation_type_name])
177180

181+
if subscription_type_name:
182+
schema_kwargs['subscription'] = produce_type_def(ast_map[subscription_type_name])
183+
178184
return GraphQLSchema(**schema_kwargs)

graphql/core/utils/build_client_schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,7 @@ def build_input_value_def_map(input_value_introspection, argument_type):
204204

205205
query_type = get_type(schema_introspection['queryType'])
206206
mutation_type = get_type(schema_introspection['mutationType']) if schema_introspection.get('mutationType') else None
207+
subscription_type = get_type(schema_introspection['subscriptionType']) if \
208+
schema_introspection.get('subscriptionType') else None
207209

208-
return GraphQLSchema(query_type, mutation_type)
210+
return GraphQLSchema(query=query_type, mutation=mutation_type, subscription=subscription_type)

graphql/core/utils/introspection_query.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
__schema {
44
queryType { name }
55
mutationType { name }
6+
subscriptionType { name }
67
types {
78
...FullType
89
}

tests/core_type/test_introspection.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def test_executes_an_introspection_query():
5353
'onFragment': True,
5454
'onOperation': False}],
5555
'mutationType': None,
56+
'subscriptionType': None,
5657
'queryType': {'name': u'QueryRoot'},
5758
'types': [{'description': None,
5859
'enumValues': None,
@@ -111,6 +112,14 @@ def test_executes_an_introspection_query():
111112
'type': {'kind': 'OBJECT',
112113
'name': u'__Type',
113114
'ofType': None}},
115+
{'args': [],
116+
'deprecationReason': None,
117+
'description': u'If this server support subscription, the type that subscription operations will be rooted at.',
118+
'isDeprecated': False,
119+
'name': u'subscriptionType',
120+
'type': {'kind': 'OBJECT',
121+
'name': u'__Type',
122+
'ofType': None}},
114123
{'args': [],
115124
'deprecationReason': None,
116125
'description': u'A list of all directives supported by this server.',
@@ -744,9 +753,14 @@ def test_exposes_descriptions_on_types_and_fields():
744753
},
745754
{
746755
'name': 'mutationType',
747-
'description': 'If this server supports mutation, the type that ' +
756+
'description': 'If this server supports mutation, the type that '
748757
'mutation operations will be rooted at.'
749758
},
759+
{
760+
'name': 'subscriptionType',
761+
'description': 'If this server support subscription, the type '
762+
'that subscription operations will be rooted at.'
763+
},
750764
{
751765
'name': 'directives',
752766
'description': 'A list of all directives supported by this server.'

tests/core_utils/test_schema_printer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ def test_prints_introspection_schema():
499499
types: [__Type!]!
500500
queryType: __Type!
501501
mutationType: __Type
502+
subscriptionType: __Type
502503
directives: [__Directive!]!
503504
}
504505

0 commit comments

Comments
 (0)