This example illustrates returning a different object instead of normal object type resolution on rate limit exceeded.
Inspired by Where art thou, my error?, an Artsy Engineering Blog post, we can give exceptions their own type and return those instead of the success type, when they occur. The onLimit
function returns a RateLimit
object with contextual information and that is part of the query's data in a union type.
WARNING: This approach only works on object types (not scalar types). See Proposal: Support union scalar types.
yarn install
node index.js
Navigate to http://localhost:4000/graphql
in a browser.
Server is configured to allow each field to be queried once every 15 seconds.
Sample query:
{
favouriteBook {
__typename
... on Book {
title
author
}
... on RateLimit {
limit
resetAt
}
}
}
Sample rate limited response:
{
"data": {
"favouriteBook": {
"__typename": "RateLimit",
"limit": 1,
"resetAt": "2019-02-05T04:58:48.238Z"
}
}
}