Skip to content

Commit 89b1a15

Browse files
committed
Make is_specified_directive/scalar stricter
Also improve unit tests for the predicates. Pull request for graphql/graphql-js is pending.
1 parent 08c6ddd commit 89b1a15

File tree

4 files changed

+96
-28
lines changed

4 files changed

+96
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ a query language for APIs created by Facebook.
1313
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
1414

1515
The current version 1.0.2 of GraphQL-core-next is up-to-date with GraphQL.js version
16-
14.2.1. All parts of the API are covered by an extensive test suite of currently 1731
16+
14.2.1. All parts of the API are covered by an extensive test suite of currently 1742
1717
unit tests.
1818

1919

graphql/type/directives.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ def assert_directive(directive: Any) -> GraphQLDirective:
173173
)
174174

175175

176-
def is_specified_directive(directive: GraphQLDirective):
176+
def is_specified_directive(directive: Any) -> bool:
177177
"""Check whether the given directive is one of the specified directives."""
178-
return isinstance(directive, GraphQLDirective) and any(
178+
return is_directive(directive) and any(
179179
specified_directive.name == directive.name
180180
for specified_directive in specified_directives
181181
)

graphql/type/scalars.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
IntValueNode,
1010
StringValueNode,
1111
)
12-
from .definition import GraphQLScalarType, is_named_type
12+
from .definition import GraphQLScalarType, is_scalar_type
1313

