amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Allow @connection to set a limit

Open malcomm opened this issue 7 years ago • 14 comments

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

malcomm avatar Jun 21 '19 17:06 malcomm

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.

jkeys-ecg-nmsu avatar Jun 24 '19 18:06 jkeys-ecg-nmsu

Line to update in your Query.listModel resolvers:

#set( $limit = $util.defaultIfNull($context.args.limit, 10) )

jkeys-ecg-nmsu avatar Jun 24 '19 18:06 jkeys-ecg-nmsu

@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:

  1. As mentioned by @jkeys-ecg-nmsu, you can add a file named 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.
  2. Pass the limit at query time. Each @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.

mikeparisstuff avatar Jun 24 '19 19:06 mikeparisstuff

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.

jkeys-ecg-nmsu avatar Jun 25 '19 00:06 jkeys-ecg-nmsu

@mikeparisstuff could you give an example of #2?

Do you include that somewhere in here?

const getStuff = await await API.graphql(graphqlOperation(getStuff, { id }));

karldanninger avatar Jun 25 '19 17:06 karldanninger

@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!

malcomm avatar Jun 25 '19 18:06 malcomm

@mikeparisstuff - just curious if you have a public link for the feature request? Thanks

malcomm avatar Jun 25 '19 22:06 malcomm

@malcomm This issue is the feature-request which we would be tracking.

kaustavghosh06 avatar Jun 27 '19 17:06 kaustavghosh06

@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!

malcomm avatar Jun 27 '19 17:06 malcomm

@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.

malcomm avatar Jan 10 '20 00:01 malcomm

+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?

amuresia avatar Apr 02 '20 11:04 amuresia

@mikeparisstuff @kaustavghosh06
Running into the same issue. @connection(keyName: "reservationByOffer", fields: ["id"], limit: 1000) still limited to 10 items.

julescsv avatar Apr 18 '20 04:04 julescsv

Should be fixed by #4021

andreialecu avatar Apr 21 '20 15:04 andreialecu

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 26 '21 06:05 github-actions[bot]