1 | import { __assign, __spreadArray } from "tslib";
|
2 | import { invariant, InvariantError } from "../globals/index.js";
|
3 | export function getFragmentQueryDocument(document, fragmentName) {
|
4 | var actualFragmentName = fragmentName;
|
5 | var fragments = [];
|
6 | document.definitions.forEach(function (definition) {
|
7 | if (definition.kind === 'OperationDefinition') {
|
8 | throw __DEV__ ? new InvariantError("Found a ".concat(definition.operation, " operation").concat(definition.name ? " named '".concat(definition.name.value, "'") : '', ". ") +
|
9 | 'No operations are allowed when using a fragment as a query. Only fragments are allowed.') : new InvariantError(44);
|
10 | }
|
11 | if (definition.kind === 'FragmentDefinition') {
|
12 | fragments.push(definition);
|
13 | }
|
14 | });
|
15 | if (typeof actualFragmentName === 'undefined') {
|
16 | __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);
|
17 | actualFragmentName = fragments[0].name.value;
|
18 | }
|
19 | var query = __assign(__assign({}, document), { definitions: __spreadArray([
|
20 | {
|
21 | kind: 'OperationDefinition',
|
22 | operation: 'query',
|
23 | selectionSet: {
|
24 | kind: 'SelectionSet',
|
25 | selections: [
|
26 | {
|
27 | kind: 'FragmentSpread',
|
28 | name: {
|
29 | kind: 'Name',
|
30 | value: actualFragmentName,
|
31 | },
|
32 | },
|
33 | ],
|
34 | },
|
35 | }
|
36 | ], document.definitions, true) });
|
37 | return query;
|
38 | }
|
39 | export function createFragmentMap(fragments) {
|
40 | if (fragments === void 0) { fragments = []; }
|
41 | var symTable = {};
|
42 | fragments.forEach(function (fragment) {
|
43 | symTable[fragment.name.value] = fragment;
|
44 | });
|
45 | return symTable;
|
46 | }
|
47 | export function getFragmentFromSelection(selection, fragmentMap) {
|
48 | switch (selection.kind) {
|
49 | case 'InlineFragment':
|
50 | return selection;
|
51 | case 'FragmentSpread': {
|
52 | var fragmentName = selection.name.value;
|
53 | if (typeof fragmentMap === "function") {
|
54 | return fragmentMap(fragmentName);
|
55 | }
|
56 | var fragment = fragmentMap && fragmentMap[fragmentName];
|
57 | __DEV__ ? invariant(fragment, "No fragment named ".concat(fragmentName)) : invariant(fragment, 46);
|
58 | return fragment || null;
|
59 | }
|
60 | default:
|
61 | return null;
|
62 | }
|
63 | }
|
64 |
|
\ | No newline at end of file |