1414
__all__ = [
1515
"is_specified_scalar_type",
@@ -248,4 +248,4 @@ def parse_id_literal(ast, _variables=None):
248248

249249

250250
def is_specified_scalar_type(type_: Any) -> bool:
251-
return is_named_type(type_) and type_.name in specified_scalar_types
251+
return is_scalar_type(type_) and type_.name in specified_scalar_types

tests/type/test_predicate.py

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
from graphql.type import (
55
GraphQLArgument,
66
GraphQLDeprecatedDirective,
7+
GraphQLBoolean,
78
GraphQLDirective,
89
GraphQLEnumType,
10+
GraphQLField,
11+
GraphQLFloat,
12+
GraphQLID,
913
GraphQLIncludeDirective,
1014
GraphQLInputField,
1115
GraphQLInputObjectType,
16+
GraphQLInt,
1217
GraphQLInterfaceType,
1318
GraphQLList,
1419
GraphQLNonNull,
@@ -19,6 +24,7 @@
1924
GraphQLUnionType,
2025
assert_abstract_type,
2126
assert_composite_type,
27+
assert_directive,
2228
assert_enum_type,
2329
assert_input_object_type,
2430
assert_input_type,
@@ -54,6 +60,7 @@
5460
is_output_type,
5561
is_scalar_type,
5662
is_specified_directive,
63+
is_specified_scalar_type,
5764
is_type,
5865
is_union_type,
5966
is_wrapping_type,
@@ -64,9 +71,8 @@
6471
UnionType = GraphQLUnionType("Union", types=[ObjectType])
6572
EnumType = GraphQLEnumType("Enum", values={"foo": {}})
6673
InputObjectType = GraphQLInputObjectType("InputObject", {})
67-
ScalarType = GraphQLScalarType(
68-
"Scalar", serialize=lambda: {}, parse_value=lambda: {}, parse_literal=lambda: {}
69-
)
74+
ScalarType = GraphQLScalarType("Scalar", lambda: None)
75+
Directive = GraphQLDirective("Directive", [DirectiveLocation.QUERY])
7076

7177

7278
def describe_type_predicates():
@@ -100,10 +106,65 @@ def returns_true_for_custom_scalar():
100106
assert is_scalar_type(ScalarType) is True
101107
assert_scalar_type(ScalarType)
102108

109+
def returns_fals_for_scalar_class_rather_than_instance():
110+
assert is_scalar_type(GraphQLScalarType) is False
111+
with raises(TypeError):
112+
assert_scalar_type(GraphQLScalarType)
113+
114+
def returns_false_for_wrapped_scalar():
115+
assert is_scalar_type(GraphQLList(ScalarType)) is False
116+
with raises(TypeError):
117+
assert_scalar_type(GraphQLList(ScalarType))
118+
103119
def returns_false_for_non_scalar():
104120
assert is_scalar_type(EnumType) is False
105121
with raises(TypeError):
106122
assert_scalar_type(EnumType)
123+
assert is_scalar_type(Directive) is False
124+
with raises(TypeError):
125+
assert_scalar_type(Directive)
126+
127+
def returns_false_for_random_garbage():
128+
assert is_scalar_type(None) is False
129+
with raises(TypeError):
130+
assert_scalar_type(None)
131+
assert is_scalar_type({"what": "is this"}) is False
132+
with raises(TypeError):
133+
assert_scalar_type({"what": "is this"})
134+
135+
def describe_is_specified_scalar_type():
136+
def returns_true_for_specified_scalars():
137+
assert is_specified_scalar_type(GraphQLString) is True
138+
assert is_specified_scalar_type(GraphQLInt) is True
139+
assert is_specified_scalar_type(GraphQLFloat) is True
140+
assert is_specified_scalar_type(GraphQLBoolean) is True
141+
assert is_specified_scalar_type(GraphQLID) is True
142+
143+
def returns_false_for_custom_scalar():
144+
assert is_specified_scalar_type(ScalarType) is False
145+
146+
def returns_fals_for_scalar_class_rather_than_specified_instance():
147+
assert is_specified_scalar_type(GraphQLScalarType) is False
148+
149+
def returns_false_for_wrapped_specified_scalar():
150+
assert is_scalar_type(GraphQLList(GraphQLString)) is False
151+
152+
def returns_false_for_non_scalar():
153+
assert is_specified_scalar_type(EnumType) is False
154+
assert is_specified_scalar_type(Directive) is False
155+
156+
def returns_false_for_spec_defined_directive():
157+
assert is_specified_scalar_type(GraphQLSkipDirective) is False
158+
159+
def returns_false_for_object_type_named_like_specified_directive():
160+
ObjectNamedLikeScalar = GraphQLObjectType(
161+
"String", {"serialize": GraphQLField(GraphQLString)}
162+
)
163+
assert is_specified_scalar_type(ObjectNamedLikeScalar) is False
164+
165+
def returns_false_for_random_garbage():
166+
assert is_specified_scalar_type(None) is False
167+
assert is_specified_scalar_type({"what": "is this"}) is False
107168

108169
def describe_is_object_type():
109170
def returns_true_for_object_type():
@@ -463,23 +524,34 @@ def returns_false_for_optional_input_field():
463524

464525
def describe_directive_predicates():
465526
def describe_is_directive():
466-
def returns_true_for_directives():
467-
directive = GraphQLDirective("Foo", [DirectiveLocation.QUERY])
468-
assert is_directive(directive) is True
527+
def returns_true_for_spec_defined_directive():
469528
assert is_directive(GraphQLSkipDirective) is True
529+
assert_directive(GraphQLSkipDirective)
530+
531+
def returns_true_for_custom_directive():
532+
assert is_directive(Directive) is True
533+
assert_directive(Directive)
470534

471535
def returns_false_for_directive_class_rather_than_instance():
472536
assert is_directive(GraphQLDirective) is False
537+
with raises(TypeError):
538+
assert_directive(GraphQLScalarType)
473539

474-
def returns_false_for_object_type():
475-
assert is_directive(ObjectType) is False
476-
477-
def returns_false_for_scalar_type():
478-
assert is_directive(GraphQLString) is False
540+
def returns_false_for_non_directive():
541+
assert is_directive(EnumType) is False
542+
with raises(TypeError):
543+
assert_directive(EnumType)
544+
assert is_directive(ScalarType) is False
545+
with raises(TypeError):
546+
assert_directive(ScalarType)
479547

480548
def returns_false_for_random_garbage():
481549
assert is_directive(None) is False
550+
with raises(TypeError):
551+
assert_directive(None)
482552
assert is_directive({"what": "is this"}) is False
553+
with raises(TypeError):
554+
assert_directive({"what": "is this"})
483555

484556
def describe_is_specified_directive():
485557
def returns_true_for_specified_directives():
@@ -488,25 +560,21 @@ def returns_true_for_specified_directives():
488560
assert is_specified_directive(GraphQLDeprecatedDirective) is True
489561

490562
def returns_false_for_custom_directive():
491-
directive = GraphQLDirective("Foo", [DirectiveLocation.QUERY])
492-
assert is_specified_directive(directive) is False
563+
assert is_specified_directive(Directive) is False
493564

494-
def returns_false_for_directive_class_rather_than_instance():
565+
def returns_false_for_directive_class_rather_than_specified_instance():
495566
assert is_specified_directive(GraphQLDirective) is False
496567

497-
def returns_false_for_object_type():
498-
assert is_specified_directive(ObjectType) is False
568+
def returns_false_for_non_directive():
569+
assert is_specified_directive(EnumType) is False
570+
assert is_specified_directive(ScalarType) is False
499571

500572
def returns_false_for_spec_defined_scalar_type():
501573
assert is_specified_directive(GraphQLString) is False
502574

503-
def returns_false_for_scalar_type_with_name_of_specified_directive():
504-
assert (
505-
is_specified_directive(
506-
GraphQLScalarType("deprecated", lambda: None)
507-
)
508-
is False
509-
)
575+
def returns_false_for_scalar_type_named_like_specified_directive():
576+
ScalarNamedLikeDirective = GraphQLScalarType("deprecated", lambda: None)
577+
assert is_specified_directive(ScalarNamedLikeDirective) is False
510578

511579
def returns_false_for_random_garbage():
512580
assert is_specified_directive(None) is False

0 commit comments

Comments
 (0)