Skip to content

Commit

Permalink
Fix #577 by exploring directives for renaming
Browse files Browse the repository at this point in the history
In query merging, explore directives for varaiable usage and rename effectively the variables.
  • Loading branch information
rricard committed Aug 24, 2016
1 parent 057a6cd commit ea33d01
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/batching/queryMerging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,33 @@ export function renameFragmentSpreads(selSet: SelectionSet, aliasName: string):
return selSet;
}

function renameVariablesInArgument(argument: Argument, aliasName: string): Argument {
if (argument.kind === 'Argument' &&
(argument as Argument).value.kind === 'Variable') {
const varx = argument.value as Variable;
(argument.value as Variable).name.value = getVariableAliasName(varx, aliasName);
}
return argument;
}

export function renameVariables(selSet: SelectionSet, aliasName: string): SelectionSet {
if (selSet && selSet.selections) {
selSet.selections = selSet.selections.map((selection) => {
if (selection.kind === 'Field') {
const field = selection as Field;
if (field.arguments) {
field.arguments = field.arguments.map((argument) => {
if (argument.kind === 'Argument' &&
(argument as Argument).value.kind === 'Variable') {
const varx = argument.value as Variable;
(argument.value as Variable).name.value = getVariableAliasName(varx, aliasName);
field.arguments = field.arguments.map(argument =>
renameVariablesInArgument(argument, aliasName)
);
}
if (field.directives) {
field.directives = field.directives.map((directive) => {
if (directive.arguments) {
directive.arguments = directive.arguments.map(argument =>
renameVariablesInArgument(argument, aliasName)
);
}
return argument;
return directive;
});
}
field.selectionSet = renameVariables(field.selectionSet, aliasName);
Expand Down

0 comments on commit ea33d01

Please sign in to comment.