Skip to content

Commit

Permalink
Deduping of new variable definitions when delegating to schemas (#607)
Browse files Browse the repository at this point in the history
* add tests demonstrating bug with var generation

* add deduping of new variable definitions when delegating to schemas

* update changelog.md
  • Loading branch information
adamkl authored and freiksenet committed Feb 5, 2018
1 parent 2868fe2 commit 90838c1
Show file tree
Hide file tree
Showing 3 changed files with 472 additions and 332 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change log

### vNEXT
* Fix `delegateToSchema.ts` to remove duplicate new variable definitions when delegating to schemas [PR #607](https://github.com/apollographql/graphql-tools/pull/607)

### v2.19.1
* Fix duplicate subscriptions for schema stitching [PR #609](https://github.com/apollographql/graphql-tools/pull/609)
Expand Down
13 changes: 8 additions & 5 deletions src/stitching/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,17 @@ export function createDocument(
}
}),
};

const newVariableDefinitions = newVariables.map(({ arg, variable }) => {
const newVariableDefinitions: VariableDefinitionNode[] = [];
newVariables.forEach(({ arg, variable }) => {
if (newVariableDefinitions.find(newVarDef => newVarDef.variable.name.value === variable)) {
return;
}
const argDef = rootField.args.find(rootArg => rootArg.name === arg);
if (!argDef) {
throw new Error('Unexpected missing arg');
}
const typeName = typeToAst(argDef.type);
return {
newVariableDefinitions.push({
kind: Kind.VARIABLE_DEFINITION,
variable: {
kind: Kind.VARIABLE,
Expand All @@ -156,7 +159,7 @@ export function createDocument(
},
},
type: typeName,
};
});
});

const {
Expand All @@ -179,7 +182,7 @@ export function createDocument(
variableDefinition =>
usedVariables.indexOf(variableDefinition.variable.name.value) !== -1,
),
...newVariableDefinitions,
...newVariableDefinitions
],
selectionSet,
};
Expand Down
Loading

0 comments on commit 90838c1

Please sign in to comment.