ardatan / ardatan/graphql-tools
getLoader in batch-delegate uses a non-unique cacheKey
- 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
- [ ] 3. A local solution has been provided
- [ ] 4. A pull request is pending review
---
**Describe the bug**
At https://github.com/ardatan/graphql-tools/blob/master/packages/batch-delegate/src/getLoader.ts#L88 the fieldName seems to always be "_entities" whereas the info.fieldName seems to be the field of the extended type. Thus, the cacheKey is not unique when the selection set matches on different object types! And the wrong loader is then used which results in null values for those entities. In my opinion, the cacheKey should include the Parent Type Name (info.parentType.name) and the Info Field name (info.fieldName) and retain the concatenation of the selection set.
[I'm at a loss to understand why the fieldName variables is not equal to info.fieldName despite it being seemingly initiatize to it, but my debugger shows it is different (and the resultant behavior confirms it).]
**To Reproduce** Steps to reproduce the behavior:
My GraphQL query looks like:
```
query pq {
problemQuery {
account {
id
name
}
topic {
name
environment {
id
name
}
count
}
total
}
}
```
There are 3 sub-schemas stitched together:
_Schema 1_
```
type Problem {
account: Account!
topic: Topic!
total: Int
}
type Account @key(fields : "id") @extends {
id: ID! @external
}
type Environment @key(fields : "id") @extends {
id: ID!
}
type Topic @key(fields : "name environment { id }") @extends {
name: ID!
environment: Environment!
}
```
_Schema 2_
```
Topic @key(fields : "name environment { id }") {
name: ID!
environment: Environment!
count: Int
}
_Schema 3_
```
type Account @key(fields : "id") {
id: ID!
name: String
}
type Environment @key(fields : "id") {
id: ID!
name: String
}
```
**Expected behavior**
The query should return with all the entities populated
**Environment:**
- `@graphql-tools/stitch`: 9.0.3
- NodeJS: v20.5.1
**Additional context**
Contributor guide
Research direction
Start at packages/batch-delegate/src/getLoader.ts around line 88 and reproduce the issue with the provided stitched schemas and query. Inspect how the cache key is formed for different entity types; done means the query returns all entities populated without a loader collision.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nodejs, typescript
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100