graphql-query-rewriter-rc
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,2 +0,2 @@ | ||
import { parse, print, parseType, Kind, isValueNode, valueFromASTUntyped } from 'graphql'; | ||
import { parse, print, isValueNode, Kind, parseType } from 'graphql'; | ||
@@ -377,4 +377,5 @@ /*! ***************************************************************************** | ||
if (node.kind !== 'Field' || | ||
(this.fieldName ? node.name.value !== this.fieldName : !this.matchConditions)) | ||
(this.fieldName ? node.name.value !== this.fieldName : !this.matchConditions)) { | ||
return false; | ||
} | ||
var root = parents[0]; | ||
@@ -394,6 +395,6 @@ if (root.kind === 'OperationDefinition' && | ||
}; | ||
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs, _) { | ||
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) { | ||
return nodeAndVarDefs; | ||
}; | ||
Rewriter.prototype.rewriteVariables = function (_, variables) { | ||
Rewriter.prototype.rewriteVariables = function (nodeAndVarDefs, variables) { | ||
return variables; | ||
@@ -537,4 +538,2 @@ }; | ||
// TODO: Importing this module with ES6 causes type issues at building. | ||
var astFromValueUntyped = require('@graphql-tools/utils').astFromValueUntyped; | ||
/** | ||
@@ -552,2 +551,3 @@ * Rewriter which replaces the type of a single argument of a field | ||
_this.coerceVariable = options.coerceVariable || identifyFunc; | ||
_this.coerceArgumentValue = options.coerceArgumentValue || identifyFunc; | ||
return _this; | ||
@@ -611,12 +611,20 @@ } | ||
// If argument value is not stored in a variable but in the query node. | ||
else { | ||
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.rewriteArgument(matchingArgument.value, { variables: variables, args: args }); | ||
if (newValue) | ||
Object.assign(matchingArgument, { value: newValue }); | ||
} | ||
return { node: node, variableDefinitions: variableDefinitions }; | ||
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 }; | ||
}; | ||
@@ -632,41 +640,10 @@ FieldArgTypeRewriter.prototype.rewriteVariables = function (_a, variables) { | ||
return __assign({}, variables, (varRefName | ||
? (_b = {}, _b[varRefName] = this._coerceVariable(variables[varRefName], { variables: variables, args: args }), _b) : {})); | ||
? (_b = {}, _b[varRefName] = this.coerceVariable(variables[varRefName], { variables: variables, args: args }), _b) : {})); | ||
}; | ||
FieldArgTypeRewriter.prototype.extractMatchingVarRefName = function (node) { | ||
var _this = this; | ||
var matchingArgument = ((node.arguments || []).find(function (arg) { return arg.name.value === _this.argName; })); | ||
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; | ||
}; | ||
FieldArgTypeRewriter.prototype.rewriteArgument = function (value, context) { | ||
var oldArgValue = valueFromASTUntyped(value); | ||
var newArgValue = this._coerceVariable(oldArgValue, context); | ||
if (!newArgValue) | ||
return; | ||
var newArgValueNode = astFromValueUntyped(newArgValue); | ||
return newArgValueNode; | ||
}; | ||
/** | ||
* Since the coerceVariable function is up to the user, | ||
* we better inform if anything goes wrong with it, otherwise | ||
* the rewrite wont happen but the original query will get executed, | ||
* thus unexpected results may be returned back without the user being aware. | ||
*/ | ||
FieldArgTypeRewriter.prototype._coerceVariable = function (value, context) { | ||
var result, error; | ||
try { | ||
result = this.coerceVariable(value, context); | ||
} | ||
catch (error) { | ||
error = '[FieldArgTypeRewriter]: coerceVariable error.'; | ||
console.warn(error); | ||
} | ||
finally { | ||
// If no error and result is undefined is probably going to brake the rewrite. | ||
// Warn the user about this event. | ||
if (result === undefined && !error) | ||
console.warn('[FieldArgTypeRewriter]: coerceVariable returned undefined.'); | ||
return result; | ||
} | ||
}; | ||
return FieldArgTypeRewriter; | ||
@@ -673,0 +650,0 @@ }(Rewriter)); |
@@ -381,4 +381,5 @@ (function (global, factory) { | ||
if (node.kind !== 'Field' || | ||
(this.fieldName ? node.name.value !== this.fieldName : !this.matchConditions)) | ||
(this.fieldName ? node.name.value !== this.fieldName : !this.matchConditions)) { | ||
return false; | ||
} | ||
var root = parents[0]; | ||
@@ -398,6 +399,6 @@ if (root.kind === 'OperationDefinition' && | ||
}; | ||
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs, _) { | ||
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) { | ||
return nodeAndVarDefs; | ||
}; | ||
Rewriter.prototype.rewriteVariables = function (_, variables) { | ||
Rewriter.prototype.rewriteVariables = function (nodeAndVarDefs, variables) { | ||
return variables; | ||
@@ -541,4 +542,2 @@ }; | ||
// TODO: Importing this module with ES6 causes type issues at building. | ||
var astFromValueUntyped = require('@graphql-tools/utils').astFromValueUntyped; | ||
/** | ||
@@ -556,2 +555,3 @@ * Rewriter which replaces the type of a single argument of a field | ||
_this.coerceVariable = options.coerceVariable || identifyFunc; | ||
_this.coerceArgumentValue = options.coerceArgumentValue || identifyFunc; | ||
return _this; | ||
@@ -615,12 +615,20 @@ } | ||
// If argument value is not stored in a variable but in the query node. | ||
else { | ||
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.rewriteArgument(matchingArgument.value, { variables: variables, args: args }); | ||
if (newValue) | ||
Object.assign(matchingArgument, { value: newValue }); | ||
} | ||
return { node: node, variableDefinitions: variableDefinitions }; | ||
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 }; | ||
}; | ||
@@ -636,41 +644,10 @@ FieldArgTypeRewriter.prototype.rewriteVariables = function (_a, variables) { | ||
return __assign({}, variables, (varRefName | ||
? (_b = {}, _b[varRefName] = this._coerceVariable(variables[varRefName], { variables: variables, args: args }), _b) : {})); | ||
? (_b = {}, _b[varRefName] = this.coerceVariable(variables[varRefName], { variables: variables, args: args }), _b) : {})); | ||
}; | ||
FieldArgTypeRewriter.prototype.extractMatchingVarRefName = function (node) { | ||
var _this = this; | ||
var matchingArgument = ((node.arguments || []).find(function (arg) { return arg.name.value === _this.argName; })); | ||
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; | ||
}; | ||
FieldArgTypeRewriter.prototype.rewriteArgument = function (value, context) { | ||
var oldArgValue = graphql.valueFromASTUntyped(value); | ||
var newArgValue = this._coerceVariable(oldArgValue, context); | ||
if (!newArgValue) | ||
return; | ||
var newArgValueNode = astFromValueUntyped(newArgValue); | ||
return newArgValueNode; | ||
}; | ||
/** | ||
* Since the coerceVariable function is up to the user, | ||
* we better inform if anything goes wrong with it, otherwise | ||
* the rewrite wont happen but the original query will get executed, | ||
* thus unexpected results may be returned back without the user being aware. | ||
*/ | ||
FieldArgTypeRewriter.prototype._coerceVariable = function (value, context) { | ||
var result, error; | ||
try { | ||
result = this.coerceVariable(value, context); | ||
} | ||
catch (error) { | ||
error = '[FieldArgTypeRewriter]: coerceVariable error.'; | ||
console.warn(error); | ||
} | ||
finally { | ||
// If no error and result is undefined is probably going to brake the rewrite. | ||
// Warn the user about this event. | ||
if (result === undefined && !error) | ||
console.warn('[FieldArgTypeRewriter]: coerceVariable returned undefined.'); | ||
return result; | ||
} | ||
}; | ||
return FieldArgTypeRewriter; | ||
@@ -677,0 +654,0 @@ }(Rewriter)); |
@@ -27,4 +27,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// TODO: Importing this module with ES6 causes type issues at building. | ||
var astFromValueUntyped = require('@graphql-tools/utils').astFromValueUntyped; | ||
var graphql_1 = require("graphql"); | ||
@@ -46,2 +44,3 @@ var ast_1 = require("../ast"); | ||
_this.coerceVariable = options.coerceVariable || utils_1.identifyFunc; | ||
_this.coerceArgumentValue = options.coerceArgumentValue || utils_1.identifyFunc; | ||
return _this; | ||
@@ -105,12 +104,20 @@ } | ||
// If argument value is not stored in a variable but in the query node. | ||
else { | ||
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.rewriteArgument(matchingArgument.value, { variables: variables, args: args }); | ||
if (newValue) | ||
Object.assign(matchingArgument, { value: newValue }); | ||
} | ||
return { node: node, variableDefinitions: variableDefinitions }; | ||
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 }; | ||
}; | ||
@@ -126,41 +133,10 @@ FieldArgTypeRewriter.prototype.rewriteVariables = function (_a, variables) { | ||
return __assign({}, variables, (varRefName | ||
? (_b = {}, _b[varRefName] = this._coerceVariable(variables[varRefName], { variables: variables, args: args }), _b) : {})); | ||
? (_b = {}, _b[varRefName] = this.coerceVariable(variables[varRefName], { variables: variables, args: args }), _b) : {})); | ||
}; | ||
FieldArgTypeRewriter.prototype.extractMatchingVarRefName = function (node) { | ||
var _this = this; | ||
var matchingArgument = ((node.arguments || []).find(function (arg) { return arg.name.value === _this.argName; })); | ||
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; | ||
}; | ||
FieldArgTypeRewriter.prototype.rewriteArgument = function (value, context) { | ||
var oldArgValue = graphql_1.valueFromASTUntyped(value); | ||
var newArgValue = this._coerceVariable(oldArgValue, context); | ||
if (!newArgValue) | ||
return; | ||
var newArgValueNode = astFromValueUntyped(newArgValue); | ||
return newArgValueNode; | ||
}; | ||
/** | ||
* Since the coerceVariable function is up to the user, | ||
* we better inform if anything goes wrong with it, otherwise | ||
* the rewrite wont happen but the original query will get executed, | ||
* thus unexpected results may be returned back without the user being aware. | ||
*/ | ||
FieldArgTypeRewriter.prototype._coerceVariable = function (value, context) { | ||
var result, error; | ||
try { | ||
result = this.coerceVariable(value, context); | ||
} | ||
catch (error) { | ||
error = '[FieldArgTypeRewriter]: coerceVariable error.'; | ||
console.warn(error); | ||
} | ||
finally { | ||
// If no error and result is undefined is probably going to brake the rewrite. | ||
// Warn the user about this event. | ||
if (result === undefined && !error) | ||
console.warn('[FieldArgTypeRewriter]: coerceVariable returned undefined.'); | ||
return result; | ||
} | ||
}; | ||
return FieldArgTypeRewriter; | ||
@@ -167,0 +143,0 @@ }(Rewriter_1.default)); |
@@ -25,4 +25,5 @@ "use strict"; | ||
if (node.kind !== 'Field' || | ||
(this.fieldName ? node.name.value !== this.fieldName : !this.matchConditions)) | ||
(this.fieldName ? node.name.value !== this.fieldName : !this.matchConditions)) { | ||
return false; | ||
} | ||
var root = parents[0]; | ||
@@ -42,6 +43,6 @@ if (root.kind === 'OperationDefinition' && | ||
}; | ||
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs, _) { | ||
Rewriter.prototype.rewriteQuery = function (nodeAndVarDefs, variables) { | ||
return nodeAndVarDefs; | ||
}; | ||
Rewriter.prototype.rewriteVariables = function (_, variables) { | ||
Rewriter.prototype.rewriteVariables = function (nodeAndVarDefs, variables) { | ||
return variables; | ||
@@ -48,0 +49,0 @@ }; |
@@ -1,2 +0,3 @@ | ||
import { ArgumentNode, ASTNode, FieldNode, TypeNode } from 'graphql'; | ||
import { ArgumentNode, ASTNode, FieldNode, TypeNode, ValueNode } from 'graphql'; | ||
import Maybe from 'graphql/tsutils/Maybe'; | ||
import { NodeAndVarDefs } from '../ast'; | ||
@@ -12,2 +13,12 @@ import Rewriter, { RewriterOpts, Variables } from './Rewriter'; | ||
}) => 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>; | ||
} | ||
@@ -26,2 +37,6 @@ /** | ||
}) => any; | ||
protected coerceArgumentValue: (variable: any, context: { | ||
variables: Variables; | ||
args: ArgumentNode[]; | ||
}) => Maybe<ValueNode>; | ||
constructor(options: FieldArgTypeRewriterOpts); | ||
@@ -37,11 +52,3 @@ matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]): boolean; | ||
private extractMatchingVarRefName; | ||
private rewriteArgument; | ||
/** | ||
* Since the coerceVariable function is up to the user, | ||
* we better inform if anything goes wrong with it, otherwise | ||
* the rewrite wont happen but the original query will get executed, | ||
* thus unexpected results may be returned back without the user being aware. | ||
*/ | ||
private _coerceVariable; | ||
} | ||
export default FieldArgTypeRewriter; |
@@ -23,4 +23,4 @@ import { ASTNode } from 'graphql'; | ||
matches(nodeAndVarDefs: NodeAndVarDefs, parents: ReadonlyArray<ASTNode>): boolean; | ||
rewriteQuery(nodeAndVarDefs: NodeAndVarDefs, _?: Variables): NodeAndVarDefs; | ||
rewriteVariables(_: NodeAndVarDefs, variables: Variables): Variables; | ||
rewriteQuery(nodeAndVarDefs: NodeAndVarDefs, variables: Variables): NodeAndVarDefs; | ||
rewriteVariables(nodeAndVarDefs: NodeAndVarDefs, variables: Variables): Variables; | ||
rewriteResponse(response: any, key: string, index?: number): any; | ||
@@ -27,0 +27,0 @@ protected extractReponseElement(response: any, key: string, index?: number): any; |
{ | ||
"name": "graphql-query-rewriter-rc", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "", | ||
@@ -87,3 +87,2 @@ "keywords": [], | ||
"@commitlint/config-conventional": "^7.1.2", | ||
"@graphql-tools/utils": "^8.6.12", | ||
"@types/express": "^4.16.1", | ||
@@ -90,0 +89,0 @@ "@types/graphql": "^14.2.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
327227
38
64
3221