Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-query-rewriter-rc

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-query-rewriter-rc - npm Package Compare versions

Comparing version 0.0.17 to 0.0.18

354

dist/index.es5.js

@@ -1,2 +0,2 @@

import { Kind, parse, print, isValueNode, parseType } from 'graphql';
import { parse, print, parseType } from 'graphql';

@@ -43,12 +43,2 @@ /*! *****************************************************************************

function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
}
/** @hidden */

@@ -254,19 +244,10 @@ var identifyFunc = function (val) { return val; };

/**
* Return the path that will be returned in the response from the chain of parents.
* By default this will only build up paths for field nodes, but the anyKind flag allows
* to build paths for any named node.
*
* It also supports aliases.
* return the path that will be returned in the response from from the chain of parents
*/
/** @hidden */
var extractPath = function (parents, anyKind) {
var extractPath = function (parents) {
var path = [];
parents.forEach(function (parent) {
if (parent.kind === 'Field' || anyKind) {
if (parent.alias) {
path.push(parent.alias.value);
}
else if (parent.name) {
path.push(parent.name.value);
}
if (parent.kind === 'Field') {
path.push(parent.name.value);
}

@@ -276,24 +257,4 @@ });

};
/**
* return an ArgumentNode with a VariableNode as its value node with matching name.
*/
/** @hidden */
var astArgVarNode = function (argName) {
return {
kind: Kind.ARGUMENT,
name: {
kind: Kind.NAME,
value: argName
},
value: {
kind: Kind.VARIABLE,
name: {
kind: Kind.NAME,
value: argName
}
}
};
};
/** @hidden */
var rewriteResultsAtPath = function (results, path, callback, includesNonFieldPaths) {
var rewriteResultsAtPath = function (results, path, callback) {
if (path.length === 0)

@@ -304,7 +265,2 @@ return results;

var curResults = results[curPathElm];
// if results[curPathElm] is an empty array, call the callback response rewriter
// because there's nothing left to do.
if (Array.isArray(curResults) && curResults.length === 0) {
callback(results, curPathElm);
}
if (path.length === 1) {

@@ -317,10 +273,2 @@ if (Array.isArray(curResults)) {

var remainingPath = path.slice(1);
// If curResults is undefined, and includesNonFieldPaths is true,
// then curResults is not a field path, so call the callback to allow rewrites
// for non-field paths.
if (remainingPath.length && includesNonFieldPaths && curResults === undefined) {
callback(results, curPathElm);
// Then just continue with the next path
return rewriteResultsAtPath(results, remainingPath, callback, includesNonFieldPaths);
}
// if the path stops here, just return results without any rewriting

@@ -331,7 +279,7 @@ if (curResults === undefined || curResults === null)

newResults[curPathElm] = curResults.map(function (result) {
return rewriteResultsAtPath(result, remainingPath, callback, includesNonFieldPaths);
return rewriteResultsAtPath(result, remainingPath, callback);
});
}
else {
newResults[curPathElm] = rewriteResultsAtPath(curResults, remainingPath, callback, includesNonFieldPaths);
newResults[curPathElm] = rewriteResultsAtPath(curResults, remainingPath, callback);
}

@@ -371,7 +319,5 @@ return newResults;

rewrittenVariables = rewriter.rewriteVariables(rewrittenNodeAndVars, rewrittenVariables);
rewrittenNodeAndVars = rewriter.rewriteQuery(rewrittenNodeAndVars, rewrittenVariables);
var fieldPath = extractPath(parents.concat([rewrittenNodeAndVars.node]));
var anyPath = extractPath(parents.concat([rewrittenNodeAndVars.node]), true);
var fieldPaths = [fieldPath];
var allPaths = [anyPath];
rewrittenNodeAndVars = rewriter.rewriteQuery(rewrittenNodeAndVars);
var simplePath = extractPath(parents.concat([rewrittenNodeAndVars.node]));
var paths = [simplePath];
var fragmentDef = parents.find(function (_a) {

@@ -382,10 +328,7 @@ var kind = _a.kind;

if (fragmentDef) {
fieldPaths = fragmentTracer.prependFragmentPaths(fragmentDef.name.value, fieldPath);
allPaths = fragmentTracer.prependFragmentPaths(fragmentDef.name.value, anyPath);
paths = fragmentTracer.prependFragmentPaths(fragmentDef.name.value, simplePath);
}
_this.matches.push({
rewriter: rewriter,
allPaths: allPaths,
paths: fieldPaths,
nodeMatchAndParents: parents.concat([rewrittenNodeAndVars.node])
paths: paths
});

@@ -409,11 +352,6 @@ }

var rewrittenResponse = response;
this.matches
.reverse()
.forEach(function (_a) {
var rewriter = _a.rewriter, fieldPaths = _a.paths, allPaths = _a.allPaths, nodeMatchAndParents = _a.nodeMatchAndParents;
var paths = rewriter.includeNonFieldPathsInMatch ? allPaths : fieldPaths;
this.matches.reverse().forEach(function (_a) {
var rewriter = _a.rewriter, paths = _a.paths;
paths.forEach(function (path) {
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key, index) {
return rewriter.rewriteResponse(parentResponse, key, index, nodeMatchAndParents);
}, rewriter.includeNonFieldPathsInMatch);
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key, index) { return rewriter.rewriteResponse(parentResponse, key, index); });
});

@@ -432,11 +370,6 @@ });

function Rewriter(_a) {
var fieldName = _a.fieldName, rootTypes = _a.rootTypes, matchConditions = _a.matchConditions, _b = _a.includeNonFieldPathsInMatch, includeNonFieldPathsInMatch = _b === void 0 ? false : _b;
this.includeNonFieldPathsInMatch = false;
var fieldName = _a.fieldName, rootTypes = _a.rootTypes, matchConditions = _a.matchConditions;
this.rootTypes = ['query', 'mutation', 'fragment'];
this.fieldName = fieldName;
this.matchConditions = matchConditions;
this.includeNonFieldPathsInMatch = includeNonFieldPathsInMatch;
if (!this.fieldName && !this.matchConditions) {
throw new Error('Neither a fieldName or matchConditions were provided. Please choose to pass either one in order to be able to detect which fields to rewrite.');
}
if (rootTypes)

@@ -447,9 +380,4 @@ this.rootTypes = rootTypes;

var node = nodeAndVarDefs.node;
// If no fieldName is provided, check for defined matchConditions.
// This avoids having to define one rewriter for many fields individually.
// Alternatively, regex matching for fieldName could be implemented.
if (node.kind !== 'Field' ||
(this.fieldName ? node.name.value !== this.fieldName : !this.matchConditions)) {
if (node.kind !== 'Field' || node.name.value !== this.fieldName)
return false;
}
var root = parents[0];

@@ -469,3 +397,3 @@ if (root.kind === 'OperationDefinition' &&

};
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) {
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs) {
return nodeAndVarDefs;

@@ -480,3 +408,3 @@ };

*/
Rewriter.prototype.rewriteResponse = function (response, key, index, nodeMatchAndParents) {
Rewriter.prototype.rewriteResponse = function (response, key, index) {
return response;

@@ -496,7 +424,3 @@ };

if (Array.isArray(element)) {
// if element is an empty array do not try to get
// one of its array elements
if (element.length !== 0) {
element = element[index] || null;
}
element = element[index] || null;
}

@@ -630,3 +554,2 @@ return element;

_this.coerceVariable = options.coerceVariable || identifyFunc;
_this.coerceArgumentValue = options.coerceArgumentValue || identifyFunc;
return _this;

@@ -643,78 +566,36 @@ }

return false;
// does this field contain arguments?
if (!node.arguments)
if (node.name.value !== this.fieldName || !node.arguments)
return false;
// is there an argument with the correct name and type in a variable?
var matchingArgument = node.arguments.find(function (arg) { return arg.name.value === _this.argName; });
if (!matchingArgument)
if (!matchingArgument || matchingArgument.value.kind !== 'Variable')
return false;
// argument value is stored in a variable
if (matchingArgument.value.kind === 'Variable') {
var varRef = matchingArgument.value.name.value;
// does the referenced variable have the correct type?
for (var _i = 0, variableDefinitions_1 = variableDefinitions; _i < variableDefinitions_1.length; _i++) {
var varDefinition = variableDefinitions_1[_i];
if (varDefinition.variable.name.value === varRef) {
return nodesMatch(this.oldTypeNode, varDefinition.type);
}
var varRef = matchingArgument.value.name.value;
// does the referenced variable have the correct type?
for (var _i = 0, variableDefinitions_1 = variableDefinitions; _i < variableDefinitions_1.length; _i++) {
var varDefinition = variableDefinitions_1[_i];
if (varDefinition.variable.name.value === varRef) {
return nodesMatch(this.oldTypeNode, varDefinition.type);
}
}
// argument value comes in query doc.
else {
var argValueNode = matchingArgument.value;
return isValueNode(argValueNode);
// Would be ideal to do a nodesMatch in here, however argument value nodes
// have different format for their values than when passed as variables.
// For instance, are parsed with Kinds as "graphql.Kind" (e.g., INT="IntValue") and not "graphql.TokenKinds" (e.g., INT="Int")
// So they might not match correctly. Also they dont contain additional parsed syntax
// as the non-optional symbol "!". So just return true if the argument.value is a ValueNode.
//
// return nodesMatch(this.oldTypeNode, parseType(argRef.kind));
}
return false;
};
FieldArgTypeRewriter.prototype.rewriteQuery = function (_a, variables) {
FieldArgTypeRewriter.prototype.rewriteQuery = function (_a) {
var _this = this;
var astNode = _a.node, variableDefinitions = _a.variableDefinitions;
var node = astNode;
var node = _a.node, variableDefinitions = _a.variableDefinitions;
var varRefName = this.extractMatchingVarRefName(node);
// If argument value is stored in a variable
if (varRefName) {
var newVarDefs = variableDefinitions.map(function (varDef) {
if (varDef.variable.name.value !== varRefName)
return varDef;
return __assign({}, varDef, { type: _this.newTypeNode });
});
return { node: node, variableDefinitions: newVarDefs };
}
// If argument value is not stored in a variable but in the query node.
var matchingArgument = (node.arguments || []).find(function (arg) { return arg.name.value === _this.argName; });
if (node.arguments && matchingArgument) {
var args = node.arguments.slice();
var newValue = this.coerceArgumentValue(matchingArgument.value, { variables: variables, args: args });
/**
* TODO: If somewhow we can get the schema here, we could make the coerceArgumentValue
* even easier, as we would be able to construct the ast node for the argument value.
* as of now, the user has to take care of correctly constructing the argument value ast node herself.
*
* const schema = makeExecutableSchema({typeDefs})
* const myCustomType = schema.getType("MY_CUSTOM_TYPE_NAME")
* const newArgValue = astFromValue(newValue, myCustomType)
* Object.assign(matchingArgument, { value: newArgValue })
*/
if (newValue)
Object.assign(matchingArgument, { value: newValue });
}
return { node: node, variableDefinitions: variableDefinitions };
var newVarDefs = variableDefinitions.map(function (varDef) {
if (varDef.variable.name.value !== varRefName)
return varDef;
return __assign({}, varDef, { type: _this.newTypeNode });
});
return { node: node, variableDefinitions: newVarDefs };
};
FieldArgTypeRewriter.prototype.rewriteVariables = function (_a, variables) {
var astNode = _a.node;
var node = _a.node;
var _b;
var node = astNode;
if (!variables)
return variables;
var varRefName = this.extractMatchingVarRefName(node);
var args = (node.arguments ? node.arguments : []).slice();
return __assign({}, variables, (varRefName
? (_b = {}, _b[varRefName] = this.coerceVariable(variables[varRefName], { variables: variables, args: args }), _b) : {}));
return __assign({}, variables, (_b = {}, _b[varRefName] = this.coerceVariable(variables[varRefName]), _b));
};

@@ -724,4 +605,3 @@ FieldArgTypeRewriter.prototype.extractMatchingVarRefName = function (node) {

var matchingArgument = (node.arguments || []).find(function (arg) { return arg.name.value === _this.argName; });
var variableNode = matchingArgument.value;
return variableNode.kind === Kind.VARIABLE && variableNode.name.value;
return matchingArgument.value.name.value;
};

@@ -892,154 +772,2 @@ return FieldArgTypeRewriter;

/**
* More generic version of ScalarFieldToObjectField rewriter
*/
var FieldRewriter = /** @class */ (function (_super) {
__extends(FieldRewriter, _super);
function FieldRewriter(options) {
var _this = _super.call(this, options) || this;
_this.newFieldName = options.newFieldName;
_this.arguments = options.arguments;
_this.objectFieldName = options.objectFieldName;
return _this;
}
FieldRewriter.prototype.matches = function (nodeAndVars, parents) {
if (!_super.prototype.matches.call(this, nodeAndVars, parents))
return false;
var node = nodeAndVars.node;
// if there's the intention of converting the field to a subselection
// make sure there's no subselections on this field
if (node.selectionSet && !!this.objectFieldName)
return false;
return true;
};
FieldRewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) {
var _this = this;
var node = nodeAndVarDefs.node;
var variableDefinitions = nodeAndVarDefs.variableDefinitions;
// if there's the intention of converting the field to a subselection
// and there's a subselection already, just return
if (node.selectionSet && !!this.objectFieldName)
return nodeAndVarDefs;
// if fieldName is meant to be renamed.
if (this.newFieldName) {
var newName = this.newFieldName;
if (this.newFieldName.includes(':')) {
var _a = this.newFieldName.split(':'), alias = _a[0], name_1 = _a[1];
newName = name_1.trim();
Object.assign(node, { alias: { value: alias.trim(), kind: Kind.NAME } });
}
Object.assign(node.name, { value: newName });
}
// if there's the intention of converting the field to a subselection
// of objectFieldNames assign SelectionSetNode to the field accordingly.
if (this.objectFieldName) {
var selectionSet = {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: this.objectFieldName }
}
]
};
Object.assign(node, { selectionSet: selectionSet });
}
// If, 1) the field is a SelectionSet,
// 2) this.arguments is not empty nor undefined, and
// 3) query comes with variables, then assign ArgumentNodes to the field accordingly.
if (node.selectionSet && !!this.arguments && variables) {
// field may already come with some arguments
var newArguments_1 = (node.arguments || []).slice();
this.arguments.forEach(function (argName) {
if (_this.isArgumentInVariables(argName, variables) &&
!_this.isArgumentInArguments(argName, newArguments_1)) {
newArguments_1.push(astArgVarNode(argName));
}
});
if (!!newArguments_1)
Object.assign(node, { arguments: newArguments_1 });
}
return {
variableDefinitions: variableDefinitions,
node: node
};
};
FieldRewriter.prototype.rewriteResponse = function (response, key, index) {
var _a;
// Extract the element we are working on
var element = _super.prototype.extractReponseElement.call(this, response, key, index);
if (element === null)
return response;
var originalKey = key;
// if the key is found to be the renamed field
// then change the name of such field in the response
// and pass the new key (field name) down.
if (this.newFieldName) {
var newFieldName = this.newFieldName;
// the newFieldName may be alised.
if (this.newFieldName.includes(':')) {
var alias = this.newFieldName.split(':')[0];
newFieldName = alias.trim();
}
if (key === newFieldName) {
if (this.fieldName) {
originalKey = this.fieldName;
Object.assign(response, (_a = {}, _a[originalKey] = response[key], _a));
delete response[key];
}
}
}
// If the element is an empty array, return the response, since
// there's nothing left to rewrite down that path.
if (Array.isArray(element) && element.length === 0)
return response;
// Undo the nesting in the response so it matches the original query
var newElement = element;
if (this.objectFieldName) {
newElement = element[this.objectFieldName];
}
return _super.prototype.rewriteResponseElement.call(this, response, newElement, originalKey, index);
};
FieldRewriter.prototype.isArgumentInArguments = function (argName, argumentNodes) {
return argumentNodes.map(function (argNode) { return argNode.name.value; }).includes(argName);
};
FieldRewriter.prototype.isArgumentInVariables = function (argName, variables) {
if (variables && Object.keys(variables).includes(argName))
return true;
return false;
};
return FieldRewriter;
}(Rewriter));
/**
* A Custom rewriter with its Rewriter functions received as arguments.
* This Rewriter allows users to write their own rewriter functions.
*/
var CustomRewriter = /** @class */ (function (_super) {
__extends(CustomRewriter, _super);
function CustomRewriter(options) {
var _this = this;
var matchesFn = options.matchesFn, rewriteQueryFn = options.rewriteQueryFn, rewriteVariablesFn = options.rewriteVariablesFn, rewriteResponseFn = options.rewriteResponseFn, _a = options.matchConditions, matchConditions = _a === void 0 ? [function () { return true; }] : _a, rewriterOpts = __rest(options, ["matchesFn", "rewriteQueryFn", "rewriteVariablesFn", "rewriteResponseFn", "matchConditions"]);
_this = _super.call(this, __assign({}, rewriterOpts, { matchConditions: matchConditions })) || this;
_this.matchesFn = matchesFn || _super.prototype.matches;
_this.rewriteQueryFn = rewriteQueryFn || _super.prototype.rewriteQuery;
_this.rewriteVariablesFn = rewriteVariablesFn || _super.prototype.rewriteVariables;
_this.rewriteResponseFn = rewriteResponseFn || _super.prototype.rewriteResponse;
return _this;
}
CustomRewriter.prototype.matches = function (nodeAndVarDefs, parents) {
return this.matchesFn(nodeAndVarDefs, parents);
};
CustomRewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) {
return this.rewriteQueryFn(nodeAndVarDefs, variables);
};
CustomRewriter.prototype.rewriteResponse = function (response, key, index, nodeMatchAndParents) {
return this.rewriteResponseFn(response, key, index, nodeMatchAndParents);
};
CustomRewriter.prototype.rewriteVariables = function (nodeAndVarDefs, variables) {
return this.rewriteVariablesFn(nodeAndVarDefs, variables);
};
return CustomRewriter;
}(Rewriter));
var fragmentMatchCondition = function (_a) {

@@ -1118,3 +846,3 @@ var _b = _a === void 0 ? {} : _a, fragmentNames = _b.fragmentNames, fragmentTypes = _b.fragmentTypes, pathRegexes = _b.pathRegexes;

export { RewriteHandler, Rewriter, FieldArgNameRewriter, FieldArgsToInputTypeRewriter, FieldArgTypeRewriter, NestFieldOutputsRewriter, ScalarFieldToObjectFieldRewriter, JsonToTypedObjectRewriter, FieldRewriter, CustomRewriter, fragmentMatchCondition, queryMatchCondition, mutationMatchCondition };
export { RewriteHandler, Rewriter, FieldArgNameRewriter, FieldArgsToInputTypeRewriter, FieldArgTypeRewriter, NestFieldOutputsRewriter, ScalarFieldToObjectFieldRewriter, JsonToTypedObjectRewriter, fragmentMatchCondition, queryMatchCondition, mutationMatchCondition };
//# sourceMappingURL=index.es5.js.map

@@ -47,12 +47,2 @@ (function (global, factory) {

function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
}
/** @hidden */

@@ -258,19 +248,10 @@ var identifyFunc = function (val) { return val; };

/**
* Return the path that will be returned in the response from the chain of parents.
* By default this will only build up paths for field nodes, but the anyKind flag allows
* to build paths for any named node.
*
* It also supports aliases.
* return the path that will be returned in the response from from the chain of parents
*/
/** @hidden */
var extractPath = function (parents, anyKind) {
var extractPath = function (parents) {
var path = [];
parents.forEach(function (parent) {
if (parent.kind === 'Field' || anyKind) {
if (parent.alias) {
path.push(parent.alias.value);
}
else if (parent.name) {
path.push(parent.name.value);
}
if (parent.kind === 'Field') {
path.push(parent.name.value);
}

@@ -280,24 +261,4 @@ });

};
/**
* return an ArgumentNode with a VariableNode as its value node with matching name.
*/
/** @hidden */
var astArgVarNode = function (argName) {
return {
kind: graphql.Kind.ARGUMENT,
name: {
kind: graphql.Kind.NAME,
value: argName
},
value: {
kind: graphql.Kind.VARIABLE,
name: {
kind: graphql.Kind.NAME,
value: argName
}
}
};
};
/** @hidden */
var rewriteResultsAtPath = function (results, path, callback, includesNonFieldPaths) {
var rewriteResultsAtPath = function (results, path, callback) {
if (path.length === 0)

@@ -308,7 +269,2 @@ return results;

var curResults = results[curPathElm];
// if results[curPathElm] is an empty array, call the callback response rewriter
// because there's nothing left to do.
if (Array.isArray(curResults) && curResults.length === 0) {
callback(results, curPathElm);
}
if (path.length === 1) {

@@ -321,10 +277,2 @@ if (Array.isArray(curResults)) {

var remainingPath = path.slice(1);
// If curResults is undefined, and includesNonFieldPaths is true,
// then curResults is not a field path, so call the callback to allow rewrites
// for non-field paths.
if (remainingPath.length && includesNonFieldPaths && curResults === undefined) {
callback(results, curPathElm);
// Then just continue with the next path
return rewriteResultsAtPath(results, remainingPath, callback, includesNonFieldPaths);
}
// if the path stops here, just return results without any rewriting

@@ -335,7 +283,7 @@ if (curResults === undefined || curResults === null)

newResults[curPathElm] = curResults.map(function (result) {
return rewriteResultsAtPath(result, remainingPath, callback, includesNonFieldPaths);
return rewriteResultsAtPath(result, remainingPath, callback);
});
}
else {
newResults[curPathElm] = rewriteResultsAtPath(curResults, remainingPath, callback, includesNonFieldPaths);
newResults[curPathElm] = rewriteResultsAtPath(curResults, remainingPath, callback);
}

@@ -375,7 +323,5 @@ return newResults;

rewrittenVariables = rewriter.rewriteVariables(rewrittenNodeAndVars, rewrittenVariables);
rewrittenNodeAndVars = rewriter.rewriteQuery(rewrittenNodeAndVars, rewrittenVariables);
var fieldPath = extractPath(parents.concat([rewrittenNodeAndVars.node]));
var anyPath = extractPath(parents.concat([rewrittenNodeAndVars.node]), true);
var fieldPaths = [fieldPath];
var allPaths = [anyPath];
rewrittenNodeAndVars = rewriter.rewriteQuery(rewrittenNodeAndVars);
var simplePath = extractPath(parents.concat([rewrittenNodeAndVars.node]));
var paths = [simplePath];
var fragmentDef = parents.find(function (_a) {

@@ -386,10 +332,7 @@ var kind = _a.kind;

if (fragmentDef) {
fieldPaths = fragmentTracer.prependFragmentPaths(fragmentDef.name.value, fieldPath);
allPaths = fragmentTracer.prependFragmentPaths(fragmentDef.name.value, anyPath);
paths = fragmentTracer.prependFragmentPaths(fragmentDef.name.value, simplePath);
}
_this.matches.push({
rewriter: rewriter,
allPaths: allPaths,
paths: fieldPaths,
nodeMatchAndParents: parents.concat([rewrittenNodeAndVars.node])
paths: paths
});

@@ -413,11 +356,6 @@ }

var rewrittenResponse = response;
this.matches
.reverse()
.forEach(function (_a) {
var rewriter = _a.rewriter, fieldPaths = _a.paths, allPaths = _a.allPaths, nodeMatchAndParents = _a.nodeMatchAndParents;
var paths = rewriter.includeNonFieldPathsInMatch ? allPaths : fieldPaths;
this.matches.reverse().forEach(function (_a) {
var rewriter = _a.rewriter, paths = _a.paths;
paths.forEach(function (path) {
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key, index) {
return rewriter.rewriteResponse(parentResponse, key, index, nodeMatchAndParents);
}, rewriter.includeNonFieldPathsInMatch);
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key, index) { return rewriter.rewriteResponse(parentResponse, key, index); });
});

@@ -436,11 +374,6 @@ });

function Rewriter(_a) {
var fieldName = _a.fieldName, rootTypes = _a.rootTypes, matchConditions = _a.matchConditions, _b = _a.includeNonFieldPathsInMatch, includeNonFieldPathsInMatch = _b === void 0 ? false : _b;
this.includeNonFieldPathsInMatch = false;
var fieldName = _a.fieldName, rootTypes = _a.rootTypes, matchConditions = _a.matchConditions;
this.rootTypes = ['query', 'mutation', 'fragment'];
this.fieldName = fieldName;
this.matchConditions = matchConditions;
this.includeNonFieldPathsInMatch = includeNonFieldPathsInMatch;
if (!this.fieldName && !this.matchConditions) {
throw new Error('Neither a fieldName or matchConditions were provided. Please choose to pass either one in order to be able to detect which fields to rewrite.');
}
if (rootTypes)

@@ -451,9 +384,4 @@ this.rootTypes = rootTypes;

var node = nodeAndVarDefs.node;
// If no fieldName is provided, check for defined matchConditions.
// This avoids having to define one rewriter for many fields individually.
// Alternatively, regex matching for fieldName could be implemented.
if (node.kind !== 'Field' ||
(this.fieldName ? node.name.value !== this.fieldName : !this.matchConditions)) {
if (node.kind !== 'Field' || node.name.value !== this.fieldName)
return false;
}
var root = parents[0];

@@ -473,3 +401,3 @@ if (root.kind === 'OperationDefinition' &&

};
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) {
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs) {
return nodeAndVarDefs;

@@ -484,3 +412,3 @@ };

*/
Rewriter.prototype.rewriteResponse = function (response, key, index, nodeMatchAndParents) {
Rewriter.prototype.rewriteResponse = function (response, key, index) {
return response;

@@ -500,7 +428,3 @@ };

if (Array.isArray(element)) {
// if element is an empty array do not try to get
// one of its array elements
if (element.length !== 0) {
element = element[index] || null;
}
element = element[index] || null;
}

@@ -634,3 +558,2 @@ return element;

_this.coerceVariable = options.coerceVariable || identifyFunc;
_this.coerceArgumentValue = options.coerceArgumentValue || identifyFunc;
return _this;

@@ -647,78 +570,36 @@ }

return false;
// does this field contain arguments?
if (!node.arguments)
if (node.name.value !== this.fieldName || !node.arguments)
return false;
// is there an argument with the correct name and type in a variable?
var matchingArgument = node.arguments.find(function (arg) { return arg.name.value === _this.argName; });
if (!matchingArgument)
if (!matchingArgument || matchingArgument.value.kind !== 'Variable')
return false;
// argument value is stored in a variable
if (matchingArgument.value.kind === 'Variable') {
var varRef = matchingArgument.value.name.value;
// does the referenced variable have the correct type?
for (var _i = 0, variableDefinitions_1 = variableDefinitions; _i < variableDefinitions_1.length; _i++) {
var varDefinition = variableDefinitions_1[_i];
if (varDefinition.variable.name.value === varRef) {
return nodesMatch(this.oldTypeNode, varDefinition.type);
}
var varRef = matchingArgument.value.name.value;
// does the referenced variable have the correct type?
for (var _i = 0, variableDefinitions_1 = variableDefinitions; _i < variableDefinitions_1.length; _i++) {
var varDefinition = variableDefinitions_1[_i];
if (varDefinition.variable.name.value === varRef) {
return nodesMatch(this.oldTypeNode, varDefinition.type);
}
}
// argument value comes in query doc.
else {
var argValueNode = matchingArgument.value;
return graphql.isValueNode(argValueNode);
// Would be ideal to do a nodesMatch in here, however argument value nodes
// have different format for their values than when passed as variables.
// For instance, are parsed with Kinds as "graphql.Kind" (e.g., INT="IntValue") and not "graphql.TokenKinds" (e.g., INT="Int")
// So they might not match correctly. Also they dont contain additional parsed syntax
// as the non-optional symbol "!". So just return true if the argument.value is a ValueNode.
//
// return nodesMatch(this.oldTypeNode, parseType(argRef.kind));
}
return false;
};
FieldArgTypeRewriter.prototype.rewriteQuery = function (_a, variables) {
FieldArgTypeRewriter.prototype.rewriteQuery = function (_a) {
var _this = this;
var astNode = _a.node, variableDefinitions = _a.variableDefinitions;
var node = astNode;
var node = _a.node, variableDefinitions = _a.variableDefinitions;
var varRefName = this.extractMatchingVarRefName(node);
// If argument value is stored in a variable
if (varRefName) {
var newVarDefs = variableDefinitions.map(function (varDef) {
if (varDef.variable.name.value !== varRefName)
return varDef;
return __assign({}, varDef, { type: _this.newTypeNode });
});
return { node: node, variableDefinitions: newVarDefs };
}
// If argument value is not stored in a variable but in the query node.
var matchingArgument = (node.arguments || []).find(function (arg) { return arg.name.value === _this.argName; });
if (node.arguments && matchingArgument) {
var args = node.arguments.slice();
var newValue = this.coerceArgumentValue(matchingArgument.value, { variables: variables, args: args });
/**
* TODO: If somewhow we can get the schema here, we could make the coerceArgumentValue
* even easier, as we would be able to construct the ast node for the argument value.
* as of now, the user has to take care of correctly constructing the argument value ast node herself.
*
* const schema = makeExecutableSchema({typeDefs})
* const myCustomType = schema.getType("MY_CUSTOM_TYPE_NAME")
* const newArgValue = astFromValue(newValue, myCustomType)
* Object.assign(matchingArgument, { value: newArgValue })
*/
if (newValue)
Object.assign(matchingArgument, { value: newValue });
}
return { node: node, variableDefinitions: variableDefinitions };
var newVarDefs = variableDefinitions.map(function (varDef) {
if (varDef.variable.name.value !== varRefName)
return varDef;
return __assign({}, varDef, { type: _this.newTypeNode });
});
return { node: node, variableDefinitions: newVarDefs };
};
FieldArgTypeRewriter.prototype.rewriteVariables = function (_a, variables) {
var astNode = _a.node;
var node = _a.node;
var _b;
var node = astNode;
if (!variables)
return variables;
var varRefName = this.extractMatchingVarRefName(node);
var args = (node.arguments ? node.arguments : []).slice();
return __assign({}, variables, (varRefName
? (_b = {}, _b[varRefName] = this.coerceVariable(variables[varRefName], { variables: variables, args: args }), _b) : {}));
return __assign({}, variables, (_b = {}, _b[varRefName] = this.coerceVariable(variables[varRefName]), _b));
};

@@ -728,4 +609,3 @@ FieldArgTypeRewriter.prototype.extractMatchingVarRefName = function (node) {

var matchingArgument = (node.arguments || []).find(function (arg) { return arg.name.value === _this.argName; });
var variableNode = matchingArgument.value;
return variableNode.kind === graphql.Kind.VARIABLE && variableNode.name.value;
return matchingArgument.value.name.value;
};

@@ -896,154 +776,2 @@ return FieldArgTypeRewriter;

/**
* More generic version of ScalarFieldToObjectField rewriter
*/
var FieldRewriter = /** @class */ (function (_super) {
__extends(FieldRewriter, _super);
function FieldRewriter(options) {
var _this = _super.call(this, options) || this;
_this.newFieldName = options.newFieldName;
_this.arguments = options.arguments;
_this.objectFieldName = options.objectFieldName;
return _this;
}
FieldRewriter.prototype.matches = function (nodeAndVars, parents) {
if (!_super.prototype.matches.call(this, nodeAndVars, parents))
return false;
var node = nodeAndVars.node;
// if there's the intention of converting the field to a subselection
// make sure there's no subselections on this field
if (node.selectionSet && !!this.objectFieldName)
return false;
return true;
};
FieldRewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) {
var _this = this;
var node = nodeAndVarDefs.node;
var variableDefinitions = nodeAndVarDefs.variableDefinitions;
// if there's the intention of converting the field to a subselection
// and there's a subselection already, just return
if (node.selectionSet && !!this.objectFieldName)
return nodeAndVarDefs;
// if fieldName is meant to be renamed.
if (this.newFieldName) {
var newName = this.newFieldName;
if (this.newFieldName.includes(':')) {
var _a = this.newFieldName.split(':'), alias = _a[0], name_1 = _a[1];
newName = name_1.trim();
Object.assign(node, { alias: { value: alias.trim(), kind: graphql.Kind.NAME } });
}
Object.assign(node.name, { value: newName });
}
// if there's the intention of converting the field to a subselection
// of objectFieldNames assign SelectionSetNode to the field accordingly.
if (this.objectFieldName) {
var selectionSet = {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: this.objectFieldName }
}
]
};
Object.assign(node, { selectionSet: selectionSet });
}
// If, 1) the field is a SelectionSet,
// 2) this.arguments is not empty nor undefined, and
// 3) query comes with variables, then assign ArgumentNodes to the field accordingly.
if (node.selectionSet && !!this.arguments && variables) {
// field may already come with some arguments
var newArguments_1 = (node.arguments || []).slice();
this.arguments.forEach(function (argName) {
if (_this.isArgumentInVariables(argName, variables) &&
!_this.isArgumentInArguments(argName, newArguments_1)) {
newArguments_1.push(astArgVarNode(argName));
}
});
if (!!newArguments_1)
Object.assign(node, { arguments: newArguments_1 });
}
return {
variableDefinitions: variableDefinitions,
node: node
};
};
FieldRewriter.prototype.rewriteResponse = function (response, key, index) {
var _a;
// Extract the element we are working on
var element = _super.prototype.extractReponseElement.call(this, response, key, index);
if (element === null)
return response;
var originalKey = key;
// if the key is found to be the renamed field
// then change the name of such field in the response
// and pass the new key (field name) down.
if (this.newFieldName) {
var newFieldName = this.newFieldName;
// the newFieldName may be alised.
if (this.newFieldName.includes(':')) {
var alias = this.newFieldName.split(':')[0];
newFieldName = alias.trim();
}
if (key === newFieldName) {
if (this.fieldName) {
originalKey = this.fieldName;
Object.assign(response, (_a = {}, _a[originalKey] = response[key], _a));
delete response[key];
}
}
}
// If the element is an empty array, return the response, since
// there's nothing left to rewrite down that path.
if (Array.isArray(element) && element.length === 0)
return response;
// Undo the nesting in the response so it matches the original query
var newElement = element;
if (this.objectFieldName) {
newElement = element[this.objectFieldName];
}
return _super.prototype.rewriteResponseElement.call(this, response, newElement, originalKey, index);
};
FieldRewriter.prototype.isArgumentInArguments = function (argName, argumentNodes) {
return argumentNodes.map(function (argNode) { return argNode.name.value; }).includes(argName);
};
FieldRewriter.prototype.isArgumentInVariables = function (argName, variables) {
if (variables && Object.keys(variables).includes(argName))
return true;
return false;
};
return FieldRewriter;
}(Rewriter));
/**
* A Custom rewriter with its Rewriter functions received as arguments.
* This Rewriter allows users to write their own rewriter functions.
*/
var CustomRewriter = /** @class */ (function (_super) {
__extends(CustomRewriter, _super);
function CustomRewriter(options) {
var _this = this;
var matchesFn = options.matchesFn, rewriteQueryFn = options.rewriteQueryFn, rewriteVariablesFn = options.rewriteVariablesFn, rewriteResponseFn = options.rewriteResponseFn, _a = options.matchConditions, matchConditions = _a === void 0 ? [function () { return true; }] : _a, rewriterOpts = __rest(options, ["matchesFn", "rewriteQueryFn", "rewriteVariablesFn", "rewriteResponseFn", "matchConditions"]);
_this = _super.call(this, __assign({}, rewriterOpts, { matchConditions: matchConditions })) || this;
_this.matchesFn = matchesFn || _super.prototype.matches;
_this.rewriteQueryFn = rewriteQueryFn || _super.prototype.rewriteQuery;
_this.rewriteVariablesFn = rewriteVariablesFn || _super.prototype.rewriteVariables;
_this.rewriteResponseFn = rewriteResponseFn || _super.prototype.rewriteResponse;
return _this;
}
CustomRewriter.prototype.matches = function (nodeAndVarDefs, parents) {
return this.matchesFn(nodeAndVarDefs, parents);
};
CustomRewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) {
return this.rewriteQueryFn(nodeAndVarDefs, variables);
};
CustomRewriter.prototype.rewriteResponse = function (response, key, index, nodeMatchAndParents) {
return this.rewriteResponseFn(response, key, index, nodeMatchAndParents);
};
CustomRewriter.prototype.rewriteVariables = function (nodeAndVarDefs, variables) {
return this.rewriteVariablesFn(nodeAndVarDefs, variables);
};
return CustomRewriter;
}(Rewriter));
var fragmentMatchCondition = function (_a) {

@@ -1130,4 +858,2 @@ var _b = _a === void 0 ? {} : _a, fragmentNames = _b.fragmentNames, fragmentTypes = _b.fragmentTypes, pathRegexes = _b.pathRegexes;

exports.JsonToTypedObjectRewriter = JsonToTypedObjectRewriter;
exports.FieldRewriter = FieldRewriter;
exports.CustomRewriter = CustomRewriter;
exports.fragmentMatchCondition = fragmentMatchCondition;

@@ -1134,0 +860,0 @@ exports.queryMatchCondition = queryMatchCondition;

@@ -14,3 +14,2 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var graphql_1 = require("graphql");
var utils_1 = require("./utils");

@@ -209,19 +208,10 @@ var ignoreKeys = new Set(['loc']);

/**
* Return the path that will be returned in the response from the chain of parents.
* By default this will only build up paths for field nodes, but the anyKind flag allows
* to build paths for any named node.
*
* It also supports aliases.
* return the path that will be returned in the response from from the chain of parents
*/
/** @hidden */
exports.extractPath = function (parents, anyKind) {
exports.extractPath = function (parents) {
var path = [];
parents.forEach(function (parent) {
if (parent.kind === 'Field' || anyKind) {
if (parent.alias) {
path.push(parent.alias.value);
}
else if (parent.name) {
path.push(parent.name.value);
}
if (parent.kind === 'Field') {
path.push(parent.name.value);
}

@@ -231,24 +221,4 @@ });

};
/**
* return an ArgumentNode with a VariableNode as its value node with matching name.
*/
/** @hidden */
exports.astArgVarNode = function (argName) {
return {
kind: graphql_1.Kind.ARGUMENT,
name: {
kind: graphql_1.Kind.NAME,
value: argName
},
value: {
kind: graphql_1.Kind.VARIABLE,
name: {
kind: graphql_1.Kind.NAME,
value: argName
}
}
};
};
/** @hidden */
exports.rewriteResultsAtPath = function (results, path, callback, includesNonFieldPaths) {
exports.rewriteResultsAtPath = function (results, path, callback) {
if (path.length === 0)

@@ -259,7 +229,2 @@ return results;

var curResults = results[curPathElm];
// if results[curPathElm] is an empty array, call the callback response rewriter
// because there's nothing left to do.
if (Array.isArray(curResults) && curResults.length === 0) {
callback(results, curPathElm);
}
if (path.length === 1) {

@@ -272,10 +237,2 @@ if (Array.isArray(curResults)) {

var remainingPath = path.slice(1);
// If curResults is undefined, and includesNonFieldPaths is true,
// then curResults is not a field path, so call the callback to allow rewrites
// for non-field paths.
if (remainingPath.length && includesNonFieldPaths && curResults === undefined) {
callback(results, curPathElm);
// Then just continue with the next path
return exports.rewriteResultsAtPath(results, remainingPath, callback, includesNonFieldPaths);
}
// if the path stops here, just return results without any rewriting

@@ -286,7 +243,7 @@ if (curResults === undefined || curResults === null)

newResults[curPathElm] = curResults.map(function (result) {
return exports.rewriteResultsAtPath(result, remainingPath, callback, includesNonFieldPaths);
return exports.rewriteResultsAtPath(result, remainingPath, callback);
});
}
else {
newResults[curPathElm] = exports.rewriteResultsAtPath(curResults, remainingPath, callback, includesNonFieldPaths);
newResults[curPathElm] = exports.rewriteResultsAtPath(curResults, remainingPath, callback);
}

@@ -293,0 +250,0 @@ return newResults;

26

dist/lib/RewriteHandler.js

@@ -35,7 +35,5 @@ "use strict";

rewrittenVariables = rewriter.rewriteVariables(rewrittenNodeAndVars, rewrittenVariables);
rewrittenNodeAndVars = rewriter.rewriteQuery(rewrittenNodeAndVars, rewrittenVariables);
var fieldPath = ast_1.extractPath(parents.concat([rewrittenNodeAndVars.node]));
var anyPath = ast_1.extractPath(parents.concat([rewrittenNodeAndVars.node]), true);
var fieldPaths = [fieldPath];
var allPaths = [anyPath];
rewrittenNodeAndVars = rewriter.rewriteQuery(rewrittenNodeAndVars);
var simplePath = ast_1.extractPath(parents.concat([rewrittenNodeAndVars.node]));
var paths = [simplePath];
var fragmentDef = parents.find(function (_a) {

@@ -46,10 +44,7 @@ var kind = _a.kind;

if (fragmentDef) {
fieldPaths = fragmentTracer.prependFragmentPaths(fragmentDef.name.value, fieldPath);
allPaths = fragmentTracer.prependFragmentPaths(fragmentDef.name.value, anyPath);
paths = fragmentTracer.prependFragmentPaths(fragmentDef.name.value, simplePath);
}
_this.matches.push({
rewriter: rewriter,
allPaths: allPaths,
paths: fieldPaths,
nodeMatchAndParents: parents.concat([rewrittenNodeAndVars.node])
paths: paths
});

@@ -73,11 +68,6 @@ }

var rewrittenResponse = response;
this.matches
.reverse()
.forEach(function (_a) {
var rewriter = _a.rewriter, fieldPaths = _a.paths, allPaths = _a.allPaths, nodeMatchAndParents = _a.nodeMatchAndParents;
var paths = rewriter.includeNonFieldPathsInMatch ? allPaths : fieldPaths;
this.matches.reverse().forEach(function (_a) {
var rewriter = _a.rewriter, paths = _a.paths;
paths.forEach(function (path) {
rewrittenResponse = ast_1.rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key, index) {
return rewriter.rewriteResponse(parentResponse, key, index, nodeMatchAndParents);
}, rewriter.includeNonFieldPathsInMatch);
rewrittenResponse = ast_1.rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key, index) { return rewriter.rewriteResponse(parentResponse, key, index); });
});

@@ -84,0 +74,0 @@ });

@@ -43,3 +43,2 @@ "use strict";

_this.coerceVariable = options.coerceVariable || utils_1.identifyFunc;
_this.coerceArgumentValue = options.coerceArgumentValue || utils_1.identifyFunc;
return _this;

@@ -56,78 +55,36 @@ }

return false;
// does this field contain arguments?
if (!node.arguments)
if (node.name.value !== this.fieldName || !node.arguments)
return false;
// is there an argument with the correct name and type in a variable?
var matchingArgument = node.arguments.find(function (arg) { return arg.name.value === _this.argName; });
if (!matchingArgument)
if (!matchingArgument || matchingArgument.value.kind !== 'Variable')
return false;
// argument value is stored in a variable
if (matchingArgument.value.kind === 'Variable') {
var varRef = matchingArgument.value.name.value;
// does the referenced variable have the correct type?
for (var _i = 0, variableDefinitions_1 = variableDefinitions; _i < variableDefinitions_1.length; _i++) {
var varDefinition = variableDefinitions_1[_i];
if (varDefinition.variable.name.value === varRef) {
return ast_1.nodesMatch(this.oldTypeNode, varDefinition.type);
}
var varRef = matchingArgument.value.name.value;
// does the referenced variable have the correct type?
for (var _i = 0, variableDefinitions_1 = variableDefinitions; _i < variableDefinitions_1.length; _i++) {
var varDefinition = variableDefinitions_1[_i];
if (varDefinition.variable.name.value === varRef) {
return ast_1.nodesMatch(this.oldTypeNode, varDefinition.type);
}
}
// argument value comes in query doc.
else {
var argValueNode = matchingArgument.value;
return graphql_1.isValueNode(argValueNode);
// Would be ideal to do a nodesMatch in here, however argument value nodes
// have different format for their values than when passed as variables.
// For instance, are parsed with Kinds as "graphql.Kind" (e.g., INT="IntValue") and not "graphql.TokenKinds" (e.g., INT="Int")
// So they might not match correctly. Also they dont contain additional parsed syntax
// as the non-optional symbol "!". So just return true if the argument.value is a ValueNode.
//
// return nodesMatch(this.oldTypeNode, parseType(argRef.kind));
}
return false;
};
FieldArgTypeRewriter.prototype.rewriteQuery = function (_a, variables) {
FieldArgTypeRewriter.prototype.rewriteQuery = function (_a) {
var _this = this;
var astNode = _a.node, variableDefinitions = _a.variableDefinitions;
var node = astNode;
var node = _a.node, variableDefinitions = _a.variableDefinitions;
var varRefName = this.extractMatchingVarRefName(node);
// If argument value is stored in a variable
if (varRefName) {
var newVarDefs = variableDefinitions.map(function (varDef) {
if (varDef.variable.name.value !== varRefName)
return varDef;
return __assign({}, varDef, { type: _this.newTypeNode });
});
return { node: node, variableDefinitions: newVarDefs };
}
// If argument value is not stored in a variable but in the query node.
var matchingArgument = (node.arguments || []).find(function (arg) { return arg.name.value === _this.argName; });
if (node.arguments && matchingArgument) {
var args = node.arguments.slice();
var newValue = this.coerceArgumentValue(matchingArgument.value, { variables: variables, args: args });
/**
* TODO: If somewhow we can get the schema here, we could make the coerceArgumentValue
* even easier, as we would be able to construct the ast node for the argument value.
* as of now, the user has to take care of correctly constructing the argument value ast node herself.
*
* const schema = makeExecutableSchema({typeDefs})
* const myCustomType = schema.getType("MY_CUSTOM_TYPE_NAME")
* const newArgValue = astFromValue(newValue, myCustomType)
* Object.assign(matchingArgument, { value: newArgValue })
*/
if (newValue)
Object.assign(matchingArgument, { value: newValue });
}
return { node: node, variableDefinitions: variableDefinitions };
var newVarDefs = variableDefinitions.map(function (varDef) {
if (varDef.variable.name.value !== varRefName)
return varDef;
return __assign({}, varDef, { type: _this.newTypeNode });
});
return { node: node, variableDefinitions: newVarDefs };
};
FieldArgTypeRewriter.prototype.rewriteVariables = function (_a, variables) {
var astNode = _a.node;
var node = _a.node;
var _b;
var node = astNode;
if (!variables)
return variables;
var varRefName = this.extractMatchingVarRefName(node);
var args = (node.arguments ? node.arguments : []).slice();
return __assign({}, variables, (varRefName
? (_b = {}, _b[varRefName] = this.coerceVariable(variables[varRefName], { variables: variables, args: args }), _b) : {}));
return __assign({}, variables, (_b = {}, _b[varRefName] = this.coerceVariable(variables[varRefName]), _b));
};

@@ -137,4 +94,3 @@ FieldArgTypeRewriter.prototype.extractMatchingVarRefName = function (node) {

var matchingArgument = (node.arguments || []).find(function (arg) { return arg.name.value === _this.argName; });
var variableNode = matchingArgument.value;
return variableNode.kind === graphql_1.Kind.VARIABLE && variableNode.name.value;
return matchingArgument.value.name.value;
};

@@ -141,0 +97,0 @@ return FieldArgTypeRewriter;

@@ -17,6 +17,2 @@ "use strict";

exports.JsonToTypedObjectRewriter = JsonToTypedObjectRewriter_1.default;
var FieldRewriter_1 = require("./FieldRewriter");
exports.FieldRewriter = FieldRewriter_1.default;
var CustomRewriter_1 = require("./CustomRewriter");
exports.CustomRewriter = CustomRewriter_1.default;
//# sourceMappingURL=index.js.map

@@ -9,11 +9,6 @@ "use strict";

function Rewriter(_a) {
var fieldName = _a.fieldName, rootTypes = _a.rootTypes, matchConditions = _a.matchConditions, _b = _a.includeNonFieldPathsInMatch, includeNonFieldPathsInMatch = _b === void 0 ? false : _b;
this.includeNonFieldPathsInMatch = false;
var fieldName = _a.fieldName, rootTypes = _a.rootTypes, matchConditions = _a.matchConditions;
this.rootTypes = ['query', 'mutation', 'fragment'];
this.fieldName = fieldName;
this.matchConditions = matchConditions;
this.includeNonFieldPathsInMatch = includeNonFieldPathsInMatch;
if (!this.fieldName && !this.matchConditions) {
throw new Error('Neither a fieldName or matchConditions were provided. Please choose to pass either one in order to be able to detect which fields to rewrite.');
}
if (rootTypes)

@@ -24,9 +19,4 @@ this.rootTypes = rootTypes;

var node = nodeAndVarDefs.node;
// If no fieldName is provided, check for defined matchConditions.
// This avoids having to define one rewriter for many fields individually.
// Alternatively, regex matching for fieldName could be implemented.
if (node.kind !== 'Field' ||
(this.fieldName ? node.name.value !== this.fieldName : !this.matchConditions)) {
if (node.kind !== 'Field' || node.name.value !== this.fieldName)
return false;
}
var root = parents[0];

@@ -46,3 +36,3 @@ if (root.kind === 'OperationDefinition' &&

};
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) {
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs) {
return nodeAndVarDefs;

@@ -57,3 +47,3 @@ };

*/
Rewriter.prototype.rewriteResponse = function (response, key, index, nodeMatchAndParents) {
Rewriter.prototype.rewriteResponse = function (response, key, index) {
return response;

@@ -73,7 +63,3 @@ };

if (Array.isArray(element)) {
// if element is an empty array do not try to get
// one of its array elements
if (element.length !== 0) {
element = element[index] || null;
}
element = element[index] || null;
}

@@ -80,0 +66,0 @@ return element;

@@ -1,2 +0,2 @@

import { ArgumentNode, ASTNode, DocumentNode, VariableDefinitionNode } from 'graphql';
import { ASTNode, DocumentNode, VariableDefinitionNode } from 'graphql';
/** @hidden */

@@ -37,16 +37,7 @@ export declare const nodesMatch: (node1: ASTNode, node2: ASTNode) => boolean;

/**
* Return the path that will be returned in the response from the chain of parents.
* By default this will only build up paths for field nodes, but the anyKind flag allows
* to build paths for any named node.
*
* It also supports aliases.
* return the path that will be returned in the response from from the chain of parents
*/
/** @hidden */
export declare const extractPath: (parents: ReadonlyArray<ASTNode>, anyKind?: boolean | undefined) => ReadonlyArray<string>;
/**
* return an ArgumentNode with a VariableNode as its value node with matching name.
*/
export declare const extractPath: (parents: ReadonlyArray<ASTNode>) => ReadonlyArray<string>;
/** @hidden */
export declare const astArgVarNode: (argName: string) => ArgumentNode;
/** @hidden */
interface ResultObj {

@@ -56,3 +47,3 @@ [key: string]: any;

/** @hidden */
export declare const rewriteResultsAtPath: (results: ResultObj, path: ReadonlyArray<string>, callback: (parentResult: any, key: string, position?: number | undefined) => any, includesNonFieldPaths?: boolean | undefined) => ResultObj;
export declare const rewriteResultsAtPath: (results: ResultObj, path: ReadonlyArray<string>, callback: (parentResult: any, key: string, position?: number | undefined) => any) => ResultObj;
export {};

@@ -1,2 +0,1 @@

import { ASTNode } from 'graphql';
import Rewriter, { Variables } from './rewriters/Rewriter';

@@ -6,4 +5,2 @@ interface RewriterMatch {

paths: ReadonlyArray<ReadonlyArray<string>>;
allPaths: ReadonlyArray<ReadonlyArray<string>>;
nodeMatchAndParents?: ASTNode[];
}

@@ -10,0 +7,0 @@ /**

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

import { ArgumentNode, ASTNode, FieldNode, TypeNode, ValueNode } from 'graphql';
import Maybe from 'graphql/tsutils/Maybe';
import { ASTNode, TypeNode } from 'graphql';
import { NodeAndVarDefs } from '../ast';

@@ -9,16 +8,3 @@ import Rewriter, { RewriterOpts, Variables } from './Rewriter';

newType: string;
coerceVariable?: (variable: any, context: {
variables: Variables;
args: ArgumentNode[];
}) => any;
/**
* EXPERIMENTAL:
* This allows to coerce value of argument when their value is not stored in a variable
* but comes in the query node itself.
* NOTE: At the moment, the user has to return the ast value node herself.
*/
coerceArgumentValue?: (variable: any, context: {
variables: Variables;
args: ArgumentNode[];
}) => Maybe<ValueNode>;
coerceVariable?: (variable: any) => any;
}

@@ -33,17 +19,10 @@ /**

protected newTypeNode: TypeNode;
protected coerceVariable: (variable: any, context: {
variables: Variables;
args: ArgumentNode[];
}) => any;
protected coerceArgumentValue: (variable: any, context: {
variables: Variables;
args: ArgumentNode[];
}) => Maybe<ValueNode>;
protected coerceVariable: (variable: any) => any;
constructor(options: FieldArgTypeRewriterOpts);
matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]): boolean;
rewriteQuery({ node: astNode, variableDefinitions }: NodeAndVarDefs, variables: Variables): {
node: FieldNode;
variableDefinitions: ReadonlyArray<import("graphql").VariableDefinitionNode>;
rewriteQuery({ node, variableDefinitions }: NodeAndVarDefs): {
node: ASTNode;
variableDefinitions: import("graphql").VariableDefinitionNode[];
};
rewriteVariables({ node: astNode }: NodeAndVarDefs, variables: Variables): {
rewriteVariables({ node }: NodeAndVarDefs, variables: Variables): {
[x: string]: any;

@@ -50,0 +29,0 @@ } | undefined;

@@ -8,3 +8,1 @@ export { default as Rewriter, RewriterOpts } from './Rewriter';

export { default as JsonToTypedObjectRewriter } from './JsonToTypedObjectRewriter';
export { default as FieldRewriter } from './FieldRewriter';
export { default as CustomRewriter } from './CustomRewriter';

@@ -9,6 +9,5 @@ import { ASTNode } from 'graphql';

export interface RewriterOpts {
fieldName?: string;
fieldName: string;
rootTypes?: RootType[];
matchConditions?: matchCondition[];
includeNonFieldPathsInMatch?: boolean;
}

@@ -20,11 +19,10 @@ /**

declare abstract class Rewriter {
includeNonFieldPathsInMatch: boolean;
protected fieldName: string;
protected rootTypes: RootType[];
protected fieldName?: string;
protected matchConditions?: matchCondition[];
constructor({ fieldName, rootTypes, matchConditions, includeNonFieldPathsInMatch }: RewriterOpts);
constructor({ fieldName, rootTypes, matchConditions }: RewriterOpts);
matches(nodeAndVarDefs: NodeAndVarDefs, parents: ReadonlyArray<ASTNode>): boolean;
rewriteQuery(nodeAndVarDefs: NodeAndVarDefs, variables: Variables): NodeAndVarDefs;
rewriteQuery(nodeAndVarDefs: NodeAndVarDefs): NodeAndVarDefs;
rewriteVariables(nodeAndVarDefs: NodeAndVarDefs, variables: Variables): Variables;
rewriteResponse(response: any, key: string, index?: number, nodeMatchAndParents?: ASTNode[]): any;
rewriteResponse(response: any, key: string, index?: number): any;
protected extractReponseElement(response: any, key: string, index?: number): any;

@@ -31,0 +29,0 @@ protected rewriteResponseElement(response: any, newElement: any, key: string, index?: number): any;

{
"name": "graphql-query-rewriter-rc",
"version": "0.0.17",
"version": "0.0.18",
"description": "",

@@ -88,3 +88,2 @@ "keywords": [],

"@types/express": "^4.16.1",
"@types/graphql": "^14.2.0",
"@types/jest": "^23.3.2",

@@ -98,3 +97,3 @@ "@types/node": "^10.11.0",

"dedent-js": "^1.0.1",
"graphql": "^14.2.1",
"graphql": "^15",
"husky": "^1.0.1",

@@ -127,4 +126,4 @@ "jest": "^23.6.0",

"peerDependencies": {
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0"
"graphql": "^14.0.0 || ^15.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

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