Socket
Socket
Sign inDemoInstall

apollo-utilities

Package Overview
Dependencies
Maintainers
4
Versions
105
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-utilities - npm Package Compare versions

Comparing version 1.0.26 to 1.0.27

275

lib/bundle.umd.js

@@ -587,23 +587,30 @@ (function (global, factory) {

}
function removeDirectivesFromSelectionSet(directives, selectionSet) {
if (!selectionSet.selections)
return selectionSet;
var agressiveRemove = directives.some(function (dir) { return dir.remove; });
selectionSet.selections = selectionSet.selections
function getSelectionsMatchingDirectiveFromSelectionSet(directives, selectionSet, invert, fieldsOnly) {
if (invert === void 0) { invert = false; }
if (fieldsOnly === void 0) { fieldsOnly = false; }
return selectionSet.selections
.map(function (selection) {
if (selection.kind !== 'Field' ||
!selection ||
!selection.directives)
return selection;
!selection.directives) {
return fieldsOnly ? null : selection;
}
var isMatch;
var directiveMatcher = getDirectiveMatcher(directives);
var remove;
selection.directives = selection.directives.filter(function (directive) {
var shouldKeep = !directiveMatcher(directive);
if (!remove && !shouldKeep && agressiveRemove)
remove = true;
if (!isMatch && !shouldKeep) {
isMatch = true;
}
return shouldKeep;
});
return remove ? null : selection;
return isMatch && invert ? null : selection;
})
.filter(function (x) { return !!x; });
.filter(function (s) { return !!s; });
}
function removeDirectivesFromSelectionSet(directives, selectionSet) {
if (!selectionSet.selections)
return selectionSet;
var agressiveRemove = directives.some(function (dir) { return dir.remove; });
selectionSet.selections = getSelectionsMatchingDirectiveFromSelectionSet(directives, selectionSet, agressiveRemove);
selectionSet.selections.forEach(function (selection) {

@@ -619,5 +626,69 @@ if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&

var docClone = cloneDeep(doc);
var removedArguments = [];
var removedFragments = [];
var aggressiveRemove = directives.some(function (directive) { return directive.remove; });
docClone.definitions.forEach(function (definition) {
removeDirectivesFromSelectionSet(directives, definition.selectionSet);
var operationDefinition = definition;
var originalSelectionSet = cloneDeep(operationDefinition.selectionSet);
var newSelectionSet = removeDirectivesFromSelectionSet(directives, operationDefinition.selectionSet);
if (aggressiveRemove && !!docClone) {
var matchingSelections = getSelectionsMatchingDirectiveFromSelectionSet(directives.map(function (config) { return ({
name: config.name,
test: config.test,
}); }), originalSelectionSet);
var remainingArguments_1 = getAllArgumentsFromSelectionSet(newSelectionSet);
removedArguments = removedArguments.concat(matchingSelections
.map(getAllArgumentsFromSelection)
.reduce(function (allArguments, selectionArguments) { return allArguments.concat(selectionArguments); }, [])
.filter(function (removedArg) {
return !remainingArguments_1.some(function (remainingArg) {
if (remainingArg.value.kind !== 'Variable' ||
!remainingArg.value)
return false;
if (removedArg.value.kind !== 'Variable' ||
!removedArg.value)
return false;
return (remainingArg.value.name.value === removedArg.value.name.value);
});
})
.map(function (argument) {
if (argument.value.kind !== 'Variable' ||
!argument.value)
return null;
return {
name: argument.value.name.value,
remove: aggressiveRemove,
};
})
.filter(function (node) { return !!node; }));
var remainingFragmentSpreads_1 = getAllFragmentSpreadsFromSelectionSet(newSelectionSet);
removedFragments = removedFragments.concat(matchingSelections
.map(getAllFragmentSpreadsFromSelection)
.reduce(function (allFragments, selectionFragments) { return allFragments.concat(selectionFragments); }, [])
.filter(function (removedFragment) {
return !remainingFragmentSpreads_1.some(function (remainingFragment) {
return remainingFragment.name.value === removedFragment.name.value;
});
})
.map(function (fragment) { return ({
name: fragment.name.value,
remove: aggressiveRemove,
}); }));
}
});
if (!docClone) {
return null;
}
if (removedFragments.length > 0) {
docClone = removeFragmentSpreadFromDocument(removedFragments, docClone);
if (!docClone) {
return null;
}
}
if (removedArguments.length > 0) {
docClone = removeArgumentsFromDocument(removedArguments, docClone);
if (!docClone) {
return null;
}
}
var operation = getOperationDefinitionOrDie(docClone);

@@ -655,9 +726,5 @@ var fragments = createFragmentMap(getFragmentDefinitions(docClone));

if (nestedCheck === void 0) { nestedCheck = true; }
if (!(selectionSet && selectionSet.selections)) {
return false;
}
var matchedSelections = selectionSet.selections.filter(function (selection) {
return filterSelectionSet(selectionSet, function (selection) {
return hasDirectivesInSelection(directives, selection, nestedCheck);
});
return matchedSelections.length > 0;
}

@@ -674,3 +741,4 @@ function hasDirectivesInSelection(directives, selection, nestedCheck) {

var matchedDirectives = selection.directives.filter(directiveMatcher);
return (matchedDirectives.length > 0 ||
var hasMatches = matchedDirectives.length > 0;
return (hasMatches ||
(nestedCheck &&

@@ -712,2 +780,169 @@ hasDirectivesInSelectionSet(directives, selection.selectionSet, nestedCheck)));

}
function getArgumentMatcher(config) {
return function (argument) {
return config.some(function (aConfig) {
if (argument.value.kind !== 'Variable' ||
!argument.value)
return false;
if (!argument.value.name)
return false;
if (aConfig.name === argument.value.name.value)
return true;
if (aConfig.test && aConfig.test(argument))
return true;
return false;
});
};
}
function getAllArgumentsFromSelectionSet(selectionSet) {
return selectionSet.selections
.map(getAllArgumentsFromSelection)
.reduce(function (allArguments, selectionArguments) {
return allArguments.concat(selectionArguments);
}, []);
}
function getAllArgumentsFromSelection(selection) {
if (selection.kind !== 'Field' || !selection) {
return [];
}
return selection.arguments || [];
}
function removeArgumentsFromDocument(config, query) {
var docClone = cloneDeep(query);
docClone.definitions.forEach(function (definition) {
var operationDefinition = definition;
var removeVariableConfig = config
.filter(function (aConfig) { return !!aConfig.name; })
.map(function (aConfig) { return ({
name: aConfig.name,
remove: aConfig.remove,
}); });
removeArgumentsFromSelectionSet(config, operationDefinition.selectionSet);
removeArgumentsFromOperationDefinition(removeVariableConfig, operationDefinition);
});
var operation = getOperationDefinitionOrDie(docClone);
var fragments = createFragmentMap(getFragmentDefinitions(docClone));
return isNotEmpty(operation, fragments) ? docClone : null;
}
function removeArgumentsFromOperationDefinition(config, definition) {
if (!definition.variableDefinitions)
return definition;
var aggressiveRemove = config.some(function (aConfig) { return aConfig.remove; });
definition.variableDefinitions = definition.variableDefinitions.filter(function (aDefinition) {
var shouldKeep = !config.some(function (aConfig) {
if (aConfig.name === aDefinition.variable.name.value)
return true;
if (aConfig.test && aConfig.test(aDefinition))
return true;
return false;
});
return shouldKeep;
});
return definition;
}
function removeArgumentsFromSelectionSet(config, selectionSet) {
if (!selectionSet.selections)
return selectionSet;
var aggressiveRemove = config.some(function (aConfig) { return aConfig.remove; });
selectionSet.selections = selectionSet.selections
.map(function (selection) {
if (selection.kind !== 'Field' ||
!selection ||
!selection.arguments) {
return selection;
}
var remove;
var argumentMatcher = getArgumentMatcher(config);
selection.arguments = selection.arguments.filter(function (argument) {
var shouldKeep = !argumentMatcher(argument);
if (!remove && !shouldKeep && aggressiveRemove) {
remove = true;
}
return shouldKeep;
});
return remove ? null : selection;
})
.filter(function (x) { return !!x; });
selectionSet.selections.forEach(function (selection) {
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet) {
removeArgumentsFromSelectionSet(config, selection.selectionSet);
}
});
return selectionSet;
}
function hasFragmentSpreadInSelection(config, selection) {
if (selection.kind !== 'FragmentSpread' ||
!selection) {
return false;
}
return config.some(function (aConfig) {
if (aConfig.name === selection.name.value)
return true;
if (aConfig.test && aConfig.test(selection))
return true;
return false;
});
}
function removeFragmentSpreadFromDocument(config, query) {
var docClone = cloneDeep(query);
docClone.definitions.forEach(function (definition) {
removeFragmentSpreadFromSelectionSet(config, definition.selectionSet);
});
docClone.definitions = removeFragmentSpreadFromDefinitions(config
.filter(function (aConfig) { return !!aConfig.name; })
.map(function (aConfig) { return ({ name: aConfig.name }); }), docClone.definitions);
var operation = getOperationDefinitionOrDie(docClone);
var fragments = createFragmentMap(getFragmentDefinitions(docClone));
return isNotEmpty(operation, fragments) ? docClone : null;
}
function removeFragmentSpreadFromDefinitions(config, definitions) {
return definitions.filter(function (definition) {
if (definition.kind !== 'FragmentDefinition' ||
!definition) {
return true;
}
return !config.some(function (aConfig) {
if (aConfig.name && aConfig.name === definition.name.value)
return true;
if (aConfig.test && aConfig.test(definition))
return true;
return false;
});
});
}
function removeFragmentSpreadFromSelectionSet(config, selectionSet) {
if (!selectionSet.selections)
return selectionSet;
selectionSet.selections = selectionSet.selections.filter(function (selection) { return !hasFragmentSpreadInSelection(config, selection); });
selectionSet.selections.forEach(function (selection) {
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet) {
removeFragmentSpreadFromSelectionSet(config, selection.selectionSet);
}
});
return selectionSet;
}
function getAllFragmentSpreadsFromSelectionSet(selectionSet) {
return selectionSet.selections
.map(getAllFragmentSpreadsFromSelection)
.reduce(function (allFragments, selectionFragments) { return allFragments.concat(selectionFragments); }, []);
}
function getAllFragmentSpreadsFromSelection(selection) {
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet) {
return getAllFragmentSpreadsFromSelectionSet(selection.selectionSet);
}
else if (selection.kind === 'FragmentSpread' &&
selection) {
return [selection];
}
return [];
}
function filterSelectionSet(selectionSet, filter) {
if (!(selectionSet && selectionSet.selections)) {
return false;
}
return selectionSet.selections.filter(filter).length > 0;
}

@@ -846,2 +1081,4 @@ function getEnv() {

exports.getDirectivesFromDocument = getDirectivesFromDocument;
exports.removeArgumentsFromDocument = removeArgumentsFromDocument;
exports.removeFragmentSpreadFromDocument = removeFragmentSpreadFromDocument;
exports.isScalarValue = isScalarValue;

@@ -848,0 +1085,0 @@ exports.isNumberValue = isNumberValue;

23

lib/transform.d.ts

@@ -1,15 +0,24 @@

import { DocumentNode, DirectiveNode } from 'graphql';
export declare type RemoveDirectiveConfig = {
import { DocumentNode, DirectiveNode, FragmentDefinitionNode, ArgumentNode, FragmentSpreadNode, VariableDefinitionNode } from 'graphql';
export declare type RemoveNodeConfig<N> = {
name?: string;
test?: (directive: DirectiveNode) => boolean;
test?: (node: N) => boolean;
remove?: boolean;
};
export declare type GetNodeConfig<N> = {
name?: string;
test?: (node: N) => boolean;
};
export declare type RemoveDirectiveConfig = RemoveNodeConfig<DirectiveNode>;
export declare type GetDirectiveConfig = GetNodeConfig<DirectiveNode>;
export declare type RemoveArgumentsConfig = RemoveNodeConfig<ArgumentNode>;
export declare type GetFragmentSpreadConfig = GetNodeConfig<FragmentSpreadNode>;
export declare type RemoveFragmentSpreadConfig = RemoveNodeConfig<FragmentSpreadNode>;
export declare type RemoveFragmentDefinitionConfig = RemoveNodeConfig<FragmentDefinitionNode>;
export declare type RemoveVariableDefinitionConfig = RemoveNodeConfig<VariableDefinitionNode>;
export declare function removeDirectivesFromDocument(directives: RemoveDirectiveConfig[], doc: DocumentNode): DocumentNode | null;
export declare function addTypenameToDocument(doc: DocumentNode): DocumentNode;
export declare function removeConnectionDirectiveFromDocument(doc: DocumentNode): DocumentNode;
export declare type GetDirectiveConfig = {
name?: string;
test?: (directive: DirectiveNode) => boolean;
};
export declare function getDirectivesFromDocument(directives: GetDirectiveConfig[], doc: DocumentNode, includeAllFragments?: boolean): DocumentNode | null;
export declare function removeArgumentsFromDocument(config: RemoveArgumentsConfig[], query: DocumentNode): DocumentNode | null;
export declare function removeFragmentSpreadFromDocument(config: RemoveFragmentSpreadConfig[], query: DocumentNode): DocumentNode | null;
//# sourceMappingURL=transform.d.ts.map

@@ -55,23 +55,30 @@ import { cloneDeep } from './util/cloneDeep';

}
function removeDirectivesFromSelectionSet(directives, selectionSet) {
if (!selectionSet.selections)
return selectionSet;
var agressiveRemove = directives.some(function (dir) { return dir.remove; });
selectionSet.selections = selectionSet.selections
function getSelectionsMatchingDirectiveFromSelectionSet(directives, selectionSet, invert, fieldsOnly) {
if (invert === void 0) { invert = false; }
if (fieldsOnly === void 0) { fieldsOnly = false; }
return selectionSet.selections
.map(function (selection) {
if (selection.kind !== 'Field' ||
!selection ||
!selection.directives)
return selection;
!selection.directives) {
return fieldsOnly ? null : selection;
}
var isMatch;
var directiveMatcher = getDirectiveMatcher(directives);
var remove;
selection.directives = selection.directives.filter(function (directive) {
var shouldKeep = !directiveMatcher(directive);
if (!remove && !shouldKeep && agressiveRemove)
remove = true;
if (!isMatch && !shouldKeep) {
isMatch = true;
}
return shouldKeep;
});
return remove ? null : selection;
return isMatch && invert ? null : selection;
})
.filter(function (x) { return !!x; });
.filter(function (s) { return !!s; });
}
function removeDirectivesFromSelectionSet(directives, selectionSet) {
if (!selectionSet.selections)
return selectionSet;
var agressiveRemove = directives.some(function (dir) { return dir.remove; });
selectionSet.selections = getSelectionsMatchingDirectiveFromSelectionSet(directives, selectionSet, agressiveRemove);
selectionSet.selections.forEach(function (selection) {

@@ -87,5 +94,69 @@ if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&

var docClone = cloneDeep(doc);
var removedArguments = [];
var removedFragments = [];
var aggressiveRemove = directives.some(function (directive) { return directive.remove; });
docClone.definitions.forEach(function (definition) {
removeDirectivesFromSelectionSet(directives, definition.selectionSet);
var operationDefinition = definition;
var originalSelectionSet = cloneDeep(operationDefinition.selectionSet);
var newSelectionSet = removeDirectivesFromSelectionSet(directives, operationDefinition.selectionSet);
if (aggressiveRemove && !!docClone) {
var matchingSelections = getSelectionsMatchingDirectiveFromSelectionSet(directives.map(function (config) { return ({
name: config.name,
test: config.test,
}); }), originalSelectionSet);
var remainingArguments_1 = getAllArgumentsFromSelectionSet(newSelectionSet);
removedArguments = removedArguments.concat(matchingSelections
.map(getAllArgumentsFromSelection)
.reduce(function (allArguments, selectionArguments) { return allArguments.concat(selectionArguments); }, [])
.filter(function (removedArg) {
return !remainingArguments_1.some(function (remainingArg) {
if (remainingArg.value.kind !== 'Variable' ||
!remainingArg.value)
return false;
if (removedArg.value.kind !== 'Variable' ||
!removedArg.value)
return false;
return (remainingArg.value.name.value === removedArg.value.name.value);
});
})
.map(function (argument) {
if (argument.value.kind !== 'Variable' ||
!argument.value)
return null;
return {
name: argument.value.name.value,
remove: aggressiveRemove,
};
})
.filter(function (node) { return !!node; }));
var remainingFragmentSpreads_1 = getAllFragmentSpreadsFromSelectionSet(newSelectionSet);
removedFragments = removedFragments.concat(matchingSelections
.map(getAllFragmentSpreadsFromSelection)
.reduce(function (allFragments, selectionFragments) { return allFragments.concat(selectionFragments); }, [])
.filter(function (removedFragment) {
return !remainingFragmentSpreads_1.some(function (remainingFragment) {
return remainingFragment.name.value === removedFragment.name.value;
});
})
.map(function (fragment) { return ({
name: fragment.name.value,
remove: aggressiveRemove,
}); }));
}
});
if (!docClone) {
return null;
}
if (removedFragments.length > 0) {
docClone = removeFragmentSpreadFromDocument(removedFragments, docClone);
if (!docClone) {
return null;
}
}
if (removedArguments.length > 0) {
docClone = removeArgumentsFromDocument(removedArguments, docClone);
if (!docClone) {
return null;
}
}
var operation = getOperationDefinitionOrDie(docClone);

@@ -123,9 +194,5 @@ var fragments = createFragmentMap(getFragmentDefinitions(docClone));

if (nestedCheck === void 0) { nestedCheck = true; }
if (!(selectionSet && selectionSet.selections)) {
return false;
}
var matchedSelections = selectionSet.selections.filter(function (selection) {
return filterSelectionSet(selectionSet, function (selection) {
return hasDirectivesInSelection(directives, selection, nestedCheck);
});
return matchedSelections.length > 0;
}

@@ -142,3 +209,4 @@ function hasDirectivesInSelection(directives, selection, nestedCheck) {

var matchedDirectives = selection.directives.filter(directiveMatcher);
return (matchedDirectives.length > 0 ||
var hasMatches = matchedDirectives.length > 0;
return (hasMatches ||
(nestedCheck &&

@@ -180,2 +248,193 @@ hasDirectivesInSelectionSet(directives, selection.selectionSet, nestedCheck)));

}
function getArgumentMatcher(config) {
return function (argument) {
return config.some(function (aConfig) {
if (argument.value.kind !== 'Variable' ||
!argument.value)
return false;
if (!argument.value.name)
return false;
if (aConfig.name === argument.value.name.value)
return true;
if (aConfig.test && aConfig.test(argument))
return true;
return false;
});
};
}
function hasArgumentsInSelectionSet(config, selectionSet, nestedCheck) {
if (nestedCheck === void 0) { nestedCheck = false; }
return filterSelectionSet(selectionSet, function (selection) {
return hasArgumentsInSelection(config, selection, nestedCheck);
});
}
function hasArgumentsInSelection(config, selection, nestedCheck) {
if (nestedCheck === void 0) { nestedCheck = false; }
if (selection.kind !== 'Field' || !selection) {
return true;
}
if (!selection.arguments) {
return false;
}
var matcher = getArgumentMatcher(config);
var matchedArguments = selection.arguments.filter(matcher);
return (matchedArguments.length > 0 ||
(nestedCheck &&
hasArgumentsInSelectionSet(config, selection.selectionSet, nestedCheck)));
}
function getAllArgumentsFromSelectionSet(selectionSet) {
return selectionSet.selections
.map(getAllArgumentsFromSelection)
.reduce(function (allArguments, selectionArguments) {
return allArguments.concat(selectionArguments);
}, []);
}
function getAllArgumentsFromSelection(selection) {
if (selection.kind !== 'Field' || !selection) {
return [];
}
return selection.arguments || [];
}
export function removeArgumentsFromDocument(config, query) {
var docClone = cloneDeep(query);
docClone.definitions.forEach(function (definition) {
var operationDefinition = definition;
var removeVariableConfig = config
.filter(function (aConfig) { return !!aConfig.name; })
.map(function (aConfig) { return ({
name: aConfig.name,
remove: aConfig.remove,
}); });
removeArgumentsFromSelectionSet(config, operationDefinition.selectionSet);
removeArgumentsFromOperationDefinition(removeVariableConfig, operationDefinition);
});
var operation = getOperationDefinitionOrDie(docClone);
var fragments = createFragmentMap(getFragmentDefinitions(docClone));
return isNotEmpty(operation, fragments) ? docClone : null;
}
function removeArgumentsFromOperationDefinition(config, definition) {
if (!definition.variableDefinitions)
return definition;
var aggressiveRemove = config.some(function (aConfig) { return aConfig.remove; });
var remove;
definition.variableDefinitions = definition.variableDefinitions.filter(function (aDefinition) {
var shouldKeep = !config.some(function (aConfig) {
if (aConfig.name === aDefinition.variable.name.value)
return true;
if (aConfig.test && aConfig.test(aDefinition))
return true;
return false;
});
if (!remove && !shouldKeep && aggressiveRemove) {
remove = true;
}
return shouldKeep;
});
return definition;
}
function removeArgumentsFromSelectionSet(config, selectionSet) {
if (!selectionSet.selections)
return selectionSet;
var aggressiveRemove = config.some(function (aConfig) { return aConfig.remove; });
selectionSet.selections = selectionSet.selections
.map(function (selection) {
if (selection.kind !== 'Field' ||
!selection ||
!selection.arguments) {
return selection;
}
var remove;
var argumentMatcher = getArgumentMatcher(config);
selection.arguments = selection.arguments.filter(function (argument) {
var shouldKeep = !argumentMatcher(argument);
if (!remove && !shouldKeep && aggressiveRemove) {
remove = true;
}
return shouldKeep;
});
return remove ? null : selection;
})
.filter(function (x) { return !!x; });
selectionSet.selections.forEach(function (selection) {
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet) {
removeArgumentsFromSelectionSet(config, selection.selectionSet);
}
});
return selectionSet;
}
function hasFragmentSpreadInSelection(config, selection) {
if (selection.kind !== 'FragmentSpread' ||
!selection) {
return false;
}
return config.some(function (aConfig) {
if (aConfig.name === selection.name.value)
return true;
if (aConfig.test && aConfig.test(selection))
return true;
return false;
});
}
export function removeFragmentSpreadFromDocument(config, query) {
var docClone = cloneDeep(query);
docClone.definitions.forEach(function (definition) {
removeFragmentSpreadFromSelectionSet(config, definition.selectionSet);
});
docClone.definitions = removeFragmentSpreadFromDefinitions(config
.filter(function (aConfig) { return !!aConfig.name; })
.map(function (aConfig) { return ({ name: aConfig.name }); }), docClone.definitions);
var operation = getOperationDefinitionOrDie(docClone);
var fragments = createFragmentMap(getFragmentDefinitions(docClone));
return isNotEmpty(operation, fragments) ? docClone : null;
}
function removeFragmentSpreadFromDefinitions(config, definitions) {
return definitions.filter(function (definition) {
if (definition.kind !== 'FragmentDefinition' ||
!definition) {
return true;
}
return !config.some(function (aConfig) {
if (aConfig.name && aConfig.name === definition.name.value)
return true;
if (aConfig.test && aConfig.test(definition))
return true;
return false;
});
});
}
function removeFragmentSpreadFromSelectionSet(config, selectionSet) {
if (!selectionSet.selections)
return selectionSet;
selectionSet.selections = selectionSet.selections.filter(function (selection) { return !hasFragmentSpreadInSelection(config, selection); });
selectionSet.selections.forEach(function (selection) {
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet) {
removeFragmentSpreadFromSelectionSet(config, selection.selectionSet);
}
});
return selectionSet;
}
function getAllFragmentSpreadsFromSelectionSet(selectionSet) {
return selectionSet.selections
.map(getAllFragmentSpreadsFromSelection)
.reduce(function (allFragments, selectionFragments) { return allFragments.concat(selectionFragments); }, []);
}
function getAllFragmentSpreadsFromSelection(selection) {
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet) {
return getAllFragmentSpreadsFromSelectionSet(selection.selectionSet);
}
else if (selection.kind === 'FragmentSpread' &&
selection) {
return [selection];
}
return [];
}
function filterSelectionSet(selectionSet, filter) {
if (!(selectionSet && selectionSet.selections)) {
return false;
}
return selectionSet.selections.filter(filter).length > 0;
}
//# sourceMappingURL=transform.js.map
{
"name": "apollo-utilities",
"version": "1.0.26",
"version": "1.0.27",
"description": "Utilities for working with GraphQL ASTs",

@@ -57,3 +57,3 @@ "author": "James Baxley <james@meteor.com>",

},
"gitHead": "1da881cce56a1cde8e2fd5eb86e9429f718f09b4"
"gitHead": "4c78a05160c4b0f236eeae99ed7ea4d27c8332e4"
}

@@ -0,0 +0,0 @@ import gql from 'graphql-tag';

@@ -0,0 +0,0 @@ import { print } from 'graphql/language/printer';

@@ -0,0 +0,0 @@ import { print } from 'graphql/language/printer';

@@ -13,6 +13,220 @@ import { print } from 'graphql/language/printer';

removeConnectionDirectiveFromDocument,
removeArgumentsFromDocument,
removeFragmentSpreadFromDocument,
} from '../transform';
import { getQueryDefinition } from '../getFromAST';
describe('removeArgumentsFromDocument', () => {
it('should remove a single variable', () => {
const query = gql`
query Simple($variable: String!) {
field(usingVariable: $variable) {
child
foo
}
network
}
`;
const expected = gql`
query Simple {
field {
child
foo
}
network
}
`;
const doc = removeArgumentsFromDocument([{ name: 'variable' }], query);
expect(print(doc)).toBe(print(expected));
});
it('should remove a single variable and the field from the query', () => {
const query = gql`
query Simple($variable: String!) {
field(usingVariable: $variable) {
child
foo
}
network
}
`;
const expected = gql`
query Simple {
network
}
`;
const doc = removeArgumentsFromDocument(
[{ name: 'variable', remove: true }],
query,
);
expect(print(doc)).toBe(print(expected));
});
});
describe('removeFragmentSpreadFromDocument', () => {
it('should remove a named fragment spread', () => {
const query = gql`
query Simple {
...FragmentSpread
property
...ValidSpread
}
fragment FragmentSpread on Thing {
foo
bar
baz
}
fragment ValidSpread on Thing {
oof
rab
zab
}
`;
const expected = gql`
query Simple {
property
...ValidSpread
}
fragment ValidSpread on Thing {
oof
rab
zab
}
`;
const doc = removeFragmentSpreadFromDocument(
[{ name: 'FragmentSpread', remove: true }],
query,
);
expect(print(doc)).toBe(print(expected));
});
});
describe('removeDirectivesFromDocument', () => {
it('should not remove unused variable definitions unless the field is removed', () => {
const query = gql`
query Simple($variable: String!) {
field(usingVariable: $variable) @client
networkField
}
`;
const expected = gql`
query Simple($variable: String!) {
field(usingVariable: $variable)
networkField
}
`;
const doc = removeDirectivesFromDocument([{ name: 'client' }], query);
expect(print(doc)).toBe(print(expected));
});
it('should remove unused variable definitions associated with the removed directive', () => {
const query = gql`
query Simple($variable: String!) {
field(usingVariable: $variable) @client
networkField
}
`;
const expected = gql`
query Simple {
networkField
}
`;
const doc = removeDirectivesFromDocument(
[{ name: 'client', remove: true }],
query,
);
expect(print(doc)).toBe(print(expected));
});
it('should not remove used variable definitions', () => {
const query = gql`
query Simple($variable: String!) {
field(usingVariable: $variable) @client
networkField(usingVariable: $variable)
}
`;
const expected = gql`
query Simple($variable: String!) {
networkField(usingVariable: $variable)
}
`;
const doc = removeDirectivesFromDocument(
[{ name: 'client', remove: true }],
query,
);
expect(print(doc)).toBe(print(expected));
});
it('should remove fragment spreads and definitions associated with the removed directive', () => {
const query = gql`
query Simple {
networkField
field @client {
...ClientFragment
}
}
fragment ClientFragment on Thing {
otherField
bar
}
`;
const expected = gql`
query Simple {
networkField
}
`;
const doc = removeDirectivesFromDocument(
[{ name: 'client', remove: true }],
query,
);
expect(print(doc)).toBe(print(expected));
});
it('should not remove fragment spreads and definitions used without the removed directive', () => {
const query = gql`
query Simple {
networkField {
...ClientFragment
}
field @client {
...ClientFragment
}
}
fragment ClientFragment on Thing {
otherField
bar
}
`;
const expected = gql`
query Simple {
networkField {
...ClientFragment
}
}
fragment ClientFragment on Thing {
otherField
bar
}
`;
const doc = removeDirectivesFromDocument(
[{ name: 'client', remove: true }],
query,
);
expect(print(doc)).toBe(print(expected));
});
it('should remove a simple directive', () => {

@@ -33,2 +247,3 @@ const query = gql`

});
it('should remove a simple directive [test function]', () => {

@@ -50,2 +265,3 @@ const query = gql`

});
it('should remove only the wanted directive', () => {

@@ -105,2 +321,3 @@ const query = gql`

});
it('should remove multiple directives of different kinds in the query', () => {

@@ -131,2 +348,3 @@ const query = gql`

});
it('should remove a simple directive and its field if needed', () => {

@@ -151,2 +369,3 @@ const query = gql`

});
it('should remove a simple directive [test function]', () => {

@@ -169,2 +388,3 @@ const query = gql`

});
it('should return null if the query is no longer valid', () => {

@@ -184,2 +404,3 @@ const query = gql`

});
it('should return null if the query is no longer valid [test function]', () => {

@@ -196,2 +417,3 @@ const query = gql`

});
it('should return null only if the query is not valid', () => {

@@ -215,2 +437,3 @@ const query = gql`

});
it('should return null only if the query is not valid through nested fragments', () => {

@@ -238,2 +461,3 @@ const query = gql`

});
it('should only remove values asked through nested fragments', () => {

@@ -539,2 +763,3 @@ const query = gql`

});
it('should correctly remove connections', () => {

@@ -683,2 +908,3 @@ let testQuery = gql`

);
expect(print(doc)).toBe(print(expected));

@@ -868,2 +1094,3 @@ });

});
it('should get mutation with client fields', () => {

@@ -901,2 +1128,3 @@ const query = gql`

});
describe('includeAllFragments', () => {

@@ -903,0 +1131,0 @@ it('= false: should remove the values without a client in fragment', () => {

@@ -0,0 +0,0 @@ // Provides the methods that allow QueryManager to handle

@@ -0,0 +0,0 @@ import { DocumentNode, FragmentDefinitionNode } from 'graphql';

@@ -0,0 +0,0 @@ export * from './directives';

@@ -10,2 +10,6 @@ import {

FragmentDefinitionNode,
ArgumentNode,
FragmentSpreadNode,
VariableDefinitionNode,
VariableNode,
} from 'graphql';

@@ -23,2 +27,25 @@

export type RemoveNodeConfig<N> = {
name?: string;
test?: (node: N) => boolean;
remove?: boolean;
};
export type GetNodeConfig<N> = {
name?: string;
test?: (node: N) => boolean;
};
export type RemoveDirectiveConfig = RemoveNodeConfig<DirectiveNode>;
export type GetDirectiveConfig = GetNodeConfig<DirectiveNode>;
export type RemoveArgumentsConfig = RemoveNodeConfig<ArgumentNode>;
export type GetFragmentSpreadConfig = GetNodeConfig<FragmentSpreadNode>;
export type RemoveFragmentSpreadConfig = RemoveNodeConfig<FragmentSpreadNode>;
export type RemoveFragmentDefinitionConfig = RemoveNodeConfig<
FragmentDefinitionNode
>;
export type RemoveVariableDefinitionConfig = RemoveNodeConfig<
VariableDefinitionNode
>;
const TYPENAME_FIELD: FieldNode = {

@@ -103,19 +130,9 @@ kind: 'Field',

export type RemoveDirectiveConfig = {
name?: string;
test?: (directive: DirectiveNode) => boolean;
remove?: boolean;
};
function removeDirectivesFromSelectionSet(
directives: RemoveDirectiveConfig[],
function getSelectionsMatchingDirectiveFromSelectionSet(
directives: GetDirectiveConfig[],
selectionSet: SelectionSetNode,
): SelectionSetNode {
if (!selectionSet.selections) return selectionSet;
// if any of the directives are set to remove this selectionSet, remove it
const agressiveRemove = directives.some(
(dir: RemoveDirectiveConfig) => dir.remove,
);
selectionSet.selections = selectionSet.selections
invert: boolean = false,
fieldsOnly: boolean = false,
): SelectionNode[] {
return selectionSet.selections
.map(selection => {

@@ -126,10 +143,14 @@ if (

!selection.directives
)
return selection;
) {
return fieldsOnly ? null : selection;
}
let isMatch: boolean;
const directiveMatcher = getDirectiveMatcher(directives);
let remove: boolean;
selection.directives = selection.directives.filter(directive => {
const shouldKeep = !directiveMatcher(directive);
if (!remove && !shouldKeep && agressiveRemove) remove = true;
if (!isMatch && !shouldKeep) {
isMatch = true;
}

@@ -139,6 +160,23 @@ return shouldKeep;

return remove ? null : selection;
return isMatch && invert ? null : selection;
})
.filter(x => !!x);
.filter(s => !!s);
}
function removeDirectivesFromSelectionSet(
directives: RemoveDirectiveConfig[],
selectionSet: SelectionSetNode,
): SelectionSetNode {
if (!selectionSet.selections) return selectionSet;
// if any of the directives are set to remove this selectionSet, remove it
const agressiveRemove = directives.some(
(dir: RemoveDirectiveConfig) => dir.remove,
);
selectionSet.selections = getSelectionsMatchingDirectiveFromSelectionSet(
directives,
selectionSet,
agressiveRemove,
);
selectionSet.selections.forEach(selection => {

@@ -160,12 +198,123 @@ if (

): DocumentNode | null {
const docClone = cloneDeep(doc);
let docClone = cloneDeep(doc);
let removedArguments: RemoveArgumentsConfig[] = [];
let removedFragments: RemoveFragmentSpreadConfig[] = [];
const aggressiveRemove = directives.some(directive => directive.remove);
docClone.definitions.forEach((definition: DefinitionNode) => {
removeDirectivesFromSelectionSet(
const operationDefinition = definition as OperationDefinitionNode;
const originalSelectionSet = cloneDeep(operationDefinition.selectionSet);
const newSelectionSet = removeDirectivesFromSelectionSet(
directives,
(definition as OperationDefinitionNode).selectionSet,
operationDefinition.selectionSet,
);
if (aggressiveRemove && !!docClone) {
const matchingSelections = getSelectionsMatchingDirectiveFromSelectionSet(
directives.map(config => ({
name: config.name,
test: config.test,
})),
originalSelectionSet,
);
const remainingArguments = getAllArgumentsFromSelectionSet(
newSelectionSet,
);
removedArguments = [
...removedArguments,
...matchingSelections
.map(getAllArgumentsFromSelection)
.reduce(
(allArguments, selectionArguments) => [
...allArguments,
...selectionArguments,
],
[],
)
.filter(
removedArg =>
!remainingArguments.some(remainingArg => {
if (
remainingArg.value.kind !== 'Variable' ||
!(remainingArg.value as VariableNode)
)
return false;
if (
removedArg.value.kind !== 'Variable' ||
!(removedArg.value as VariableNode)
)
return false;
return (
remainingArg.value.name.value === removedArg.value.name.value
);
}),
)
.map(argument => {
if (
argument.value.kind !== 'Variable' ||
!(argument.value as VariableNode)
)
return null;
return {
name: argument.value.name.value,
remove: aggressiveRemove,
};
})
.filter(node => !!node),
];
const remainingFragmentSpreads = getAllFragmentSpreadsFromSelectionSet(
newSelectionSet,
);
removedFragments = [
...removedFragments,
...matchingSelections
.map(getAllFragmentSpreadsFromSelection)
.reduce(
(allFragments, selectionFragments) => [
...allFragments,
...selectionFragments,
],
[],
)
.filter(
removedFragment =>
!remainingFragmentSpreads.some(
remainingFragment =>
remainingFragment.name.value === removedFragment.name.value,
),
)
.map(fragment => ({
name: fragment.name.value,
remove: aggressiveRemove,
})),
];
}
});
if (!docClone) {
return null;
}
if (removedFragments.length > 0) {
docClone = removeFragmentSpreadFromDocument(removedFragments, docClone);
if (!docClone) {
return null;
}
}
if (removedArguments.length > 0) {
docClone = removeArgumentsFromDocument(removedArguments, docClone);
if (!docClone) {
return null;
}
}
const operation = getOperationDefinitionOrDie(docClone);
const fragments = createFragmentMap(getFragmentDefinitions(docClone));
return isNotEmpty(operation, fragments) ? docClone : null;

@@ -212,7 +361,2 @@ }

export type GetDirectiveConfig = {
name?: string;
test?: (directive: DirectiveNode) => boolean;
};
function hasDirectivesInSelectionSet(

@@ -223,9 +367,5 @@ directives: GetDirectiveConfig[],

): boolean {
if (!(selectionSet && selectionSet.selections)) {
return false;
}
const matchedSelections = selectionSet.selections.filter(selection => {
return hasDirectivesInSelection(directives, selection, nestedCheck);
});
return matchedSelections.length > 0;
return filterSelectionSet(selectionSet, selection =>
hasDirectivesInSelection(directives, selection, nestedCheck),
);
}

@@ -247,4 +387,6 @@

const matchedDirectives = selection.directives.filter(directiveMatcher);
const hasMatches = matchedDirectives.length > 0;
return (
matchedDirectives.length > 0 ||
hasMatches ||
(nestedCheck &&

@@ -280,2 +422,3 @@ hasDirectivesInSelectionSet(

}
return selection;

@@ -311,1 +454,301 @@ });

}
function getArgumentMatcher(config: RemoveArgumentsConfig[]) {
return (argument: ArgumentNode): Boolean => {
return config.some((aConfig: RemoveArgumentsConfig) => {
if (
argument.value.kind !== 'Variable' ||
!(argument.value as VariableNode)
)
return false;
if (!argument.value.name) return false;
if (aConfig.name === argument.value.name.value) return true;
if (aConfig.test && aConfig.test(argument)) return true;
return false;
});
};
}
function hasArgumentsInSelectionSet(
config: RemoveArgumentsConfig[],
selectionSet: SelectionSetNode,
nestedCheck: boolean = false,
): boolean {
return filterSelectionSet(selectionSet, selection =>
hasArgumentsInSelection(config, selection, nestedCheck),
);
}
function hasArgumentsInSelection(
config: RemoveArgumentsConfig[],
selection: SelectionNode,
nestedCheck: boolean = false,
): boolean {
// Selection is a FragmentSpread or InlineFragment, ignore (include it)...
if (selection.kind !== 'Field' || !(selection as FieldNode)) {
return true;
}
if (!selection.arguments) {
return false;
}
const matcher = getArgumentMatcher(config);
const matchedArguments = selection.arguments.filter(matcher);
return (
matchedArguments.length > 0 ||
(nestedCheck &&
hasArgumentsInSelectionSet(config, selection.selectionSet, nestedCheck))
);
}
function getAllArgumentsFromSelectionSet(
selectionSet: SelectionSetNode,
): ArgumentNode[] {
return selectionSet.selections
.map(getAllArgumentsFromSelection)
.reduce((allArguments, selectionArguments) => {
return [...allArguments, ...selectionArguments];
}, []);
}
function getAllArgumentsFromSelection(
selection: SelectionNode,
): ArgumentNode[] {
if (selection.kind !== 'Field' || !(selection as FieldNode)) {
return [];
}
return selection.arguments || [];
}
export function removeArgumentsFromDocument(
config: RemoveArgumentsConfig[],
query: DocumentNode,
): DocumentNode | null {
const docClone = cloneDeep(query);
docClone.definitions.forEach((definition: DefinitionNode) => {
const operationDefinition = definition as OperationDefinitionNode;
const removeVariableConfig = config
.filter(aConfig => !!aConfig.name)
.map(aConfig => ({
name: aConfig.name,
remove: aConfig.remove,
}));
removeArgumentsFromSelectionSet(config, operationDefinition.selectionSet);
removeArgumentsFromOperationDefinition(
removeVariableConfig,
operationDefinition,
);
});
const operation = getOperationDefinitionOrDie(docClone);
const fragments = createFragmentMap(getFragmentDefinitions(docClone));
return isNotEmpty(operation, fragments) ? docClone : null;
}
function removeArgumentsFromOperationDefinition(
config: RemoveVariableDefinitionConfig[],
definition: OperationDefinitionNode,
): OperationDefinitionNode {
if (!definition.variableDefinitions) return definition;
// if any of the config is set to remove this argument, remove it
const aggressiveRemove = config.some(
(aConfig: RemoveVariableDefinitionConfig) => aConfig.remove,
);
let remove: boolean;
definition.variableDefinitions = definition.variableDefinitions.filter(
aDefinition => {
const shouldKeep = !config.some(aConfig => {
if (aConfig.name === aDefinition.variable.name.value) return true;
if (aConfig.test && aConfig.test(aDefinition)) return true;
return false;
});
if (!remove && !shouldKeep && aggressiveRemove) {
remove = true;
}
return shouldKeep;
},
);
return definition;
}
function removeArgumentsFromSelectionSet(
config: RemoveArgumentsConfig[],
selectionSet: SelectionSetNode,
): SelectionSetNode {
if (!selectionSet.selections) return selectionSet;
// if any of the config is set to remove this selectionSet, remove it
const aggressiveRemove = config.some(
(aConfig: RemoveArgumentsConfig) => aConfig.remove,
);
selectionSet.selections = selectionSet.selections
.map(selection => {
if (
selection.kind !== 'Field' ||
!(selection as FieldNode) ||
!selection.arguments
) {
return selection;
}
let remove: boolean;
const argumentMatcher = getArgumentMatcher(config);
selection.arguments = selection.arguments.filter(argument => {
const shouldKeep = !argumentMatcher(argument);
if (!remove && !shouldKeep && aggressiveRemove) {
remove = true;
}
return shouldKeep;
});
return remove ? null : selection;
})
.filter(x => !!x);
selectionSet.selections.forEach(selection => {
if (
(selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet
) {
removeArgumentsFromSelectionSet(config, selection.selectionSet);
}
});
return selectionSet;
}
function hasFragmentSpreadInSelection(
config: RemoveFragmentSpreadConfig[],
selection: SelectionNode,
): boolean {
if (
selection.kind !== 'FragmentSpread' ||
!(selection as FragmentSpreadNode)
) {
return false;
}
return config.some(aConfig => {
if (aConfig.name === selection.name.value) return true;
if (aConfig.test && aConfig.test(selection)) return true;
return false;
});
}
export function removeFragmentSpreadFromDocument(
config: RemoveFragmentSpreadConfig[],
query: DocumentNode,
): DocumentNode | null {
const docClone = cloneDeep(query);
docClone.definitions.forEach((definition: DefinitionNode) => {
removeFragmentSpreadFromSelectionSet(
config,
(definition as OperationDefinitionNode).selectionSet,
);
});
docClone.definitions = removeFragmentSpreadFromDefinitions(
config
.filter(aConfig => !!aConfig.name)
.map(aConfig => ({ name: aConfig.name })),
docClone.definitions,
);
const operation = getOperationDefinitionOrDie(docClone);
const fragments = createFragmentMap(getFragmentDefinitions(docClone));
return isNotEmpty(operation, fragments) ? docClone : null;
}
function removeFragmentSpreadFromDefinitions(
config: RemoveFragmentDefinitionConfig[],
definitions: DefinitionNode[],
): DefinitionNode[] {
return definitions.filter(definition => {
if (
definition.kind !== 'FragmentDefinition' ||
!(definition as FragmentDefinitionNode)
) {
return true;
}
return !config.some(aConfig => {
if (aConfig.name && aConfig.name === definition.name.value) return true;
if (aConfig.test && aConfig.test(definition)) return true;
return false;
});
});
}
function removeFragmentSpreadFromSelectionSet(
config: RemoveFragmentSpreadConfig[],
selectionSet: SelectionSetNode,
): SelectionSetNode {
if (!selectionSet.selections) return selectionSet;
selectionSet.selections = selectionSet.selections.filter(
selection => !hasFragmentSpreadInSelection(config, selection),
);
selectionSet.selections.forEach(selection => {
if (
(selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet
) {
removeFragmentSpreadFromSelectionSet(config, selection.selectionSet);
}
});
return selectionSet;
}
function getAllFragmentSpreadsFromSelectionSet(
selectionSet: SelectionSetNode,
): FragmentSpreadNode[] {
return selectionSet.selections
.map(getAllFragmentSpreadsFromSelection)
.reduce(
(allFragments, selectionFragments) => [
...allFragments,
...selectionFragments,
],
[],
);
}
function getAllFragmentSpreadsFromSelection(
selection: SelectionNode,
): FragmentSpreadNode[] {
if (
(selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet
) {
return getAllFragmentSpreadsFromSelectionSet(selection.selectionSet);
} else if (
selection.kind === 'FragmentSpread' &&
(selection as FragmentSpreadNode)
) {
return [selection];
}
return [];
}
function filterSelectionSet(
selectionSet: SelectionSetNode,
filter: (node: SelectionNode) => boolean,
) {
if (!(selectionSet && selectionSet.selections)) {
return false;
}
return selectionSet.selections.filter(filter).length > 0;
}

@@ -0,0 +0,0 @@ import { assign } from '../assign';

@@ -0,0 +0,0 @@ import { cloneDeep } from '../cloneDeep';

@@ -0,0 +0,0 @@ import { isEnv, isProduction, isDevelopment, isTest } from '../environment';

@@ -0,0 +0,0 @@ import { isEqual } from '../isEqual';

@@ -0,0 +0,0 @@ import { warnOnceInDevelopment } from '../warnOnce';

@@ -0,0 +0,0 @@ /**

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc