1 | import { isInterfaceType, isObjectType, isListType, isNonNullType, isAbstractType } from "../type/definition.mjs";
|
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, check if it is super type of maybeSubType.
|
63 | // Otherwise, the child type is not a valid subtype of the parent type.
|
64 |
|
65 |
|
66 | return isAbstractType(superType) && (isInterfaceType(maybeSubType) || isObjectType(maybeSubType)) && schema.isSubType(superType, maybeSubType);
|
67 | }
|
68 | /**
|
69 | * Provided two composite types, determine if they "overlap". Two composite
|
70 | * types overlap when the Sets of possible concrete types for each intersect.
|
71 | *
|
72 | * This is often used to determine if a fragment of a given type could possibly
|
73 | * be visited in a context of another type.
|
74 | *
|
75 | * This function is commutative.
|
76 | */
|
77 |
|
78 | export function doTypesOverlap(schema, typeA, typeB) {
|
79 | // Equivalent types overlap
|
80 | if (typeA === typeB) {
|
81 | return true;
|
82 | }
|
83 |
|
84 | if (isAbstractType(typeA)) {
|
85 | if (isAbstractType(typeB)) {
|
86 | // If both types are abstract, then determine if there is any intersection
|
87 | // between possible concrete types of each.
|
88 | return schema.getPossibleTypes(typeA).some(function (type) {
|
89 | return schema.isSubType(typeB, type);
|
90 | });
|
91 | } // Determine if the latter type is a possible concrete type of the former.
|
92 |
|
93 |
|
94 | return schema.isSubType(typeA, typeB);
|
95 | }
|
96 |
|
97 | if (isAbstractType(typeB)) {
|
98 | // Determine if the former type is a possible concrete type of the latter.
|
99 | return schema.isSubType(typeB, typeA);
|
100 | } // Otherwise the types do not overlap.
|
101 |
|
102 |
|
103 | return false;
|
104 | }
|