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.1.2 to 1.2.0-beta.0

lib/util/mergeDeep.d.ts

169

lib/bundle.umd.js

@@ -308,2 +308,7 @@ (function (global, factory) {

}
function hasClientExports(document) {
return (document &&
hasDirectives(['client'], document) &&
hasDirectives(['export'], document));
}

@@ -377,3 +382,3 @@ function getFragmentQueryDocument(document, fragmentName) {

function checkDocument(doc) {
if (doc.kind !== 'Document') {
if (!doc || doc.kind !== 'Document') {
throw new Error("Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql");

@@ -520,3 +525,5 @@ }

function nullIfDocIsEmpty(doc) {
return isEmpty(getOperationDefinitionOrDie(doc), createFragmentMap(getFragmentDefinitions(doc))) ? null : doc;
return isEmpty(getOperationDefinition(doc) || getFragmentDefinition(doc), createFragmentMap(getFragmentDefinitions(doc)))
? null
: doc;
}

@@ -546,23 +553,25 @@ function getDirectiveMatcher(directives) {

enter: function (node) {
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 (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,
});
}
});
}
if (node.selectionSet) {
getAllFragmentSpreadsFromSelectionSet(node.selectionSet).forEach(function (frag) {
fragmentSpreadsToRemove.push({
name: frag.name.value,
});
});
}
return null;
}
return null;
}

@@ -589,3 +598,4 @@ },

if (modifiedDoc &&
filterInPlace(fragmentSpreadsToRemove, function (fs) { return !fragmentSpreadsInUse[fs.name]; }).length) {
filterInPlace(fragmentSpreadsToRemove, function (fs) { return !fragmentSpreadsInUse[fs.name]; })
.length) {
modifiedDoc = removeFragmentSpreadFromDocument(fragmentSpreadsToRemove, modifiedDoc);

@@ -667,5 +677,3 @@ }

