Skip to content

Latest commit

 

History

History

onlimit-object

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

GraphQL Rate Limit - Return object onLimit

This example illustrates returning a different object instead of normal object type resolution on rate limit exceeded.

Overview

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.

Setup

Step 1: Install requirements

yarn install

Run

Step 1: Start server

node index.js

Step 2: Open GraphiQL

Navigate to http://localhost:4000/graphql in a browser.

Step 3: Execute GraphQL operations

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"
    }
  }
}