ravangen/graphql-rate-limit

Include rate limit data in the default response?

Closed this issue ยท 5 comments

Hi,

Is it possible to include the current rate limit information as an extension to a successful response? Right now, it's only available when the limit has been reached. I'd like to allow users to know where they currently stand with each request.

Something like the following:

"data": {
   ...
},
"extensions": {
    "consumedPoints": 50,
    "remainingPoints": 150,
    "resetAt": "2021-12-12T18:37:51.644Z"
}

Thanks

This is not currently possible, but open to suggestions, feedback, and proposals.

The library provides limits per field, it doesn't have a global limit, so there isn't a summary root level representation of this information.

Optionally returning this internal state sounds reasonable, but would likely need to be per field, and support multiple instances of a rate limiter per field. Currently there is no usage of the extension framework. Having details under extensions makes sense, it is just unfortunate a client cannot easy opt in/out of receiving this (perhaps a hook can be provided).

{
  "data": {
    ...
  },
  "extensions": {
    "directiveName": {
      [
        {
          "path": "object.field",
          "consumedPoints": 50,
          "remainingPoints": 150,
          "resetAt": "2021-12-12T18:37:51.644Z"
        }
      ]
    }
  }
}

The path would likely need to be the schema path (object.field) and not the resolved query execution path (["connection", "edges", 1, "field"]). We would only need one element to describe the final state at the end of the request per limited field contained within the executed operation.

If there are multiple rate limiters in play on a field, should we try to consolidate that context down to a single set of information for a client? Does it take the data of the limiter that resets the latest? What if the one reseting earlier is out of capacity and the next request would fail?

Would this format be meaningful to a client?

Wow, thanks for such a speedy response. And yes, I believe your recommended format would be useful for a client. This is useful for folks who are used to working with REST APIs and seeing the rate limit information in the headers.

@adavis I poked at this problem over the holiday break and posted a proof of concept. My next couple of weeks are pretty busy, but hopefully around the end of January I can polish it up unless someone else wants to give it a go before then.

Out of curiosity, which GraphQL server implementation do you use?

We use Apollo

I've released 2.0.1 which allows for a setState function to write current rate limit information into context.

Formatting response extensions is GraphQL server specific, so I've not included this functionality in the gem. I have provided an example.

With Apollo, I believe you could leverage a plugin to look at context and format the response to match your desires.

Feel free to let me know if I've overlooked anything ๐Ÿ˜„