0

I am trying to retrieve only 5 first records from my query and I saw that there was the keyword "first" to do that onGraphQL. I tried and it says undefined argument. Since it is a GraphQL keyword why it does not recognize it? I am using Ruby on Rails as back-end.

This is the query

{
  users(first: 5) {
    id
    firstName
  }
}

and here is my resolver

module Queries
  class User< Queries::BaseQuery

    argument :id, ID, required: false
    description 'Return current user'

    type [OutputTypes::UserType], null: false

    def resolve(id: nil)
      if id
        ::User.where(id: id)
      else
        ::User.all
      end
    end
  end
end

When I try to run the query I get this error:

{
  "errors": [
    {
      "message": "Field 'users' doesn't accept argument 'first'",
      "locations": [
        {
          "line": 2,
          "column": 9
        }
      ],
      "path": [
        "query",
        "users",
        "first"
      ],
      "extensions": {
        "code": "argumentNotAccepted",
        "name": "users",
        "typeName": "Field",
        "argumentName": "first"
      }
    }
  ]
}
john bowlee
  • 125
  • 1
  • 11

1 Answers1

1

first is not a keyword in GraphQL. GraphQL has no built-in methods for pagination, filtering, sorting, etc. -- it's up to the individual service to implement those features. Using first, last, after and before as arguments is a common pattern for doing pagination that's outlined in the Relay Cursor Connection Specification. However, this is not the only (or necessarily the best) way of doing pagination in GraphQL. You can implement whatever pattern makes sense for your specific application. However, whatever pattern you utilize, you'll need to add the appropriate arguments to your field and also implement the logic inside your resolver that makes use of those arguments' values.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183