Skip to content

Commit c646fca

Browse files
committed
RFC: Directive location: schema definition. Commit: graphql/graphql-js@0aa78f6
1 parent 6f040fd commit c646fca

File tree

10 files changed

+35
-6
lines changed

10 files changed

+35
-6
lines changed

graphql/language/ast.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,30 +845,34 @@ class TypeSystemDefinition(TypeDefinition):
845845

846846

847847
class SchemaDefinition(TypeSystemDefinition):
848-
__slots__ = ('loc', 'operation_types',)
848+
__slots__ = ('loc', 'directives', 'operation_types',)
849849
_fields = ('operation_types',)
850850

851-
def __init__(self, operation_types, loc=None):
851+
def __init__(self, operation_types, loc=None, directives=None):
852852
self.operation_types = operation_types
853853
self.loc = loc
854+
self.directives = directives
854855

855856
def __eq__(self, other):
856857
return (
857858
self is other or (
858859
isinstance(other, SchemaDefinition) and
859-
self.operation_types == other.operation_types
860+
self.operation_types == other.operation_types and
861+
self.directives == other.directives
860862
)
861863
)
862864

863865
def __repr__(self):
864866
return ('SchemaDefinition('
865867
'operation_types={self.operation_types!r}'
868+
', directives={self.directives!r}'
866869
')').format(self=self)
867870

868871
def __copy__(self):
869872
return type(self)(
870873
self.operation_types,
871-
self.loc
874+
self.loc,
875+
self.directives,
872876
)
873877

874878
def __hash__(self):

graphql/language/parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ def parse_type_system_definition(parser):
571571
def parse_schema_definition(parser):
572572
start = parser.token.start
573573
expect_keyword(parser, 'schema')
574+
directives = parse_directives(parser)
574575
operation_types = many(
575576
parser,
576577
TokenKind.BRACE_L,
@@ -579,6 +580,7 @@ def parse_schema_definition(parser):
579580
)
580581

581582
return ast.SchemaDefinition(
583+
directives=directives,
582584
operation_types=operation_types,
583585
loc=loc(parser, start)
584586
)

graphql/language/printer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ def leave_NonNullType(self, node, *args):
112112
# Type Definitions:
113113

114114
def leave_SchemaDefinition(self, node, *args):
115-
return 'schema ' + block(node.operation_types)
115+
return join([
116+
'schema',
117+
join(node.directives, ' '),
118+
block(node.operation_types),
119+
], ' ')
116120

117121
def leave_OperationTypeDefinition(self, node, *args):
118122
return '{}: {}'.format(node.operation, node.type)

graphql/language/visitor_meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
ast.ListType: ('type',),
3131
ast.NonNullType: ('type',),
3232

33-
ast.SchemaDefinition: ('operation_types',),
33+
ast.SchemaDefinition: ('directives', 'operation_types',),
3434
ast.OperationTypeDefinition: ('type',),
3535

3636
ast.ScalarTypeDefinition: ('name', 'directives',),

graphql/type/directives.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class DirectiveLocation(object):
1717
INLINE_FRAGMENT = 'INLINE_FRAGMENT'
1818

1919
# Schema Definitions
20+
SCHEMA = 'SCHEMA'
2021
SCALAR = 'SCALAR'
2122
OBJECT = 'OBJECT'
2223
FIELD_DEFINITION = 'FIELD_DEFINITION'

graphql/type/introspection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@
120120
DirectiveLocation.INLINE_FRAGMENT,
121121
description='Location adjacent to an inline fragment.'
122122
)),
123+
('SCHEMA', GraphQLEnumValue(
124+
DirectiveLocation.SCHEMA,
125+
description='Location adjacent to a schema definition.'
126+
)),
123127
('SCALAR', GraphQLEnumValue(
124128
DirectiveLocation.SCALAR,
125129
description='Location adjacent to a scalar definition.'

graphql/utils/tests/test_schema_printer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ def test_prints_introspection_schema():
559559
FRAGMENT_DEFINITION
560560
FRAGMENT_SPREAD
561561
INLINE_FRAGMENT
562+
SCHEMA
562563
SCALAR
563564
OBJECT
564565
FIELD_DEFINITION

graphql/validation/rules/known_directives.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def get_directive_location_for_ast_path(ancestors):
6363
elif isinstance(applied_to, ast.FragmentDefinition):
6464
return DirectiveLocation.FRAGMENT_DEFINITION
6565

66+
elif isinstance(applied_to, ast.SchemaDefinition):
67+
return DirectiveLocation.SCHEMA
68+
6669
elif isinstance(applied_to, ast.ScalarTypeDefinition):
6770
return DirectiveLocation.SCALAR
6871

graphql/validation/tests/test_known_directives.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ def test_within_schema_language_with_well_placed_directives():
133133
input MyInput @onInputObject {
134134
myField: Int @onInputFieldDefinition
135135
}
136+
137+
schema @OnSchema {
138+
query: MyQuery
139+
}
136140
''')
137141

138142

@@ -157,6 +161,10 @@ def test_within_schema_language_with_misplaced_directives():
157161
input MyInput @onEnum {
158162
myField: Int @onArgumentDefinition
159163
}
164+
165+
schema @onObject {
166+
query: MyQuery
167+
}
160168
''', [
161169
misplaced_directive('onInterface', 'OBJECT', 2, 43),
162170
misplaced_directive('onInputFieldDefinition', 'ARGUMENT_DEFINITION', 3, 30),
@@ -170,4 +178,5 @@ def test_within_schema_language_with_misplaced_directives():
170178
misplaced_directive('onUnion', 'ENUM_VALUE', 15, 20),
171179
misplaced_directive('onEnum', 'INPUT_OBJECT', 18, 23),
172180
misplaced_directive('onArgumentDefinition', 'INPUT_FIELD_DEFINITION', 19, 24),
181+
misplaced_directive('onObject', 'SCHEMA', 22, 16),
173182
])

graphql/validation/tests/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
GraphQLDirective(name='onFragmentDefinition', locations=[DirectiveLocation.FRAGMENT_DEFINITION]),
188188
GraphQLDirective(name='onFragmentSpread', locations=[DirectiveLocation.FRAGMENT_SPREAD]),
189189
GraphQLDirective(name='onInlineFragment', locations=[DirectiveLocation.INLINE_FRAGMENT]),
190+
GraphQLDirective(name='OnSchema', locations=[DirectiveLocation.SCHEMA]),
190191
GraphQLDirective(name='onScalar', locations=[DirectiveLocation.SCALAR]),
191192
GraphQLDirective(name='onObject', locations=[DirectiveLocation.OBJECT]),
192193
GraphQLDirective(name='onFieldDefinition', locations=[DirectiveLocation.FIELD_DEFINITION]),

0 commit comments

Comments
 (0)