UNPKG

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