I have an example where for a given type, will have more than 10 items, but limited to a fairly small fixed number. In my case, I will have 22 items. I'm not seeing any great options for how to fetch all of these items be default.
Given this simple example of using an @connection:
type Foo @model {
id: ID!
name: String!
bars: [Bar] @connection(name: "Bars")
}
type Bar @model {
id: ID!
name: String!
foo: @connection(name: "Bars")
}
Out of the box, a simple getFoo will only return a max of 10 Bars. What I was thinking would be ideal, is to have an option on the @connection to set the limit, so something like this:
type Foo @model {
id: ID!
name: String!
bars: [Bar] @connection(name: "Bars", limit: "50")
}
The options I see is to getFoo and then have to perform another listBar passing in a limit to be able to get all Bars. Another approach is to edit queries, but that seems so manual and extremely prone to not being done correctly. Modifying a resolver would also have a manual step as well right?
If there's a better way that does not require a manual step for me to perform, I'm all ears.
Any help on this would be greatly appreciated. Thank you!
* Which Category is your question related to? *
graphql
* What AWS Services are you utilizing? *
amplify, AppSync, DynamoDB, graphql
* Provide additional details e.g. code snippets *
see above
I would like to hear an automated solution to this.
But, FWIW, I don't think you'd need to modify the generated list query, and you'd only need to change a single line in your resolver to set the default $limit. You would likely want to do this from your project so that it does not get overwritten by future pushes, but just updating the magic number in the generated resolver will solve your use case.
Line to update in your Query.listModel resolvers:
#set( $limit = $util.defaultIfNull($context.args.limit, 10) )
@malcomm Changing the default limit size is not currently supported via a directive but I have added it as a feature request. You have a few options to get past this today:
Query.listFoo.req.vtl
to the resolvers/
directory and that file will get merged on top of the transform output at build time. You can use this to overwrite the default value.@connection
field exposes a limit
argument that your app can pass when querying to get the full set of results. This does not change the default but will allow you to get all the items when needed.It's simpler to write a shaped query with the limit variable passed, just a matter of choice. If it's a constraint that it's always 22 items you might as well override the generated request resolvers.
@mikeparisstuff could you give an example of #2?
Do you include that somewhere in here?
const getStuff = await await API.graphql(graphqlOperation(getStuff, { id }));
@mikeparisstuff and @jkeys-ecg-nmsu - thanks for the replies. Also thank you for making this a feature request, I think it would be handy. To get around it I am doing my own query and passing in a limit like this:
const statement = `query getFoo (
$filter: FooFilterInput
$limit: Int
$nextToken: String
){
listFoo (filter: $filter, limit: $limit, nextToken: $nextToken) {
__typename
items {
__typename,
id,
fields_i_want ...
},
nextToken
}
}`;
const response: any = (await API.graphql(graphqlOperation(statement, { filter: foofilter, limit: 50 }))) as any;
@karldanninger - hopefully that gives you a good example of how to perform a query like this.
I haven't looked into the resolvers but that looks promising. Thanks all!
@mikeparisstuff - just curious if you have a public link for the feature request? Thanks
@malcomm This issue is the feature-request which we would be tracking.
@kaustavghosh06 - oh thanks, that makes it easy. Sorry I misinterpreted the comment and thought another issues was created for the feature request. I just realized the tag feature-request was added.
Thanks!
@kaustavghosh06 & @mikeparisstuff - I wanted to get back to this issue. So I think this feature was implemented for the "old" connections:
bars: [Bar] @connection(name: "Bars", keyField:"barFooId", limit: "50")
So this is working very well. I've been looking at the latest amplify-cli and I was changing how I as doing my connections, so I changed to this:
bars: [Bar] @connection(keyName: "byFoo", fields: ["id"], limit: 100)
From what I'm seeing is that the limit
is not being respected here and just the default limit of 10
.
I was really hoping that this same feature can be applied.
+1 to @malcomm's comment. The 'new' connection mechanism does not respect the limit
@connection(keyName: "byAddress", fields: ["id"], limit: 50)
It always returns 10 results :(
Any idea how to make this work, preferably without any manual hacking to the auto-generated files?
@mikeparisstuff @kaustavghosh06
Running into the same issue.
@connection(keyName: "reservationByOffer", fields: ["id"], limit: 1000)
still limited to 10 items.
Should be fixed by #4021
Most helpful comment
@kaustavghosh06 & @mikeparisstuff - I wanted to get back to this issue. So I think this feature was implemented for the "old" connections:
So this is working very well. I've been looking at the latest amplify-cli and I was changing how I as doing my connections, so I changed to this:
From what I'm seeing is that the
limit
is not being respected here and just the default limit of10
.I was really hoping that this same feature can be applied.