graphql-query-rewriter-rc
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,2 +0,2 @@ | ||
import { parse, print, parseType, Kind, isValueNode } from 'graphql'; | ||
import { parse, print, parseType, Kind, isValueNode, valueFromASTUntyped } from 'graphql'; | ||
@@ -535,2 +535,4 @@ /*! ***************************************************************************** | ||
// TODO: Importing this module with ES6 causes type issues at building. | ||
var astFromValueUntyped = require('@graphql-tools/utils').astFromValueUntyped; | ||
/** | ||
@@ -548,3 +550,2 @@ * Rewriter which replaces the type of a single argument of a field | ||
_this.coerceVariable = options.coerceVariable || identifyFunc; | ||
_this.coerceArgumentValue = options.coerceArgumentValue || identifyFunc; | ||
return _this; | ||
@@ -612,14 +613,3 @@ } | ||
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 }) | ||
*/ | ||
var newValue = this.rewriteArgument(matchingArgument.value, { variables: variables, args: args }); | ||
if (newValue) | ||
@@ -640,3 +630,3 @@ Object.assign(matchingArgument, { value: newValue }); | ||
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) : {})); | ||
}; | ||
@@ -649,2 +639,33 @@ FieldArgTypeRewriter.prototype.extractMatchingVarRefName = function (node) { | ||
}; | ||
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; | ||
@@ -651,0 +672,0 @@ }(Rewriter)); |
@@ -539,2 +539,4 @@ (function (global, factory) { | ||
// TODO: Importing this module with ES6 causes type issues at building. | ||
var astFromValueUntyped = require('@graphql-tools/utils').astFromValueUntyped; | ||
/** | ||
@@ -552,3 +554,2 @@ * Rewriter which replaces the type of a single argument of a field | ||
_this.coerceVariable = options.coerceVariable || identifyFunc; | ||
_this.coerceArgumentValue = options.coerceArgumentValue || identifyFunc; | ||
return _this; | ||
@@ -616,14 +617,3 @@ } | ||
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 }) | ||
*/ | ||
var newValue = this.rewriteArgument(matchingArgument.value, { variables: variables, args: args }); | ||
if (newValue) | ||
@@ -644,3 +634,3 @@ Object.assign(matchingArgument, { value: newValue }); | ||
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) : {})); | ||
}; | ||
@@ -653,2 +643,33 @@ FieldArgTypeRewriter.prototype.extractMatchingVarRefName = function (node) { | ||
}; | ||
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; | ||
@@ -655,0 +676,0 @@ }(Rewriter)); |
@@ -27,2 +27,4 @@ "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"); | ||
@@ -44,3 +46,2 @@ var ast_1 = require("../ast"); | ||
_this.coerceVariable = options.coerceVariable || utils_1.identifyFunc; | ||
_this.coerceArgumentValue = options.coerceArgumentValue || utils_1.identifyFunc; | ||
return _this; | ||
@@ -108,14 +109,3 @@ } | ||
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 }) | ||
*/ | ||
var newValue = this.rewriteArgument(matchingArgument.value, { variables: variables, args: args }); | ||
if (newValue) | ||
@@ -136,3 +126,3 @@ Object.assign(matchingArgument, { value: newValue }); | ||
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) : {})); | ||
}; | ||
@@ -145,2 +135,33 @@ FieldArgTypeRewriter.prototype.extractMatchingVarRefName = function (node) { | ||
}; | ||
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; | ||
@@ -147,0 +168,0 @@ }(Rewriter_1.default)); |
@@ -1,3 +0,2 @@ | ||
import { ArgumentNode, ValueNode, ASTNode, FieldNode, TypeNode } from 'graphql'; | ||
import Maybe from 'graphql/tsutils/Maybe'; | ||
import { ArgumentNode, ASTNode, FieldNode, TypeNode } from 'graphql'; | ||
import { NodeAndVarDefs } from '../ast'; | ||
@@ -13,12 +12,2 @@ 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>; | ||
} | ||
@@ -37,6 +26,2 @@ /** | ||
}) => any; | ||
protected coerceArgumentValue: (variable: any, context: { | ||
variables: Variables; | ||
args: ArgumentNode[]; | ||
}) => Maybe<ValueNode>; | ||
constructor(options: FieldArgTypeRewriterOpts); | ||
@@ -52,3 +37,11 @@ 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; |
{ | ||
"name": "graphql-query-rewriter-rc", | ||
"version": "0.0.1", | ||
"description": "PR #50 of repo graphql-query-rewriter/core", | ||
"version": "0.0.2", | ||
"description": "", | ||
"keywords": [], | ||
@@ -87,2 +87,3 @@ "main": "dist/index.umd.js", | ||
"@commitlint/config-conventional": "^7.1.2", | ||
"@graphql-tools/utils": "^8.6.12", | ||
"@types/express": "^4.16.1", | ||
@@ -89,0 +90,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
325266
3176
39