Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid infinite loop when using WrapQuery on second level fields #1008

Merged
merged 3 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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