Skip to content

Commit 47c86b9

Browse files
committed
Extract type comparators into the public API
Related GraphQL-js commit: graphql/graphql-js@aff18e6
1 parent 468621b commit 47c86b9

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

graphql/core/utils/type_comparators.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ..type.definition import (GraphQLList, GraphQLNonNull, GraphQLObjectType,
2+
GraphQLInterfaceType, GraphQLUnionType,
23
is_abstract_type)
34

45

@@ -38,3 +39,18 @@ def is_type_sub_type_of(maybe_subtype, super_type):
3839
return True
3940

4041
return False
42+
43+
44+
def do_types_overlap(t1, t2):
45+
if t1 == t2:
46+
return True
47+
if isinstance(t1, GraphQLObjectType):
48+
if isinstance(t2, GraphQLObjectType):
49+
return False
50+
return t1 in t2.get_possible_types()
51+
if isinstance(t1, GraphQLInterfaceType) or isinstance(t1, GraphQLUnionType):
52+
if isinstance(t2, GraphQLObjectType):
53+
return t2 in t1.get_possible_types()
54+
55+
t1_type_names = {possible_type.name: possible_type for possible_type in t1.get_possible_types()}
56+
return any(t.name in t1_type_names for t in t2.get_possible_types())

graphql/core/validation/rules/possible_fragment_spreads.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from ...error import GraphQLError
2-
from ...type.definition import (GraphQLInterfaceType, GraphQLObjectType,
3-
GraphQLUnionType)
42
from ...utils.type_from_ast import type_from_ast
3+
from ...utils.type_comparators import do_types_overlap
54
from .base import ValidationRule
65

76

@@ -10,7 +9,7 @@ class PossibleFragmentSpreads(ValidationRule):
109
def enter_InlineFragment(self, node, key, parent, path, ancestors):
1110
frag_type = self.context.get_type()
1211
parent_type = self.context.get_parent_type()
13-
if frag_type and parent_type and not self.do_types_overlap(frag_type, parent_type):
12+
if frag_type and parent_type and not do_types_overlap(frag_type, parent_type):
1413
self.context.report_error(GraphQLError(
1514
self.type_incompatible_anon_spread_message(parent_type, frag_type),
1615
[node]
@@ -20,7 +19,7 @@ def enter_FragmentSpread(self, node, key, parent, path, ancestors):
2019
frag_name = node.name.value
2120
frag_type = self.get_fragment_type(self.context, frag_name)
2221
parent_type = self.context.get_parent_type()
23-
if frag_type and parent_type and not self.do_types_overlap(frag_type, parent_type):
22+
if frag_type and parent_type and not do_types_overlap(frag_type, parent_type):
2423
self.context.report_error(GraphQLError(
2524
self.type_incompatible_spread_message(frag_name, parent_type, frag_type),
2625
[node]
@@ -31,21 +30,6 @@ def get_fragment_type(context, name):
3130
frag = context.get_fragment(name)
3231
return frag and type_from_ast(context.get_schema(), frag.type_condition)
3332

34-
@staticmethod
35-
def do_types_overlap(t1, t2):
36-
if t1 == t2:
37-
return True
38-
if isinstance(t1, GraphQLObjectType):
39-
if isinstance(t2, GraphQLObjectType):
40-
return False
41-
return t1 in t2.get_possible_types()
42-
if isinstance(t1, GraphQLInterfaceType) or isinstance(t1, GraphQLUnionType):
43-
if isinstance(t2, GraphQLObjectType):
44-
return t2 in t1.get_possible_types()
45-
46-
t1_type_names = {possible_type.name: possible_type for possible_type in t1.get_possible_types()}
47-
return any(t.name in t1_type_names for t in t2.get_possible_types())
48-
4933
@staticmethod
5034
def type_incompatible_spread_message(frag_name, parent_type, frag_type):
5135
return 'Fragment {} cannot be spread here as objects of type {} can never be of type {}'.format(frag_name,

0 commit comments

Comments
 (0)