Skip to content

Commit 0a4c098

Browse files
committed
Add sanity checks for schema to allow only a single query, mutation, subscription in schema
Related GraphQL-js commit: graphql/graphql-js@ffe76c5
1 parent a91c06f commit 0a4c098

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

graphql/utils/build_ast_schema.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,16 @@ def build_ast_schema(document):
8282
for operation_type in schema_def.operation_types:
8383
type_name = operation_type.type.name.value
8484
if operation_type.operation == 'query':
85+
if query_type_name:
86+
raise Exception('Must provide only one query type in schema.')
8587
query_type_name = type_name
8688
elif operation_type.operation == 'mutation':
89+
if mutation_type_name:
90+
raise Exception('Must provide only one mutation type in schema.')
8791
mutation_type_name = type_name
8892
elif operation_type.operation == 'subscription':
93+
if subscription_type_name:
94+
raise Exception('Must provide only one subscription type in schema.')
8995
subscription_type_name = type_name
9096

9197
if not query_type_name:

graphql/utils/tests/test_build_ast_schema.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,74 @@ def test_requires_a_query_type():
430430
assert 'Must provide schema definition with query type.' == str(excinfo.value)
431431

432432

433+
def test_allows_only_a_single_query_type():
434+
body = '''
435+
schema {
436+
query: Hello
437+
query: Yellow
438+
}
439+
440+
type Hello {
441+
bar: Bar
442+
}
443+
444+
type Yellow {
445+
isColor: Boolean
446+
}
447+
'''
448+
doc = parse(body)
449+
with raises(Exception) as excinfo:
450+
build_ast_schema(doc)
451+
452+
assert 'Must provide only one query type in schema.' == str(excinfo.value)
453+
454+
455+
def test_allows_only_a_single_mutation_type():
456+
body = '''
457+
schema {
458+
query: Hello
459+
mutation: Hello
460+
mutation: Yellow
461+
}
462+
463+
type Hello {
464+
bar: Bar
465+
}
466+
467+
type Yellow {
468+
isColor: Boolean
469+
}
470+
'''
471+
doc = parse(body)
472+
with raises(Exception) as excinfo:
473+
build_ast_schema(doc)
474+
475+
assert 'Must provide only one mutation type in schema.' == str(excinfo.value)
476+
477+
478+
def test_allows_only_a_single_subscription_type():
479+
body = '''
480+
schema {
481+
query: Hello
482+
subscription: Hello
483+
subscription: Yellow
484+
}
485+
486+
type Hello {
487+
bar: Bar
488+
}
489+
490+
type Yellow {
491+
isColor: Boolean
492+
}
493+
'''
494+
doc = parse(body)
495+
with raises(Exception) as excinfo:
496+
build_ast_schema(doc)
497+
498+
assert 'Must provide only one subscription type in schema.' == str(excinfo.value)
499+
500+
433501
def test_unknown_type_referenced():
434502
body = '''
435503
schema {

0 commit comments

Comments
 (0)