fragments.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var ts_invariant_1 = require("ts-invariant");
  5. function getFragmentQueryDocument(document, fragmentName) {
  6. var actualFragmentName = fragmentName;
  7. var fragments = [];
  8. document.definitions.forEach(function (definition) {
  9. if (definition.kind === 'OperationDefinition') {
  10. throw new ts_invariant_1.InvariantError("Found a " + definition.operation + " operation" + (definition.name ? " named '" + definition.name.value + "'" : '') + ". " +
  11. 'No operations are allowed when using a fragment as a query. Only fragments are allowed.');
  12. }
  13. if (definition.kind === 'FragmentDefinition') {
  14. fragments.push(definition);
  15. }
  16. });
  17. if (typeof actualFragmentName === 'undefined') {
  18. ts_invariant_1.invariant(fragments.length === 1, "Found " + fragments.length + " fragments. `fragmentName` must be provided when there is not exactly 1 fragment.");
  19. actualFragmentName = fragments[0].name.value;
  20. }
  21. var query = tslib_1.__assign(tslib_1.__assign({}, document), { definitions: tslib_1.__spreadArrays([
  22. {
  23. kind: 'OperationDefinition',
  24. operation: 'query',
  25. selectionSet: {
  26. kind: 'SelectionSet',
  27. selections: [
  28. {
  29. kind: 'FragmentSpread',
  30. name: {
  31. kind: 'Name',
  32. value: actualFragmentName,
  33. },
  34. },
  35. ],
  36. },
  37. }
  38. ], document.definitions) });
  39. return query;
  40. }
  41. exports.getFragmentQueryDocument = getFragmentQueryDocument;
  42. //# sourceMappingURL=fragments.js.map