1 | import { isObjectType, isListType, isNonNullType, isAbstractType } from '../type/definition';
|
2 | /**
|
3 | * Provided two types, return true if the types are equal (invariant).
|
4 | */
|
5 |
|
6 | export function isEqualType(typeA, typeB) {
|
7 | // Equivalent types are equal.
|
8 | if (typeA === typeB) {
|
9 | return true;
|
10 | } // If either type is non-null, the other must also be non-null.
|
11 |
|
12 |
|
13 | if (isNonNullType(typeA) && isNonNullType(typeB)) {
|
14 | return isEqualType(typeA.ofType, typeB.ofType);
|
15 | } // If either type is a list, the other must also be a list.
|
16 |
|
17 |
|
18 | if (isListType(typeA) && isListType(typeB)) {
|
19 | return isEqualType(typeA.ofType, typeB.ofType);
|
20 | } // Otherwise the types are not equal.
|
21 |
|
22 |
|
23 | return false;
|
24 | }
|
25 | /**
|
26 | * Provided a type and a super type, return true if the first type is either
|
27 | * equal or a subset of the second super type (covariant).
|
28 | */
|
29 |
|
30 | export function isTypeSubTypeOf(schema, maybeSubType, superType) {
|
31 | // Equivalent type is a valid subtype
|
32 | if (maybeSubType === superType) {
|
33 | return true;
|
34 | } // If superType is non-null, maybeSubType must also be non-null.
|
35 |
|
36 |
|
37 | if (isNonNullType(superType)) {
|
38 | if (isNonNullType(maybeSubType)) {
|
39 | return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType);
|
40 | }
|
41 |
|
42 | return false;
|
43 | }
|
44 |
|
45 | if (isNonNullType(maybeSubType)) {
|
46 | // If superType is nullable, maybeSubType may be non-null or nullable.
|
47 | return isTypeSubTypeOf(schema, maybeSubType.ofType, superType);
|
48 | } // If superType type is a list, maybeSubType type must also be a list.
|
49 |
|
50 |
|
51 | if (isListType(superType)) {
|
52 | if (isListType(maybeSubType)) {
|
53 | return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType);
|
54 | }
|
55 |
|
56 | return false;
|
57 | }
|
58 |
|
59 | if (isListType(maybeSubType)) {
|
60 | // If superType is not a list, maybeSubType must also be not a list.
|
61 | return false;
|
62 | } // If superType type is an abstract type, maybeSubType type may be a currently
|
63 | // possible object type.
|
64 |
|
65 |
|
66 | if (isAbstractType(superType) && isObjectType(maybeSubType) && schema.isPossibleType(superType, maybeSubType)) {
|
67 | return true;
|
68 | } // Otherwise, the child type is not a valid subtype of the parent type.
|
69 |
|
70 |
|
71 | return false;
|
72 | }
|
73 | /**
|
74 | * Provided two composite types, determine if they "overlap". Two composite
|
75 | * types overlap when the Sets of possible concrete types for each intersect.
|
76 | *
|
77 | * This is often used to determine if a fragment of a given type could possibly
|
78 | * be visited in a context of another type.
|
79 | *
|
80 | * This function is commutative.
|
81 | */
|
82 |
|
83 | export function doTypesOverlap(schema, typeA, typeB) {
|
84 | // Equivalent types overlap
|
85 | if (typeA === typeB) {
|
86 | return true;
|
87 | }
|
88 |
|
89 | if (isAbstractType(typeA)) {
|
90 | if (isAbstractType(typeB)) {
|
91 | // If both types are abstract, then determine if there is any intersection
|
92 | // between possible concrete types of each.
|
93 | return schema.getPossibleTypes(typeA).some(function (type) {
|
94 | return schema.isPossibleType(typeB, type);
|
95 | });
|
96 | } // Determine if the latter type is a possible concrete type of the former.
|
97 |
|
98 |
|
99 | return schema.isPossibleType(typeA, typeB);
|
100 | }
|
101 |
|
102 | if (isAbstractType(typeB)) {
|
103 | // Determine if the former type is a possible concrete type of the latter.
|
104 | return schema.isPossibleType(typeB, typeA);
|
105 | } // Otherwise the types do not overlap.
|
106 |
|
107 |
|
108 | return false;
|
109 | }
|