aws-amplify / aws-amplify/amplify-data
Eager load nested objects in list and get queries
- Dominant language
- TypeScript
- Stars
- 18
- Forks
- 23
- Avg merge
- 26m
- Merged PRs (30d)
- 1
Description
### Is this related to a new or existing framework?
Next.js
### Is this related to a new or existing API?
GraphQL API
### Is this related to another service?
_No response_
### Describe the feature you'd like to request
In amplify gen2 the eager load allows to fetch nested (one to many) objects. However it does not fetch all the related objects. It would be nice to allow the client to initiate a full load of the related objects. Or if it is possible then it is not mentioned here:
https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#eagerly-load-a-has-many-relationship
```
const selectionSet = [
'id',
'title',
'content',
'tags.*',
] as const;
// this only returns the first 100 tags for each post
const listResult = await client.models.Post.list({
selectionSet,
});
// this also does not return all the objects
for (const post of listResult.data) {
// for each post we load the tags
const tagsPerPost = await client.models.Post.get(
{
id: post.id,
},
{
selectionSet: ['tags.*'] as const,
}
);
}
```
### Describe the solution you'd like
```
const selectionSet = [
'id',
'title',
'content',
'tags.*',
] as const;
// this way the client would paginate and fetch all the tags until it reaches nextToken null
const listResult = await client.models.Post.list({
selectionSet,
fetchMode: {
tags: "ALL"
}
});
// or
const listResult = await client.models.Post.list({
selectionSet,
fetchMode: {
tags: {
limit: 1000
}
}
});
```
### Describe alternatives you've considered
The alternative is to use, but then we loose the typing.
```
await client.graphql({
query: /* GraphQL */ `
...
```
### Additional context
_No response_
### Is this something that you'd be interested in working on?
- [ ] 👋 I may be able to implement this feature request
- [ ] ⚠️ This feature might incur a breaking change
Contributor guide
Research direction
Start with the eagerly loading relationships documentation and the client.models.Post.list and client.models.Post.get calls shown in the issue. Determine how nested tags pagination currently behaves and how the typed selectionSet API exposes it. Done means a supported option can retrieve all related tags, or a specified limit, while preserving typing and documenting the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, next.js, typescript
- Domain
- api, backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100