UNPKG

3.66 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.js");
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, check if it is super type of maybeSubType.
73 // Otherwise, the child type is not a valid subtype of the parent type.
74
75
76 return (0, _definition.isAbstractType)(superType) && ((0, _definition.isInterfaceType)(maybeSubType) || (0, _definition.isObjectType)(maybeSubType)) && schema.isSubType(superType, maybeSubType);
77}
78/**
79 * Provided two composite types, determine if they "overlap". Two composite
80 * types overlap when the Sets of possible concrete types for each intersect.
81 *
82 * This is often used to determine if a fragment of a given type could possibly
83 * be visited in a context of another type.
84 *
85 * This function is commutative.
86 */
87
88
89function doTypesOverlap(schema, typeA, typeB) {
90 // Equivalent types overlap
91 if (typeA === typeB) {
92 return true;
93 }
94
95 if ((0, _definition.isAbstractType)(typeA)) {
96 if ((0, _definition.isAbstractType)(typeB)) {
97 // If both types are abstract, then determine if there is any intersection
98 // between possible concrete types of each.
99 return schema.getPossibleTypes(typeA).some(function (type) {
100 return schema.isSubType(typeB, type);
101 });
102 } // Determine if the latter type is a possible concrete type of the former.
103
104
105 return schema.isSubType(typeA, typeB);
106 }
107
108 if ((0, _definition.isAbstractType)(typeB)) {
109 // Determine if the former type is a possible concrete type of the latter.
110 return schema.isSubType(typeB, typeA);
111 } // Otherwise the types do not overlap.
112
113
114 return false;
115}