UNPKG

3.94 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 }
20
21 // If either type is non-null, the other must also be non-null.
22 if ((0, _definition.isNonNullType)(typeA) && (0, _definition.isNonNullType)(typeB)) {
23 return isEqualType(typeA.ofType, typeB.ofType);
24 }
25
26 // If either type is a list, the other must also be a list.
27 if ((0, _definition.isListType)(typeA) && (0, _definition.isListType)(typeB)) {
28 return isEqualType(typeA.ofType, typeB.ofType);
29 }
30
31 // Otherwise the types are not equal.
32 return false;
33}
34
35/**
36 * Provided a type and a super type, return true if the first type is either
37 * equal or a subset of the second super type (covariant).
38 */
39/**
40 * Copyright (c) 2015-present, Facebook, Inc.
41 *
42 * This source code is licensed under the MIT license found in the
43 * LICENSE file in the root directory of this source tree.
44 *
45 *
46 */
47
48function isTypeSubTypeOf(schema, maybeSubType, superType) {
49 // Equivalent type is a valid subtype
50 if (maybeSubType === superType) {
51 return true;
52 }
53
54 // If superType is non-null, maybeSubType must also be non-null.
55 if ((0, _definition.isNonNullType)(superType)) {
56 if ((0, _definition.isNonNullType)(maybeSubType)) {
57 return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType);
58 }
59 return false;
60 }
61 if ((0, _definition.isNonNullType)(maybeSubType)) {
62 // If superType is nullable, maybeSubType may be non-null or nullable.
63 return isTypeSubTypeOf(schema, maybeSubType.ofType, superType);
64 }
65
66 // If superType type is a list, maybeSubType type must also be a list.
67 if ((0, _definition.isListType)(superType)) {
68 if ((0, _definition.isListType)(maybeSubType)) {
69 return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType);
70 }
71 return false;
72 }
73 if ((0, _definition.isListType)(maybeSubType)) {
74 // If superType is not a list, maybeSubType must also be not a list.
75 return false;
76 }
77
78 // If superType type is an abstract type, maybeSubType type may be a currently
79 // possible object type.
80 if ((0, _definition.isAbstractType)(superType) && (0, _definition.isObjectType)(maybeSubType) && schema.isPossibleType(superType, maybeSubType)) {
81 return true;
82 }
83
84 // Otherwise, the child type is not a valid subtype of the parent type.
85 return false;
86}
87
88/**
89 * Provided two composite types, determine if they "overlap". Two composite
90 * types overlap when the Sets of possible concrete types for each intersect.
91 *
92 * This is often used to determine if a fragment of a given type could possibly
93 * be visited in a context of another type.
94 *
95 * This function is commutative.
96 */
97function doTypesOverlap(schema, typeA, typeB) {
98 // So flow is aware this is constant
99 var _typeB = typeB;
100
101 // Equivalent types overlap
102 if (typeA === _typeB) {
103 return true;
104 }
105
106 if ((0, _definition.isAbstractType)(typeA)) {
107 if ((0, _definition.isAbstractType)(_typeB)) {
108 // If both types are abstract, then determine if there is any intersection
109 // between possible concrete types of each.
110 return schema.getPossibleTypes(typeA).some(function (type) {
111 return schema.isPossibleType(_typeB, type);
112 });
113 }
114 // Determine if the latter type is a possible concrete type of the former.
115 return schema.isPossibleType(typeA, _typeB);
116 }
117
118 if ((0, _definition.isAbstractType)(_typeB)) {
119 // Determine if the former type is a possible concrete type of the latter.
120 return schema.isPossibleType(_typeB, typeA);
121 }
122
123 // Otherwise the types do not overlap.
124 return false;
125}
\No newline at end of file