Skip to content

Instantly share code, notes, and snippets.

@jean-leonco
Last active January 14, 2021 12:35
Show Gist options
  • Save jean-leonco/e29ef982dea45bf07145faabce59ad3e to your computer and use it in GitHub Desktop.
Save jean-leonco/e29ef982dea45bf07145faabce59ad3e to your computer and use it in GitHub Desktop.
[WIP] GraphQL cache using Redis

When resolver returns node

The key will be the relay globalID and store all db item data. When the node is recovered, the viewerCanSee will filter this.

  • postGlobalID
  • postGlobalID
  • commentGlobalID

Format

{
  "id": "globalID",
  "_id": "mongoID",
  "content": "build a cache with relay"
}

When resolver returns connection

The Key will concat the parent_id, query name, pagination info (first, after) and any possible other argument. At the end, the key will be parent_id::query::first::after::filters. The edge will store the cursor and a reference to the node.

  • root::posts::10::postGlobalID
  • meGlobalID::posts::10::postGlobalID
  • root::comments::10::commentGlobalID

Format

{
  "pageInfo": {},
  "edges": [
    { "cursor": "postCursor", "node": "postGlobalID" },
    { "cursor": "postCursor", "node": "postGlobalID" },
    { "cursor": "postCursor", "node": "postGlobalID" }
  ]
}

Invalidating

When a mutation is called, the key and all connections with this globalID reference will be invalidated. Also, a TTL could be defined to remove the key after some time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment