the-guild-org / the-guild-org/apollo-angular
Flush on TestOperation does not work if Query Document contains a Fragment
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 1.5k
- Forks
- 309
- Avg merge
- 1h 45m
- Merged PRs (30d)
- 3
Description
Describe the bug
The ApolloTestingController removes all flushed fields, which are described in a fragment.
To Reproduce
Having the following query:
query GetBenefitOptions {
benefits {
...BenefitOption
}
}
fragment BenefitOption on Benefit {
__typename
id
description
price {
price
}
}
and code in the component:
public readonly options$: Observable<BenefitOptionFragment[]> = this.getBenefitOptions.watch().valueChanges.pipe(
map((result) => result.data.benefits),
tap((val) => console.log(JSON.stringify(val)))
);
I am doing the following test case:
// The change of the inputs should trigger the query
const op: TestOperation<GetBenefitOptionsQuery> = controller.expectOne(GetBenefitOptionsDocument);
// Mock the GraphQL response
op.flushData({
__typename: 'Query',
benefits: [
{
__typename: 'PrivateBenefit',
id: 'benefit-1' as BenefitID,
description: 'Basic Cleaning',
price: { price: 50 },
},
{
__typename: 'PrivateBenefit',
id: 'benefit-2' as BenefitID,
description: 'Premium Cleaning',
price: { price: 100 },
},
{
__typename: 'PrivateBenefit',
id: 'benefit-3' as BenefitID,
description: 'Deep Cleaning',
price: { price: 150 },
},
],
});
The component logs:
[{}, {}, {}]
Changing the query to not using a fragment
query GetBenefitOptions($filter: BenefitsFilterInput!) {
benefits {
__typename
id
description
price {
price
}
}
}
let the component produce the expected output
[{"__typename":"PrivateBenefit","id":"benefit-1","description":"Basic Cleaning","price":{"price":50}},{"__typename":"PrivateBenefit","id":"benefit-2","description":"Premium Cleaning","price":{"price":100}},{"__typename":"PrivateBenefit","id":"benefit-3","description":"Deep Cleaning","price":{"price":150}}]
Expected behavior
TestingController will also work with fragments.
Environment:
├── @angular/cli@18.2.12
├── @angular/core@18.0.5 overridden
├── @apollo/client@3.13.4
├── apollo-angular@10.0.3
├── graphql@16.9.0
└── typescript@5.4.4
Additional context
Using Karma to execute the tests.
Benefit is an interface.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with ApolloTestingController and TestOperation.flushData using the fragment-containing GetBenefitOptionsDocument shown in the reproduction. Run the relevant Karma tests around testing operations, then add a regression case for the BenefitOption fragment. Done means flushed fragment fields are preserved and the component receives the expected benefit objects instead of empty objects.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, graphql, typescript
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100