UNPKG

apollo-client-cors-hack

Version:

A simple yet functional GraphQL client.

86 lines 4.03 kB
import { isTest, } from '../util/environment'; import { warnOnceInDevelopment, } from '../util/warnOnce'; var IntrospectionFragmentMatcher = (function () { function IntrospectionFragmentMatcher(options) { if (options && options.introspectionQueryResultData) { this.possibleTypesMap = this.parseIntrospectionResult(options.introspectionQueryResultData); this.isReady = true; } else { this.isReady = false; } this.match = this.match.bind(this); } IntrospectionFragmentMatcher.prototype.match = function (idValue, typeCondition, context) { if (!this.isReady) { throw new Error('FragmentMatcher.match() was called before FragmentMatcher.init()'); } var obj = context.store[idValue.id]; if (!obj) { return false; } if (!obj.__typename) { throw new Error("Cannot match fragment because __typename property is missing: " + JSON.stringify(obj)); } if (obj.__typename === typeCondition) { return true; } var implementingTypes = this.possibleTypesMap[typeCondition]; if (implementingTypes && implementingTypes.indexOf(obj.__typename) > -1) { return true; } return false; }; IntrospectionFragmentMatcher.prototype.parseIntrospectionResult = function (introspectionResultData) { var typeMap = {}; introspectionResultData.__schema.types.forEach(function (type) { if (type.kind === 'UNION' || type.kind === 'INTERFACE') { typeMap[type.name] = type.possibleTypes.map(function (implementingType) { return implementingType.name; }); } }); return typeMap; }; return IntrospectionFragmentMatcher; }()); export { IntrospectionFragmentMatcher }; var haveWarned = false; var HeuristicFragmentMatcher = (function () { function HeuristicFragmentMatcher() { } HeuristicFragmentMatcher.prototype.ensureReady = function () { return Promise.resolve(); }; HeuristicFragmentMatcher.prototype.canBypassInit = function () { return true; }; HeuristicFragmentMatcher.prototype.match = function (idValue, typeCondition, context) { var obj = context.store[idValue.id]; if (!obj) { return false; } if (!obj.__typename) { if (!haveWarned) { console.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."); console.warn('Could not find __typename on Fragment ', typeCondition, obj); console.warn("DEPRECATION WARNING: using fragments without __typename is unsupported behavior " + "and will be removed in future versions of Apollo client. You should fix this and set addTypename to true now."); if (!isTest()) { haveWarned = true; } } context.returnPartialData = true; return true; } if (obj.__typename === typeCondition) { return true; } warnOnceInDevelopment("You are using the simple (heuristic) fragment matcher, but your queries contain union or interface types.\n Apollo Client will not be able to able to accurately map fragments." + "To make this error go away, use the IntrospectionFragmentMatcher as described in the docs: " + "http://dev.apollodata.com/react/initialization.html#fragment-matcher", 'error'); context.returnPartialData = true; return true; }; return HeuristicFragmentMatcher; }()); export { HeuristicFragmentMatcher }; //# sourceMappingURL=fragmentMatcher.js.map