UNPKG

@apollo/client

Version:

A fully-featured caching GraphQL client.

64 lines 2.73 kB
import { __assign, __spreadArray } from "tslib"; import { invariant, InvariantError } from "../globals/index.js"; export function getFragmentQueryDocument(document, fragmentName) { var actualFragmentName = fragmentName; var fragments = []; document.definitions.forEach(function (definition) { if (definition.kind === 'OperationDefinition') { throw __DEV__ ? new InvariantError("Found a ".concat(definition.operation, " operation").concat(definition.name ? " named '".concat(definition.name.value, "'") : '', ". ") + 'No operations are allowed when using a fragment as a query. Only fragments are allowed.') : new InvariantError(44); } if (definition.kind === 'FragmentDefinition') { fragments.push(definition); } }); if (typeof actualFragmentName === 'undefined') { __DEV__ ? invariant(fragments.length === 1, "Found ".concat(fragments.length, " fragments. `fragmentName` must be provided when there is not exactly 1 fragment.")) : invariant(fragments.length === 1, 45); actualFragmentName = fragments[0].name.value; } var query = __assign(__assign({}, document), { definitions: __spreadArray([ { kind: 'OperationDefinition', operation: 'query', selectionSet: { kind: 'SelectionSet', selections: [ { kind: 'FragmentSpread', name: { kind: 'Name', value: actualFragmentName, }, }, ], }, } ], document.definitions, true) }); return query; } export function createFragmentMap(fragments) { if (fragments === void 0) { fragments = []; } var symTable = {}; fragments.forEach(function (fragment) { symTable[fragment.name.value] = fragment; }); return symTable; } export function getFragmentFromSelection(selection, fragmentMap) { switch (selection.kind) { case 'InlineFragment': return selection; case 'FragmentSpread': { var fragmentName = selection.name.value; if (typeof fragmentMap === "function") { return fragmentMap(fragmentName); } var fragment = fragmentMap && fragmentMap[fragmentName]; __DEV__ ? invariant(fragment, "No fragment named ".concat(fragmentName)) : invariant(fragment, 46); return fragment || null; } default: return null; } } //# sourceMappingURL=fragments.js.map