if (node.selections) {
var selectionsWithDirectives = node.selections.filter(function (selection) {
return hasDirectivesInSelection(directives, selection);
});
var selectionsWithDirectives = node.selections.filter(function (selection) { return hasDirectivesInSelection(directives, selection); });
if (hasDirectivesInSelectionSet(directives, node, false)) {

@@ -700,3 +708,5 @@ parentPath = currentPath;

enter: function (node) {
return __assign({}, node, { variableDefinitions: node.variableDefinitions.filter(function (varDef) { return !config.some(function (arg) { return arg.name === varDef.variable.name.value; }); }) });
return __assign({}, node, { variableDefinitions: node.variableDefinitions.filter(function (varDef) {
return !config.some(function (arg) { return arg.name === varDef.variable.name.value; });
}) });
},

@@ -743,4 +753,3 @@ },

selectionSet.selections.forEach(function (selection) {
if ((selection.kind === 'Field' ||
selection.kind === 'InlineFragment') &&
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet) {

@@ -755,2 +764,44 @@ getAllFragmentSpreadsFromSelectionSet(selection.selectionSet).forEach(function (frag) { return allFragments.push(frag); });

}
function buildQueryFromSelectionSet(document) {
var definition = getMainDefinition(document);
var definitionOperation = definition.operation;
if (definitionOperation === 'query') {
return document;
}
var modifiedDoc = visitor.visit(document, {
OperationDefinition: {
enter: function (node) {
return __assign({}, node, { operation: 'query' });
},
},
});
return modifiedDoc;
}
function removeClientSetsFromDocument(document) {
checkDocument(document);
var modifiedDoc = removeDirectivesFromDocument([
{
test: function (directive) { return directive.name.value === 'client'; },
remove: true,
},
], document);
if (modifiedDoc) {
modifiedDoc = visitor.visit(modifiedDoc, {
FragmentDefinition: {
enter: function (node) {
if (node.selectionSet) {
var isTypenameOnly = node.selectionSet.selections.every(function (selection) {
return (selection.kind === 'Field' &&
selection.name.value === '__typename');
});
if (isTypenameOnly) {
return null;
}
}
},
},
});
}
return modifiedDoc;
}

@@ -874,2 +925,61 @@ var toString = Object.prototype.toString;

var hasOwnProperty = Object.prototype.hasOwnProperty;
function mergeDeep() {
var sources = [];
for (var _i = 0; _i < arguments.length; _i++) {
sources[_i] = arguments[_i];
}
return mergeDeepArray(sources);
}
function mergeDeepArray(sources) {
var target = sources[0] || {};
var count = sources.length;
if (count > 1) {
var pastCopies = [];
target = shallowCopyForMerge(target, pastCopies);
for (var i = 1; i < count; ++i) {
target = mergeHelper(target, sources[i], pastCopies);
}
}
return target;
}
function isObject(obj) {
return obj !== null && typeof obj === 'object';
}
function mergeHelper(target, source, pastCopies) {
if (isObject(source) && isObject(target)) {
if (Object.isExtensible && !Object.isExtensible(target)) {
target = shallowCopyForMerge(target, pastCopies);
}
Object.keys(source).forEach(function (sourceKey) {
var sourceValue = source[sourceKey];
if (hasOwnProperty.call(target, sourceKey)) {
var targetValue = target[sourceKey];
if (sourceValue !== targetValue) {
target[sourceKey] = mergeHelper(shallowCopyForMerge(targetValue, pastCopies), sourceValue, pastCopies);
}
}
else {
target[sourceKey] = sourceValue;
}
});
return target;
}
return source;
}
function shallowCopyForMerge(value, pastCopies) {
if (value !== null &&
typeof value === 'object' &&
pastCopies.indexOf(value) < 0) {
if (Array.isArray(value)) {
value = value.slice(0);
}
else {
value = __assign({ __proto__: Object.getPrototypeOf(value) }, value);
}
pastCopies.push(value);
}
return value;
}
var haveWarned = Object.create({});

@@ -903,2 +1013,3 @@ function warnOnceInDevelopment(msg, type) {

exports.hasDirectives = hasDirectives;
exports.hasClientExports = hasClientExports;
exports.getFragmentQueryDocument = getFragmentQueryDocument;

@@ -923,2 +1034,4 @@ exports.getMutationDefinition = getMutationDefinition;

exports.removeFragmentSpreadFromDocument = removeFragmentSpreadFromDocument;
exports.buildQueryFromSelectionSet = buildQueryFromSelectionSet;
exports.removeClientSetsFromDocument = removeClientSetsFromDocument;
exports.isScalarValue = isScalarValue;

@@ -948,2 +1061,4 @@ exports.isNumberValue = isNumberValue;

exports.maybeDeepFreeze = maybeDeepFreeze;
exports.mergeDeep = mergeDeep;
exports.mergeDeepArray = mergeDeepArray;
exports.warnOnceInDevelopment = warnOnceInDevelopment;

@@ -950,0 +1065,0 @@ exports.stripSymbols = stripSymbols;

@@ -13,2 +13,3 @@ import { FieldNode, SelectionNode, DocumentNode } from 'graphql';

export declare function hasDirectives(names: string[], doc: DocumentNode): boolean;
export declare function hasClientExports(document: DocumentNode): boolean;
//# sourceMappingURL=directives.d.ts.map

@@ -69,2 +69,7 @@ import { visit } from 'graphql/language/visitor';

}
export function hasClientExports(document) {
return (document &&
hasDirectives(['client'], document) &&
hasDirectives(['export'], document));
}
//# sourceMappingURL=directives.js.map

2

lib/getFromAST.js

@@ -15,3 +15,3 @@ import { assign } from './util/assign';

export function checkDocument(doc) {
if (doc.kind !== 'Document') {
if (!doc || doc.kind !== 'Document') {
throw new Error("Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql");

@@ -18,0 +18,0 @@ }

@@ -12,4 +12,6 @@ export * from './directives';

export * from './util/maybeDeepFreeze';
export * from './util/mergeDeep';
export * from './util/warnOnce';
export * from './util/stripSymbols';
export * from './util/mergeDeep';
//# sourceMappingURL=index.d.ts.map

@@ -12,4 +12,6 @@ export * from './directives';

export * from './util/maybeDeepFreeze';
export * from './util/mergeDeep';
export * from './util/warnOnce';
export * from './util/stripSymbols';
export * from './util/mergeDeep';
//# sourceMappingURL=index.js.map

@@ -24,2 +24,4 @@ import { DocumentNode, DirectiveNode, FragmentDefinitionNode, ArgumentNode, FragmentSpreadNode, VariableDefinitionNode } from 'graphql';

export declare function removeFragmentSpreadFromDocument(config: RemoveFragmentSpreadConfig[], doc: DocumentNode): DocumentNode;
export declare function buildQueryFromSelectionSet(document: DocumentNode): DocumentNode;
export declare function removeClientSetsFromDocument(document: DocumentNode): DocumentNode | null;
//# sourceMappingURL=transform.d.ts.map
import * as tslib_1 from "tslib";
import { visit } from 'graphql/language/visitor';
import { checkDocument, getOperationDefinitionOrDie, getFragmentDefinitions, createFragmentMap, } from './getFromAST';
import { checkDocument, getOperationDefinition, getFragmentDefinition, getFragmentDefinitions, createFragmentMap, getMainDefinition, } from './getFromAST';
import { filterInPlace } from './util/filterInPlace';

@@ -19,3 +19,5 @@ var TYPENAME_FIELD = {

function nullIfDocIsEmpty(doc) {
return isEmpty(getOperationDefinitionOrDie(doc), createFragmentMap(getFragmentDefinitions(doc))) ? null : doc;
return isEmpty(getOperationDefinition(doc) || getFragmentDefinition(doc), createFragmentMap(getFragmentDefinitions(doc)))
? null
: doc;
}

@@ -45,23 +47,25 @@ function getDirectiveMatcher(directives) {

enter: function (node) {
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 (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,
});
}
});
}
if (node.selectionSet) {
getAllFragmentSpreadsFromSelectionSet(node.selectionSet).forEach(function (frag) {
fragmentSpreadsToRemove.push({
name: frag.name.value,
});
});
}
return null;
}
return null;
}

@@ -88,3 +92,4 @@ },

