ardatan / ardatan/graphql-tools
Infinite loop in TransformCompositeFields when using WrapFields
- Dominant language
- TypeScript
- Stars
- 5.4k
- Forks
- 830
- Avg merge
- 10h 59m
- Merged PRs (30d)
- 45
Description
### Issue workflow progress
_Progress of the issue based on the [Contributor Workflow](https://github.com/the-guild-org/Stack/blob/master/CONTRIBUTING.md#a-typical-contributor-workflow)_
- [ ] 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox
> Make sure to fork this template and run `yarn generate` in the terminal.
>
> Please make sure the GraphQL Tools package versions under `package.json` matches yours.
- [ ] 2. A failing test has been provided
- [x] 3. A local solution has been provided ([commit](https://github.com/srolel/graphql-tools/commit/b36eab33a1c1a6635ff69d6c3324b98a0a9b9231))
- [ ] 4. A pull request is pending review
---
**Describe the bug**
When using WrapFields a larger number of times across multiple handlers, a `graphql.visit` invocation gets increasingly slower. I observed that the selection set in the visited document increases in powers of two for some reason. I'm not sure exactly why but after debugging it is a result of this [bit](https://github.com/ardatan/graphql-tools/blob/d8dc67aa6cb05bf10f5f16e90690e5ccc87b3426/packages/wrap/src/transforms/TransformCompositeFields.ts#L150-L161) in `TransformCompositeFields`.
**To Reproduce**
My implementation is complex (and the issue seems to be a result of that complexity) but because the [fix](https://github.com/srolel/graphql-tools/commit/b36eab33a1c1a6635ff69d6c3324b98a0a9b9231) seems harmless I thought I'd report it before trying to reproduce in a minimal example.
**Expected behavior**
The visit function is not stuck in a loop.
**Environment:**
- OS:
- `@graphql-tools/...`:
- NodeJS: 16
**Additional context**
Contributor guide
Research direction
Start with packages/wrap/src/transforms/TransformCompositeFields.ts around lines 150-161 and trace how WrapFields reaches graphql.visit. Review the linked commit b36eab33a1c1a6635ff69d6c3324b98a0a9b9231; done means the visitor no longer loops or causes selection sets to grow exponentially.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100