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