if (modifiedDoc &&
filterInPlace(fragmentSpreadsToRemove, function (fs) { return !fragmentSpreadsInUse[fs.name]; }).length) {
filterInPlace(fragmentSpreadsToRemove, function (fs) { return !fragmentSpreadsInUse[fs.name]; })
.length) {
modifiedDoc = removeFragmentSpreadFromDocument(fragmentSpreadsToRemove, modifiedDoc);

@@ -166,5 +171,3 @@ }

if (node.selections) {
var selectionsWithDirectives = node.selections.filter(function (selection) {
return hasDirectivesInSelection(directives, selection);
});
var selectionsWithDirectives = node.selections.filter(function (selection) { return hasDirectivesInSelection(directives, selection); });
if (hasDirectivesInSelectionSet(directives, node, false)) {

@@ -199,3 +202,5 @@ parentPath = currentPath;

enter: function (node) {
return tslib_1.__assign({}, node, { variableDefinitions: node.variableDefinitions.filter(function (varDef) { return !config.some(function (arg) { return arg.name === varDef.variable.name.value; }); }) });
return tslib_1.__assign({}, node, { variableDefinitions: node.variableDefinitions.filter(function (varDef) {
return !config.some(function (arg) { return arg.name === varDef.variable.name.value; });
}) });
},

@@ -242,4 +247,3 @@ },

selectionSet.selections.forEach(function (selection) {
if ((selection.kind === 'Field' ||
selection.kind === 'InlineFragment') &&
if ((selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet) {

@@ -254,2 +258,44 @@ getAllFragmentSpreadsFromSelectionSet(selection.selectionSet).forEach(function (frag) { return allFragments.push(frag); });

}
export function buildQueryFromSelectionSet(document) {
var definition = getMainDefinition(document);
var definitionOperation = definition.operation;
if (definitionOperation === 'query') {
return document;
}
var modifiedDoc = visit(document, {
OperationDefinition: {
enter: function (node) {
return tslib_1.__assign({}, node, { operation: 'query' });
},
},
});
return modifiedDoc;
}
export function removeClientSetsFromDocument(document) {
checkDocument(document);
var modifiedDoc = removeDirectivesFromDocument([
{
test: function (directive) { return directive.name.value === 'client'; },
remove: true,
},
], document);
if (modifiedDoc) {
modifiedDoc = visit(modifiedDoc, {
FragmentDefinition: {
enter: function (node) {
if (node.selectionSet) {
var isTypenameOnly = node.selectionSet.selections.every(function (selection) {
return (selection.kind === 'Field' &&
selection.name.value === '__typename');
});
if (isTypenameOnly) {
return null;
}
}
},
},
});
}
return modifiedDoc;
}
//# sourceMappingURL=transform.js.map
{
"name": "apollo-utilities",
"version": "1.1.2",
"version": "1.2.0-beta.0",
"description": "Utilities for working with GraphQL ASTs",

@@ -41,2 +41,5 @@ "author": "James Baxley <james@meteor.com>",

},
"peerDependencies": {
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0"
},
"dependencies": {

@@ -59,3 +62,3 @@ "fast-json-stable-stringify": "^2.0.0",

},
"gitHead": "42a474ce983964a3eb3fc4b1e5d90ebacffeb739"
"gitHead": "b49a7e851b5637eb9bf3784781fd2c8881a2d2d8"
}

@@ -15,2 +15,3 @@ import { print } from 'graphql/language/printer';

removeFragmentSpreadFromDocument,
removeClientSetsFromDocument,
} from '../transform';

@@ -1207,1 +1208,37 @@ import { getQueryDefinition } from '../getFromAST';

});
describe('removeClientSetsFromDocument', () => {
it('should remove @client fields from document', () => {
const query = gql`
query Author {
name
isLoggedIn @client
}
`;
const expected = gql`
query Author {
name
}
`;
const doc = removeClientSetsFromDocument(query);
expect(print(doc)).toBe(print(expected));
});
it('should remove @client fields from fragments', () => {
const query = gql`
fragment authorInfo on Author {
name
isLoggedIn @client
}
`;
const expected = gql`
fragment authorInfo on Author {
name
}
`;
const doc = removeClientSetsFromDocument(query);
expect(print(doc)).toBe(print(expected));
});
});

@@ -1,3 +0,3 @@

// Provides the methods that allow QueryManager to handle
// the `skip` and `include` directives within GraphQL.
// Provides the methods that allow QueryManager to handle the `skip` and
// `include` directives within GraphQL.
import {

@@ -116,1 +116,9 @@ FieldNode,

}
export function hasClientExports(document: DocumentNode) {
return (
document &&
hasDirectives(['client'], document) &&
hasDirectives(['export'], document)
);
}

@@ -31,3 +31,3 @@ import {

export function checkDocument(doc: DocumentNode) {
if (doc.kind !== 'Document') {
if (!doc || doc.kind !== 'Document') {
throw new Error(`Expecting a parsed GraphQL document. Perhaps you need to wrap the query \

@@ -34,0 +34,0 @@ string in a "gql" tag? http://docs.apollostack.com/apollo-client/core.html#gql`);

@@ -12,3 +12,5 @@ export * from './directives';

export * from './util/maybeDeepFreeze';
export * from './util/mergeDeep';
export * from './util/warnOnce';
export * from './util/stripSymbols';
export * from './util/mergeDeep';

@@ -18,6 +18,8 @@ import {

checkDocument,
getOperationDefinitionOrDie,
getOperationDefinition,
getFragmentDefinition,
getFragmentDefinitions,
createFragmentMap,
FragmentMap,
getMainDefinition,
} from './getFromAST';

@@ -70,7 +72,7 @@ import { filterInPlace } from './util/filterInPlace';

return isEmpty(
getOperationDefinitionOrDie(doc),
createFragmentMap(
getFragmentDefinitions(doc)
),
) ? null : doc;
getOperationDefinition(doc) || getFragmentDefinition(doc),
createFragmentMap(getFragmentDefinitions(doc)),
)
? null
: doc;
}

@@ -100,76 +102,82 @@

let modifiedDoc = nullIfDocIsEmpty(visit(doc, {
Variable: {
enter(node, _key, parent) {
// Store each variable that's referenced as part of an argument
// (excluding operation definition variables), so we know which
// variables are being used. If we later want to remove a variable
// we'll fist check to see if it's being used, before continuing with
// the removal.
if ((parent as VariableDefinitionNode).kind !== 'VariableDefinition') {
variablesInUse[node.name.value] = true;
}
let modifiedDoc = nullIfDocIsEmpty(
visit(doc, {
Variable: {
enter(node, _key, parent) {
// Store each variable that's referenced as part of an argument
// (excluding operation definition variables), so we know which
// variables are being used. If we later want to remove a variable
// we'll fist check to see if it's being used, before continuing with
// the removal.
if (
(parent as VariableDefinitionNode).kind !== 'VariableDefinition'
) {
variablesInUse[node.name.value] = true;
}
},
},
},
Field: {
enter(node) {
// If `remove` is set to true for a directive, and a directive match
// is found for a field, remove the field as well.
const shouldRemoveField = directives.some(
directive => directive.remove,
);
Field: {
enter(node) {
if (directives && node.directives) {
// If `remove` is set to true for a directive, and a directive match
// is found for a field, remove the field as well.
const shouldRemoveField = directives.some(
directive => directive.remove,
);
if (
shouldRemoveField &&
node.directives &&
node.directives.some(getDirectiveMatcher(directives))
) {
if (node.arguments) {
// Store field argument variables so they can be removed
// from the operation definition.
node.arguments.forEach(arg => {
if (arg.value.kind === 'Variable') {
variablesToRemove.push({
name: (arg.value as VariableNode).name.value,
if (
shouldRemoveField &&
node.directives &&
node.directives.some(getDirectiveMatcher(directives))
) {
if (node.arguments) {
// Store field argument variables so they can be removed
// from the operation definition.
node.arguments.forEach(arg => {
if (arg.value.kind === 'Variable') {
variablesToRemove.push({
name: (arg.value as VariableNode).name.value,
});
}
});
}
});
}
if (node.selectionSet) {
// Store fragment spread names so they can be removed from the
// docuemnt.
getAllFragmentSpreadsFromSelectionSet(node.selectionSet).forEach(
frag => {
fragmentSpreadsToRemove.push({
name: frag.name.value,
});
},
);
if (node.selectionSet) {
// Store fragment spread names so they can be removed from the
// docuemnt.
getAllFragmentSpreadsFromSelectionSet(node.selectionSet).forEach(
frag => {
fragmentSpreadsToRemove.push({
name: frag.name.value,
});
},
);
}
// Remove the field.
return null;
}
}
// Remove the field.
return null;
}
},
},
},
FragmentSpread: {
enter(node) {
// Keep track of referenced fragment spreads. This is used to
// determine if top level fragment definitions should be removed.
fragmentSpreadsInUse[node.name.value] = true;
FragmentSpread: {
enter(node) {
// Keep track of referenced fragment spreads. This is used to
// determine if top level fragment definitions should be removed.
fragmentSpreadsInUse[node.name.value] = true;
},
},
},
Directive: {
enter(node) {
// If a matching directive is found, remove it.
if (getDirectiveMatcher(directives)(node)) {
return null;
}
Directive: {
enter(node) {
// If a matching directive is found, remove it.
if (getDirectiveMatcher(directives)(node)) {
return null;
}
},
},
},
}));
}),
);

@@ -179,4 +187,6 @@ // If we've removed fields with arguments, make sure the associated

// aren't being used elsewhere.
if (modifiedDoc &&
filterInPlace(variablesToRemove, v => !variablesInUse[v.name]).length) {
if (
modifiedDoc &&
filterInPlace(variablesToRemove, v => !variablesInUse[v.name]).length
) {
modifiedDoc = removeArgumentsFromDocument(variablesToRemove, modifiedDoc);

@@ -188,7 +198,7 @@ }

// document, as long as they aren't being used elsewhere.
if (modifiedDoc &&
filterInPlace(
fragmentSpreadsToRemove,
fs => !fragmentSpreadsInUse[fs.name],
).length) {
if (
modifiedDoc &&
filterInPlace(fragmentSpreadsToRemove, fs => !fragmentSpreadsInUse[fs.name])
.length
) {
modifiedDoc = removeFragmentSpreadFromDocument(

@@ -316,32 +326,34 @@ fragmentSpreadsToRemove,

return nullIfDocIsEmpty(visit(doc, {
SelectionSet: {
enter(node, _key, _parent, path) {
const currentPath = path.join('-');
return nullIfDocIsEmpty(
visit(doc, {
SelectionSet: {
enter(node, _key, _parent, path) {
const currentPath = path.join('-');
if (
!parentPath ||
currentPath === parentPath ||
!currentPath.startsWith(parentPath)
) {
if (node.selections) {
const selectionsWithDirectives = node.selections.filter(selection =>
hasDirectivesInSelection(directives, selection),
);
if (
!parentPath ||
currentPath === parentPath ||
!currentPath.startsWith(parentPath)
) {
if (node.selections) {
const selectionsWithDirectives = node.selections.filter(
selection => hasDirectivesInSelection(directives, selection),
);
if (hasDirectivesInSelectionSet(directives, node, false)) {
parentPath = currentPath;
if (hasDirectivesInSelectionSet(directives, node, false)) {
parentPath = currentPath;
}
return {
...node,
selections: selectionsWithDirectives,
};
} else {
return null;
}
return {
...node,
selections: selectionsWithDirectives,
};
} else {
return null;
}
}
},
},
},
}));
}),
);
}

@@ -368,44 +380,47 @@

return nullIfDocIsEmpty(visit(doc, {
OperationDefinition: {
enter(node) {
return {
...node,
// Remove matching top level variables definitions.
variableDefinitions: node.variableDefinitions.filter(
varDef => !config.some(arg => arg.name === varDef.variable.name.value),
),
};
return nullIfDocIsEmpty(
visit(doc, {
OperationDefinition: {
enter(node) {
return {
...node,
// Remove matching top level variables definitions.
variableDefinitions: node.variableDefinitions.filter(
varDef =>
!config.some(arg => arg.name === varDef.variable.name.value),
),
};
},
},
},
Field: {
enter(node) {
// If `remove` is set to true for an argument, and an argument match
// is found for a field, remove the field as well.
const shouldRemoveField = config.some(argConfig => argConfig.remove);
Field: {
enter(node) {
// If `remove` is set to true for an argument, and an argument match
// is found for a field, remove the field as well.
const shouldRemoveField = config.some(argConfig => argConfig.remove);
if (shouldRemoveField) {
let argMatchCount = 0;
node.arguments.forEach(arg => {
if (argMatcher(arg)) {
argMatchCount += 1;
if (shouldRemoveField) {
let argMatchCount = 0;
node.arguments.forEach(arg => {
if (argMatcher(arg)) {
argMatchCount += 1;
}
});
if (argMatchCount === 1) {
return null;
}
});
if (argMatchCount === 1) {
return null;
}
}
},
},
},
Argument: {
enter(node) {
// Remove all matching arguments.
if (argMatcher(node)) {
return null;
}
Argument: {
enter(node) {
// Remove all matching arguments.
if (argMatcher(node)) {
return null;
}
},
},
},
}));
}),
);
}

@@ -439,8 +454,9 @@

selectionSet.selections.forEach(selection => {
if ((selection.kind === 'Field' ||
selection.kind === 'InlineFragment') &&
selection.selectionSet) {
getAllFragmentSpreadsFromSelectionSet(
selection.selectionSet
).forEach(frag => allFragments.push(frag));
if (
(selection.kind === 'Field' || selection.kind === 'InlineFragment') &&
selection.selectionSet
) {
getAllFragmentSpreadsFromSelectionSet(selection.selectionSet).forEach(
frag => allFragments.push(frag),
);
} else if (selection.kind === 'FragmentSpread') {

@@ -453,1 +469,74 @@ allFragments.push(selection);

}
// If the incoming document is a query, return it as is. Otherwise, build a
// new document containing a query operation based on the selection set
// of the previous main operation.
export function buildQueryFromSelectionSet(
document: DocumentNode,
): DocumentNode {
const definition = getMainDefinition(document);
const definitionOperation = (<OperationDefinitionNode>definition).operation;
if (definitionOperation === 'query') {
// Already a query, so return the existing document.
return document;
}
// Build a new query using the selection set of the main operation.
const modifiedDoc = visit(document, {
OperationDefinition: {
enter(node) {
return {
...node,
operation: 'query',
};
},
},
});
return modifiedDoc;
}
// Remove fields / selection sets that include an @client directive.
export function removeClientSetsFromDocument(
document: DocumentNode,
): DocumentNode | null {
checkDocument(document);
let modifiedDoc = removeDirectivesFromDocument(
[
{
test: (directive: DirectiveNode) => directive.name.value === 'client',
remove: true,
},
],
document,
);
// After a fragment definition has had its @client related document
// sets removed, if the only field it has left is a __typename field,
// remove the entire fragment operation to prevent it from being fired
// on the server.
if (modifiedDoc) {
modifiedDoc = visit(modifiedDoc, {
FragmentDefinition: {
enter(node) {
if (node.selectionSet) {
const isTypenameOnly = node.selectionSet.selections.every(
selection => {
return (
selection.kind === 'Field' &&
(selection as FieldNode).name.value === '__typename'
);
},
);
if (isTypenameOnly) {
return null;
}
}
},
},
});
}
return modifiedDoc;
}

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

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

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