Skip to content

Commit

Permalink
Avoid infinite loop when using WrapQuery on second level fields (#1008)
Browse files Browse the repository at this point in the history
* Avoid infinite loop when using WrapQuery on second level fields

* Slight formatting tweak
  • Loading branch information
mdlavin authored and hwillson committed Nov 30, 2018
1 parent 3f87d90 commit 9abc074
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
@@ -1,5 +1,11 @@
# Change log

### vNext

* Make `WrapQuery` work for non-root fields <br />
[@mdlavin](https://github.com/mdlavin) in
[#1007](https://github.com/apollographql/graphql-tools/pull/1008)

### 4.0.3

* Replaced broken link in docs homepage with Launchpad example <br />
Expand All @@ -17,7 +23,7 @@
* Changes to `extractExtensionDefinitions` to support `graphql-js` union and enum extensions. <br/>
[@jansuchy](https://github.com/jansuchy) in [#951](https://github.com/apollographql/graphql-tools/pull/951)
* Add docs for `mockServer` (closes [#951](https://github.com/apollographql/graphql-tools/issues/94))<br/>
[@mfix22](https://github.com/mfix22) in [PR #982](https://github.com/apollographql/graphql-tools/pull/982)
[@mfix22](https://github.com/mfix22) in [PR #982](https://github.com/apollographql/graphql-tools/pull/982)
* Fix regression where custom scalars were incorrectly replaced while recreating schema with `visitSchema`. <br/>
[@tgriesser](https://github.com/tgriesser) in [#985](https://github.com/apollographql/graphql-tools/pull/985)

Expand Down
6 changes: 6 additions & 0 deletions src/test/testTransforms.ts
Expand Up @@ -665,6 +665,12 @@ describe('transforms', () => {
zip: result.addressZip
})
),
// Wrap a second level field
new WrapQuery(
['userById', 'zip'],
(subtree: SelectionSetNode) => subtree,
result => result
)
],
});
},
Expand Down
9 changes: 5 additions & 4 deletions src/transforms/WrapQuery.ts
Expand Up @@ -53,11 +53,12 @@ export default class WrapQuery implements Transform {
}

public transformResult(originalResult: Result): Result {
let data = originalResult.data;
if (data) {
const rootData = originalResult.data;
if (rootData) {
let data = rootData;
const path = [...this.path];
while (path.length > 1) {
const next = path.unshift();
const next = path.shift();
if (data[next]) {
data = data[next];
}
Expand All @@ -66,7 +67,7 @@ export default class WrapQuery implements Transform {
}

return {
data,
data: rootData,
errors: originalResult.errors
};
}
Expand Down

0 comments on commit 9abc074

Please sign in to comment.