UNPKG

4.57 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var apollo_utilities_1 = require("apollo-utilities");
4var ts_invariant_1 = require("ts-invariant");
5var haveWarned = false;
6function shouldWarn() {
7 var answer = !haveWarned;
8 if (!apollo_utilities_1.isTest()) {
9 haveWarned = true;
10 }
11 return answer;
12}
13var HeuristicFragmentMatcher = (function () {
14 function HeuristicFragmentMatcher() {
15 }
16 HeuristicFragmentMatcher.prototype.ensureReady = function () {
17 return Promise.resolve();
18 };
19 HeuristicFragmentMatcher.prototype.canBypassInit = function () {
20 return true;
21 };
22 HeuristicFragmentMatcher.prototype.match = function (idValue, typeCondition, context) {
23 var obj = context.store.get(idValue.id);
24 var isRootQuery = idValue.id === 'ROOT_QUERY';
25 if (!obj) {
26 return isRootQuery;
27 }
28 var _a = obj.__typename, __typename = _a === void 0 ? isRootQuery && 'Query' : _a;
29 if (!__typename) {
30 if (shouldWarn()) {
31 ts_invariant_1.invariant.warn("You're using fragments in your queries, but either don't have the addTypename:\n true option set in Apollo Client, or you are trying to write a fragment to the store without the __typename.\n Please turn on the addTypename option and include __typename when writing fragments so that Apollo Client\n can accurately match fragments.");
32 ts_invariant_1.invariant.warn('Could not find __typename on Fragment ', typeCondition, obj);
33 ts_invariant_1.invariant.warn("DEPRECATION WARNING: using fragments without __typename is unsupported behavior " +
34 "and will be removed in future versions of Apollo client. You should fix this and set addTypename to true now.");
35 }
36 return 'heuristic';
37 }
38 if (__typename === typeCondition) {
39 return true;
40 }
41 if (shouldWarn()) {
42 ts_invariant_1.invariant.error('You are using the simple (heuristic) fragment matcher, but your ' +
43 'queries contain union or interface types. Apollo Client will not be ' +
44 'able to accurately map fragments. To make this error go away, use ' +
45 'the `IntrospectionFragmentMatcher` as described in the docs: ' +
46 'https://www.apollographql.com/docs/react/advanced/fragments.html#fragment-matcher');
47 }
48 return 'heuristic';
49 };
50 return HeuristicFragmentMatcher;
51}());
52exports.HeuristicFragmentMatcher = HeuristicFragmentMatcher;
53var IntrospectionFragmentMatcher = (function () {
54 function IntrospectionFragmentMatcher(options) {
55 if (options && options.introspectionQueryResultData) {
56 this.possibleTypesMap = this.parseIntrospectionResult(options.introspectionQueryResultData);
57 this.isReady = true;
58 }
59 else {
60 this.isReady = false;
61 }
62 this.match = this.match.bind(this);
63 }
64 IntrospectionFragmentMatcher.prototype.match = function (idValue, typeCondition, context) {
65 ts_invariant_1.invariant(this.isReady, 'FragmentMatcher.match() was called before FragmentMatcher.init()');
66 var obj = context.store.get(idValue.id);
67 var isRootQuery = idValue.id === 'ROOT_QUERY';
68 if (!obj) {
69 return isRootQuery;
70 }
71 var _a = obj.__typename, __typename = _a === void 0 ? isRootQuery && 'Query' : _a;
72 ts_invariant_1.invariant(__typename, "Cannot match fragment because __typename property is missing: " + JSON.stringify(obj));
73 if (__typename === typeCondition) {
74 return true;
75 }
76 var implementingTypes = this.possibleTypesMap[typeCondition];
77 if (__typename &&
78 implementingTypes &&
79 implementingTypes.indexOf(__typename) > -1) {
80 return true;
81 }
82 return false;
83 };
84 IntrospectionFragmentMatcher.prototype.parseIntrospectionResult = function (introspectionResultData) {
85 var typeMap = {};
86 introspectionResultData.__schema.types.forEach(function (type) {
87 if (type.kind === 'UNION' || type.kind === 'INTERFACE') {
88 typeMap[type.name] = type.possibleTypes.map(function (implementingType) { return implementingType.name; });
89 }
90 });
91 return typeMap;
92 };
93 return IntrospectionFragmentMatcher;
94}());
95exports.IntrospectionFragmentMatcher = IntrospectionFragmentMatcher;
96//# sourceMappingURL=fragmentMatcher.js.map
\No newline at end of file