Skip to content

Commit

Permalink
fix(wrap): rename existing variable names instead of creating a new o…
Browse files Browse the repository at this point in the history
…ne (#4713)

* fix(wrap): rename existing variable names instead of creating a new one

* Go
  • Loading branch information
ardatan authored Sep 14, 2022
1 parent 7d8afa0 commit c21a895
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-seahorses-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/wrap': patch
---

Just rename the existing variable instead of creating a new one
61 changes: 22 additions & 39 deletions packages/wrap/src/transforms/MapLeafValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ import {
visitWithTypeInfo,
OperationDefinitionNode,
FragmentDefinitionNode,
VariableDefinitionNode,
ArgumentNode,
FieldNode,
valueFromAST,
isLeafType,
astFromValue,
valueFromASTUntyped,
} from 'graphql';

import {
ExecutionRequest,
ExecutionResult,
visitResult,
ResultVisitorMap,
updateArgument,
transformInputValue,
createVariableNameGenerator,
} from '@graphql-tools/utils';

import { Transform, DelegationContext, SubschemaConfig } from '@graphql-tools/delegate';
Expand Down Expand Up @@ -133,43 +132,22 @@ export default class MapLeafValues<TContext = Record<string, any>>
variableValues: Record<string, any>
): Array<OperationDefinitionNode> {
return operations.map((operation: OperationDefinitionNode) => {
const variableDefinitionMap: Record<string, VariableDefinitionNode> = (
operation.variableDefinitions ?? []
).reduce(
(prev, def) => ({
...prev,
[def.variable.name.value]: def,
}),
{}
);

const newOperation = visit(
return visit(
operation,
visitWithTypeInfo(this._getTypeInfo(), {
[Kind.FIELD]: node => this.transformFieldNode(node, variableDefinitionMap, variableValues),
[Kind.FIELD]: node => this.transformFieldNode(node, variableValues),
})
);

return {
...newOperation,
variableDefinitions: Object.values(variableDefinitionMap),
};
});
}

private transformFieldNode(
field: FieldNode,
variableDefinitionMap: Record<string, VariableDefinitionNode>,
variableValues: Record<string, any>
): FieldNode | undefined {
private transformFieldNode(field: FieldNode, variableValues: Record<string, any>): FieldNode | undefined {
const targetField = this._getTypeInfo().getFieldDef();

if (!targetField) {
return;
}

const generateVariableName = createVariableNameGenerator(variableDefinitionMap);

if (!targetField.name.startsWith('__')) {
const argumentNodes = field.arguments;
if (argumentNodes != null) {
Expand All @@ -191,20 +169,25 @@ export default class MapLeafValues<TContext = Record<string, any>>
let value: any;
if (argValue != null) {
value = valueFromAST(argValue, argType, variableValues);
if (value == null) {
value = valueFromASTUntyped(argValue, variableValues);
}
}

updateArgument(
argumentNodeMap,
variableDefinitionMap,
variableValues,
argName,
generateVariableName(argName),
argType,
transformInputValue(argType, value, (t, v) => {
const newValue = this.inputValueTransformer(t.name, v);
return newValue === undefined ? v : newValue;
})
);
const transformedValue = transformInputValue(argType, value, (t, v) => {
const newValue = this.inputValueTransformer(t.name, v);
return newValue === undefined ? v : newValue;
});

if (argValue.kind === Kind.VARIABLE) {
variableValues[argValue.name.value] = transformedValue;
} else {
const newValueNode = astFromValue(transformedValue, argType);
argumentNodeMap[argName] = {
...argumentNode,
value: newValueNode!,
};
}
}

return {
Expand Down

1 comment on commit c21a895

@vercel
Copy link

@vercel vercel bot commented on c21a895 Sep 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.