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

graphql-query-rewriter

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-query-rewriter - npm Package Compare versions

Comparing version 3.0.0 to 3.0.1

91

dist/index.es5.js

@@ -264,7 +264,3 @@ import { parse, print, parseType } from 'graphql';

if (Array.isArray(curResults)) {
newResults[curPathElm] = curResults.map(function (_, index) {
var newValue = callback(curResults, index);
return newValue;
});
return newResults;
return curResults.reduce(function (reducedResults, _, index) { return callback(reducedResults, curPathElm, index); }, results);
}

@@ -352,5 +348,3 @@ return callback(results, curPathElm);

paths.forEach(function (path) {
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key) {
return rewriter.rewriteResponse(parentResponse, key);
});
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key, index) { return rewriter.rewriteResponse(parentResponse, key, index); });
});

@@ -400,5 +394,45 @@ });

};
Rewriter.prototype.rewriteResponse = function (response, key) {
/*
* Receives the parent object of the matched field with the key of the matched field.
* For arrays, the index of the element is also present.
*/
Rewriter.prototype.rewriteResponse = function (response, key, index) {
return response;
};
/*
* Helper that extracts the element from the response if possible otherwise returns null.
*/
Rewriter.prototype.extractReponseElement = function (response, key, index) {
// Verify the response format
var element = null;
if (response === null || typeof response !== 'object')
return element;
// Extract the key
element = response[key] || null;
// Extract the position
if (Array.isArray(element)) {
element = element[index] || null;
}
return element;
};
/*
* Helper that rewrite the element from the response if possible and returns the response.
*/
Rewriter.prototype.rewriteResponseElement = function (response, newElement, key, index) {
// Verify the response format
if (response === null || typeof response !== 'object')
return response;
// Extract the key
var element = response[key];
// Extract the position
// NOTE: We might eventually want to create an array if one is not present at the key
// and we receive an index in input
if (Array.isArray(element)) {
element[index] = newElement;
}
else {
response[key] = newElement;
}
return response;
};
return Rewriter;

@@ -612,13 +646,12 @@ }());

};
NestFieldOutputsRewriter.prototype.rewriteResponse = function (response, key) {
var _a;
var pathResponse = response[key];
if (typeof pathResponse === 'object') {
// undo the nesting in the response so it matches the original query
if (pathResponse[this.newOutputName] &&
typeof pathResponse[this.newOutputName] === 'object') {
var rewrittenResponse = __assign({}, pathResponse, pathResponse[this.newOutputName]);
delete rewrittenResponse[this.newOutputName];
return __assign({}, response, (_a = {}, _a[key] = rewrittenResponse, _a));
}
NestFieldOutputsRewriter.prototype.rewriteResponse = function (response, key, index) {
// Extract the element we are working on
var element = _super.prototype.extractReponseElement.call(this, response, key, index);
if (element === null || typeof element !== 'object')
return response;
// Undo the nesting in the response so it matches the original query
if (element[this.newOutputName] && typeof element[this.newOutputName] === 'object') {
var newElement = __assign({}, element, element[this.newOutputName]);
delete newElement[this.newOutputName];
return _super.prototype.rewriteResponseElement.call(this, response, newElement, key, index);
}

@@ -631,3 +664,3 @@ return response;

/**
* Rewriter which nests output fields inside of a new output object
* Rewriter which nests a scalar field inside of a new output object
* ex: change from `field { subField }` to `field { subField { objectfield } }`

@@ -671,10 +704,10 @@ */

};
ScalarFieldToObjectFieldRewriter.prototype.rewriteResponse = function (response, key) {
var _a;
if (typeof response === 'object') {
var pathResponse = response[key];
// undo the nesting in the response so it matches the original query
return __assign({}, response, (_a = {}, _a[key] = pathResponse[this.objectFieldName], _a));
}
return response;
ScalarFieldToObjectFieldRewriter.prototype.rewriteResponse = function (response, key, index) {
// Extract the element we are working on
var element = _super.prototype.extractReponseElement.call(this, response, key, index);
if (element === null)
return response;
// Undo the nesting in the response so it matches the original query
var newElement = element[this.objectFieldName];
return _super.prototype.rewriteResponseElement.call(this, response, newElement, key, index);
};

@@ -681,0 +714,0 @@ return ScalarFieldToObjectFieldRewriter;

@@ -268,7 +268,3 @@ (function (global, factory) {

if (Array.isArray(curResults)) {
newResults[curPathElm] = curResults.map(function (_, index) {
var newValue = callback(curResults, index);
return newValue;
});
return newResults;
return curResults.reduce(function (reducedResults, _, index) { return callback(reducedResults, curPathElm, index); }, results);
}

@@ -356,5 +352,3 @@ return callback(results, curPathElm);

paths.forEach(function (path) {
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key) {
return rewriter.rewriteResponse(parentResponse, key);
});
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key, index) { return rewriter.rewriteResponse(parentResponse, key, index); });
});

@@ -404,5 +398,45 @@ });

};
Rewriter.prototype.rewriteResponse = function (response, key) {
/*
* Receives the parent object of the matched field with the key of the matched field.
* For arrays, the index of the element is also present.
*/
Rewriter.prototype.rewriteResponse = function (response, key, index) {
return response;
};
/*
* Helper that extracts the element from the response if possible otherwise returns null.
*/
Rewriter.prototype.extractReponseElement = function (response, key, index) {
// Verify the response format
var element = null;
if (response === null || typeof response !== 'object')
return element;
// Extract the key
element = response[key] || null;
// Extract the position
if (Array.isArray(element)) {
element = element[index] || null;
}
return element;
};
/*
* Helper that rewrite the element from the response if possible and returns the response.
*/
Rewriter.prototype.rewriteResponseElement = function (response, newElement, key, index) {
// Verify the response format
if (response === null || typeof response !== 'object')
return response;
// Extract the key
var element = response[key];
// Extract the position
// NOTE: We might eventually want to create an array if one is not present at the key
// and we receive an index in input
if (Array.isArray(element)) {
element[index] = newElement;
}
else {
response[key] = newElement;
}
return response;
};
return Rewriter;

@@ -616,13 +650,12 @@ }());

};
NestFieldOutputsRewriter.prototype.rewriteResponse = function (response, key) {
var _a;
var pathResponse = response[key];
if (typeof pathResponse === 'object') {
// undo the nesting in the response so it matches the original query
if (pathResponse[this.newOutputName] &&
typeof pathResponse[this.newOutputName] === 'object') {
var rewrittenResponse = __assign({}, pathResponse, pathResponse[this.newOutputName]);
delete rewrittenResponse[this.newOutputName];
return __assign({}, response, (_a = {}, _a[key] = rewrittenResponse, _a));
}
NestFieldOutputsRewriter.prototype.rewriteResponse = function (response, key, index) {
// Extract the element we are working on
var element = _super.prototype.extractReponseElement.call(this, response, key, index);
if (element === null || typeof element !== 'object')
return response;
// Undo the nesting in the response so it matches the original query
if (element[this.newOutputName] && typeof element[this.newOutputName] === 'object') {
var newElement = __assign({}, element, element[this.newOutputName]);
delete newElement[this.newOutputName];
return _super.prototype.rewriteResponseElement.call(this, response, newElement, key, index);
}

@@ -635,3 +668,3 @@ return response;

/**
* Rewriter which nests output fields inside of a new output object
* Rewriter which nests a scalar field inside of a new output object
* ex: change from `field { subField }` to `field { subField { objectfield } }`

@@ -675,10 +708,10 @@ */

};
ScalarFieldToObjectFieldRewriter.prototype.rewriteResponse = function (response, key) {
var _a;
if (typeof response === 'object') {
var pathResponse = response[key];
// undo the nesting in the response so it matches the original query
return __assign({}, response, (_a = {}, _a[key] = pathResponse[this.objectFieldName], _a));
}
return response;
ScalarFieldToObjectFieldRewriter.prototype.rewriteResponse = function (response, key, index) {
// Extract the element we are working on
var element = _super.prototype.extractReponseElement.call(this, response, key, index);
if (element === null)
return response;
// Undo the nesting in the response so it matches the original query
var newElement = element[this.objectFieldName];
return _super.prototype.rewriteResponseElement.call(this, response, newElement, key, index);
};

@@ -685,0 +718,0 @@ return ScalarFieldToObjectFieldRewriter;

@@ -228,7 +228,3 @@ "use strict";

if (Array.isArray(curResults)) {
newResults[curPathElm] = curResults.map(function (_, index) {
var newValue = callback(curResults, index);
return newValue;
});
return newResults;
return curResults.reduce(function (reducedResults, _, index) { return callback(reducedResults, curPathElm, index); }, results);
}

@@ -235,0 +231,0 @@ return callback(results, curPathElm);

@@ -69,5 +69,3 @@ "use strict";

paths.forEach(function (path) {
rewrittenResponse = ast_1.rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key) {
return rewriter.rewriteResponse(parentResponse, key);
});
rewrittenResponse = ast_1.rewriteResultsAtPath(rewrittenResponse, path, function (parentResponse, key, index) { return rewriter.rewriteResponse(parentResponse, key, index); });
});

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

@@ -77,13 +77,12 @@ "use strict";

};
NestFieldOutputsRewriter.prototype.rewriteResponse = function (response, key) {
var _a;
var pathResponse = response[key];
if (typeof pathResponse === 'object') {
// undo the nesting in the response so it matches the original query
if (pathResponse[this.newOutputName] &&
typeof pathResponse[this.newOutputName] === 'object') {
var rewrittenResponse = __assign({}, pathResponse, pathResponse[this.newOutputName]);
delete rewrittenResponse[this.newOutputName];
return __assign({}, response, (_a = {}, _a[key] = rewrittenResponse, _a));
}
NestFieldOutputsRewriter.prototype.rewriteResponse = function (response, key, index) {
// Extract the element we are working on
var element = _super.prototype.extractReponseElement.call(this, response, key, index);
if (element === null || typeof element !== 'object')
return response;
// Undo the nesting in the response so it matches the original query
if (element[this.newOutputName] && typeof element[this.newOutputName] === 'object') {
var newElement = __assign({}, element, element[this.newOutputName]);
delete newElement[this.newOutputName];
return _super.prototype.rewriteResponseElement.call(this, response, newElement, key, index);
}

@@ -90,0 +89,0 @@ return response;

@@ -40,5 +40,45 @@ "use strict";

};
Rewriter.prototype.rewriteResponse = function (response, key) {
/*
* Receives the parent object of the matched field with the key of the matched field.
* For arrays, the index of the element is also present.
*/
Rewriter.prototype.rewriteResponse = function (response, key, index) {
return response;
};
/*
* Helper that extracts the element from the response if possible otherwise returns null.
*/
Rewriter.prototype.extractReponseElement = function (response, key, index) {
// Verify the response format
var element = null;
if (response === null || typeof response !== 'object')
return element;
// Extract the key
element = response[key] || null;
// Extract the position
if (Array.isArray(element)) {
element = element[index] || null;
}
return element;
};
/*
* Helper that rewrite the element from the response if possible and returns the response.
*/
Rewriter.prototype.rewriteResponseElement = function (response, newElement, key, index) {
// Verify the response format
if (response === null || typeof response !== 'object')
return response;
// Extract the key
var element = response[key];
// Extract the position
// NOTE: We might eventually want to create an array if one is not present at the key
// and we receive an index in input
if (Array.isArray(element)) {
element[index] = newElement;
}
else {
response[key] = newElement;
}
return response;
};
return Rewriter;

@@ -45,0 +85,0 @@ }());

@@ -29,3 +29,3 @@ "use strict";

/**
* Rewriter which nests output fields inside of a new output object
* Rewriter which nests a scalar field inside of a new output object
* ex: change from `field { subField }` to `field { subField { objectfield } }`

@@ -69,10 +69,10 @@ */

};
ScalarFieldToObjectFieldRewriter.prototype.rewriteResponse = function (response, key) {
var _a;
if (typeof response === 'object') {
var pathResponse = response[key];
// undo the nesting in the response so it matches the original query
return __assign({}, response, (_a = {}, _a[key] = pathResponse[this.objectFieldName], _a));
}
return response;
ScalarFieldToObjectFieldRewriter.prototype.rewriteResponse = function (response, key, index) {
// Extract the element we are working on
var element = _super.prototype.extractReponseElement.call(this, response, key, index);
if (element === null)
return response;
// Undo the nesting in the response so it matches the original query
var newElement = element[this.objectFieldName];
return _super.prototype.rewriteResponseElement.call(this, response, newElement, key, index);
};

@@ -79,0 +79,0 @@ return ScalarFieldToObjectFieldRewriter;

@@ -46,3 +46,3 @@ import { ASTNode, DocumentNode, VariableDefinitionNode } from 'graphql';

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

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

export { default as Rewriter } from './Rewriter';
export { default as Rewriter, RewriterOpts } from './Rewriter';
export { default as FieldArgNameRewriter } from './FieldArgNameRewriter';

@@ -3,0 +3,0 @@ export { default as FieldArgsToInputTypeRewriter } from './FieldArgsToInputTypeRewriter';

@@ -18,4 +18,4 @@ import { ASTNode } from 'graphql';

rewriteQuery(nodeAndVarDefs: NodeAndVarDefs): NodeAndVarDefs;
rewriteResponse(response: any, key: string | number): any;
rewriteResponse(response: any, key: string, index?: number): any;
}
export default NestFieldOutputsRewriter;

@@ -25,4 +25,6 @@ import { ASTNode } from 'graphql';

rewriteVariables(nodeAndVarDefs: NodeAndVarDefs, variables: Variables): Variables;
rewriteResponse(response: any, key: string | number): any;
rewriteResponse(response: any, key: string, index?: number): any;
protected extractReponseElement(response: any, key: string, index?: number): any;
protected rewriteResponseElement(response: any, newElement: any, key: string, index?: number): any;
}
export default Rewriter;

@@ -8,3 +8,3 @@ import { ASTNode } from 'graphql';

/**
* Rewriter which nests output fields inside of a new output object
* Rewriter which nests a scalar field inside of a new output object
* ex: change from `field { subField }` to `field { subField { objectfield } }`

@@ -17,4 +17,4 @@ */

rewriteQuery(nodeAndVarDefs: NodeAndVarDefs): NodeAndVarDefs;
rewriteResponse(response: any, key: string | number): any;
rewriteResponse(response: any, key: string, index?: number): any;
}
export default ScalarFieldToObjectFieldRewriter;
{
"name": "graphql-query-rewriter",
"version": "3.0.0",
"version": "3.0.1",
"description": "",

@@ -5,0 +5,0 @@ "keywords": [],

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