Search code examples
graphqllimit

Adding limit's to nested value in graphql


Here's a simple graphQL query to fetch all people. I'd like to add a limit to to the number of friends each (Person) node will have (say e.g. max 5) when retrieved. Is this possible in graphQL? I know its possible to add a limit to allPeople, something like allPeople(limit: 5) but i don't think that will help my use-case.

{
  allPeople {
    nodes {
      id
      friends {
        name
        id
        phone
      }
    }
  }
}

Solution

  • You can add params to friends 'level' in query

    ... if supported and resolved separately ...

    ... there is no filtering or searching syntax defined in general graphql specs.

    It all depends on specific server/env, how resolved etc.

    ... probably it's better to resolve both levels (people+friends) in people resolver (one sql query) - in this case both filters/limits should be defined on 'parent' level - allPeople(limit:5, friendsLimit:5)