123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- var tslib_1 = require("tslib");
- var visitor_1 = require("graphql/language/visitor");
- var getFromAST_1 = require("./getFromAST");
- var filterInPlace_1 = require("./util/filterInPlace");
- var ts_invariant_1 = require("ts-invariant");
- var storeUtils_1 = require("./storeUtils");
- var TYPENAME_FIELD = {
- kind: 'Field',
- name: {
- kind: 'Name',
- value: '__typename',
- },
- };
- function isEmpty(op, fragments) {
- return op.selectionSet.selections.every(function (selection) {
- return selection.kind === 'FragmentSpread' &&
- isEmpty(fragments[selection.name.value], fragments);
- });
- }
- function nullIfDocIsEmpty(doc) {
- return isEmpty(getFromAST_1.getOperationDefinition(doc) || getFromAST_1.getFragmentDefinition(doc), getFromAST_1.createFragmentMap(getFromAST_1.getFragmentDefinitions(doc)))
- ? null
- : doc;
- }
- function getDirectiveMatcher(directives) {
- return function directiveMatcher(directive) {
- return directives.some(function (dir) {
- return (dir.name && dir.name === directive.name.value) ||
- (dir.test && dir.test(directive));
- });
- };
- }
- function removeDirectivesFromDocument(directives, doc) {
- var variablesInUse = Object.create(null);
- var variablesToRemove = [];
- var fragmentSpreadsInUse = Object.create(null);
- var fragmentSpreadsToRemove = [];
- var modifiedDoc = nullIfDocIsEmpty(visitor_1.visit(doc, {
- Variable: {
- enter: function (node, _key, parent) {
- if (parent.kind !== 'VariableDefinition') {
- variablesInUse[node.name.value] = true;
- }
- },
- },
- Field: {
- enter: function (node) {
- if (directives && node.directives) {
- var shouldRemoveField = directives.some(function (directive) { return directive.remove; });
- if (shouldRemoveField &&
- node.directives &&
- node.directives.some(getDirectiveMatcher(directives))) {
- if (node.arguments) {
- node.arguments.forEach(function (arg) {
- if (arg.value.kind === 'Variable') {
- variablesToRemove.push({
- name: arg.value.name.value,
- });
- }
- });
- }
- if (node.selectionSet) {
- getAllFragmentSpreadsFromSelectionSet(node.selectionSet).forEach(function (frag) {
- fragmentSpreadsToRemove.push({
- name: frag.name.value,
- });
- });
- }
- return null;
- }
- }
- },
- },
- FragmentSpread: {
- enter: function (node) {
- fragmentSpreadsInUse[node.name.value] = true;
- },
- },
- Directive: {
- enter: function (node) {
- if (getDirectiveMatcher(directives)(node)) {
- return null;
- }
- },
- },
- }));
- if (modifiedDoc &&
- filterInPlace_1.filterInPlace(variablesToRemove, function (v) { return !variablesInUse[v.name]; }).length) {
- modifiedDoc = removeArgumentsFromDocument(variablesToRemove, modifiedDoc);
- }
- if (modifiedDoc &&
- filterInPlace_1.filterInPlace(fragmentSpreadsToRemove, function (fs) { return !fragmentSpreadsInUse[fs.name]; })
- .length) {
- modifiedDoc = removeFragmentSpreadFromDocument(fragmentSpreadsToRemove, modifiedDoc);
- }
- return modifiedDoc;
- }
- exports.removeDirectivesFromDocument = removeDirectivesFromDocument;
- function addTypenameToDocument(doc) {
- return visitor_1.visit(getFromAST_1.checkDocument(doc), {
- SelectionSet: {
- enter: function (node, _key, parent) {
- if (parent &&
- parent.kind === 'OperationDefinition') {
- return;
- }
- var selections = node.selections;
- if (!selections) {
- return;
- }
- var skip = selections.some(function (selection) {
- return (storeUtils_1.isField(selection) &&
- (selection.name.value === '__typename' ||
- selection.name.value.lastIndexOf('__', 0) === 0));
- });
- if (skip) {
- return;
- }
- var field = parent;
- if (storeUtils_1.isField(field) &&
- field.directives &&
- field.directives.some(function (d) { return d.name.value === 'export'; })) {
- return;
- }
- return tslib_1.__assign(tslib_1.__assign({}, node), { selections: tslib_1.__spreadArrays(selections, [TYPENAME_FIELD]) });
- },
- },
- });
- }
- exports.addTypenameToDocument = addTypenameToDocument;
- var connectionRemoveConfig = {
- test: function (directive) {
- var willRemove = directive.name.value === 'connection';
- if (willRemove) {
- if (!directive.arguments ||
- !directive.arguments.some(function (arg) { return arg.name.value === 'key'; })) {
- ts_invariant_1.invariant.warn('Removing an @connection directive even though it does not have a key. ' +
- 'You may want to use the key parameter to specify a store key.');
- }
- }
- return willRemove;
- },
- };
- function removeConnectionDirectiveFromDocument(doc) {
- return removeDirectivesFromDocument([connectionRemoveConfig], getFromAST_1.checkDocument(doc));
- }
- exports.removeConnectionDirectiveFromDocument = removeConnectionDirectiveFromDocument;
- function hasDirectivesInSelectionSet(directives, selectionSet, nestedCheck) {
- if (nestedCheck === void 0) { nestedCheck = true; }
- return (selectionSet &&
- selectionSet.selections &&
- selectionSet.selections.some(function (selection) {
- return hasDirectivesInSelection(directives, selection, nestedCheck);
- }));
- }
- function hasDirectivesInSelection(directives, selection, nestedCheck) {
- if (nestedCheck === void 0) { nestedCheck = true; }
- if (!storeUtils_1.isField(selection)) {
- return true;
- }
- if (!selection.directives) {
- return false;
- }
- return (selection.directives.some(getDirectiveMatcher(directives)) ||
- (nestedCheck &&
- hasDirectivesInSelectionSet(directives, selection.selectionSet, nestedCheck)));
- }
- function getDirectivesFromDocument(directives, doc) {
- getFromAST_1.checkDocument(doc);
- var parentPath;
- return nullIfDocIsEmpty(visitor_1.visit(doc, {
- SelectionSet: {
- enter: function (node, _key, _parent, path) {
- var currentPath = path.join('-');
- if (!parentPath ||
- currentPath === parentPath ||
- !currentPath.startsWith(parentPath)) {
- if (node.selections) {
- var selectionsWithDirectives = node.selections.filter(function (selection) { return hasDirectivesInSelection(directives, selection); });
- if (hasDirectivesInSelectionSet(directives, node, false)) {
- parentPath = currentPath;
- }
- return tslib_1.__assign(tslib_1.__assign({}, node), { selections: selectionsWithDirectives });
- }
- else {
- return null;
- }
- }
- },
- },
- }));
- }
- exports.getDirectivesFromDocument = getDirectivesFromDocument;
- function getArgumentMatcher(config) {
- return function argumentMatcher(argument) {
- return config.some(function (aConfig) {
- return argument.value &&
- argument.value.kind === 'Variable' &&
- argument.value.name &&
- (aConfig.name === argument.value.name.value ||
- (aConfig.test && aConfig.test(argument)));
- });
- };
- }
- function removeArgumentsFromDocument(config, doc) {
- var argMatcher = getArgumentMatcher(config);
- return nullIfDocIsEmpty(visitor_1.visit(doc, {
- OperationDefinition: {
- enter: function (node) {
- return tslib_1.__assign(tslib_1.__assign({}, node), { variableDefinitions: node.variableDefinitions.filter(function (varDef) {
- return !config.some(function (arg) { return arg.name === varDef.variable.name.value; });
- }) });
- },
- },
- Field: {
- enter: function (node) {
- var shouldRemoveField = config.some(function (argConfig) { return argConfig.remove; });
- if (shouldRemoveField) {
- var argMatchCount_1 = 0;
- node.arguments.forEach(function (arg) {
- if (argMatcher(arg)) {
- argMatchCount_1 += 1;
- }
- });
- if (argMatchCount_1 === 1) {
- return null;
- }
- }
- },
- },
- Argument: {
- enter: function (node) {
- if (argMatcher(node)) {
- return null;
- }
- },
- },
- }));
- }
- exports.removeArgumentsFromDocument = removeArgumentsFromDocument;
- function removeFragmentSpreadFromDocument(config, doc) {
- function enter(node) {
- if (config.some(function (def) { return def.name === node.name.value; })) {
- return null;
- }
- }
- return nullIfDocIsEmpty(visitor_1.visit(doc, {
- FragmentSpread: { enter: enter },
- FragmentDefinition: { enter: enter },
- }));
- }
- exports.removeFragmentSpreadFromDocument = removeFragmentSpreadFromDocument;
- function getAllFragmentSpreadsFromSelectionSet(selectionSet) {
- var allFragments = [];
- selectionSet.selections.forEach(function (selection) {
- if ((storeUtils_1.isField(selection) || storeUtils_1.isInlineFragment(selection)) &&
- selection.selectionSet) {
- getAllFragmentSpreadsFromSelectionSet(selection.selectionSet).forEach(function (frag) { return allFragments.push(frag); });
- }
- else if (selection.kind === 'FragmentSpread') {
- allFragments.push(selection);
- }
- });
- return allFragments;
- }
- function buildQueryFromSelectionSet(document) {
- var definition = getFromAST_1.getMainDefinition(document);
- var definitionOperation = definition.operation;
- if (definitionOperation === 'query') {
- return document;
- }
- var modifiedDoc = visitor_1.visit(document, {
- OperationDefinition: {
- enter: function (node) {
- return tslib_1.__assign(tslib_1.__assign({}, node), { operation: 'query' });
- },
- },
- });
- return modifiedDoc;
- }
- exports.buildQueryFromSelectionSet = buildQueryFromSelectionSet;
- function removeClientSetsFromDocument(document) {
- getFromAST_1.checkDocument(document);
- var modifiedDoc = removeDirectivesFromDocument([
- {
- test: function (directive) { return directive.name.value === 'client'; },
- remove: true,
- },
- ], document);
- if (modifiedDoc) {
- modifiedDoc = visitor_1.visit(modifiedDoc, {
- FragmentDefinition: {
- enter: function (node) {
- if (node.selectionSet) {
- var isTypenameOnly = node.selectionSet.selections.every(function (selection) {
- return storeUtils_1.isField(selection) && selection.name.value === '__typename';
- });
- if (isTypenameOnly) {
- return null;
- }
- }
- },
- },
- });
- }
- return modifiedDoc;
- }
- exports.removeClientSetsFromDocument = removeClientSetsFromDocument;
- //# sourceMappingURL=transform.js.map
|