Code Monkey home page Code Monkey logo

sw-graphql's Introduction

Cache GraphQL POST requests with Service Worker

Please check out the Cache GraphQL POST requests with Service Worker on Medium for a walkthrough of the solution in details.

TL;DR

This is a demo that demonstrates caching GraphQL POST requests with service worker. As Cache Storage API doesn't support caching POST requests, the solution is to cache POST responses in IndexedDB instead of the Cache Storage.

Sine Workbox uses CacheStorage API by default, we will hand-craft the service worker caching functionality in this demo instead of using Workbox.

You can check out the live demo at https://sw-graphql.firebaseapp.com/

Getting started

Start node.js server

Install Node modules and run node server locally. This will open up http://localhost:4000 for the website, and http://localhost:4000/graphql for GraphiQL test web.

npm install
npm start

Inspect and live-reload server

You can also run inspector if you'd like to debug the node server:

node --inspect server.js

Or, use nodemon to run the server with live-reload:

nodemon --inspect server.js

Test GraphQL API endpoint (Optional)

You can open up http://localhost:4000/graphql to test GraphQL API endpoint with the following query string:

{
  restaurants(type:"Japanese", stars: 3) {
    name
    stars
  }
}

This shall return the query result in JSON format like the following:

{
  "data": {
    "restaurants": [
      {
        "name": "Masa",
        "stars": 3
      }
    ]
  }
}

Test service worker's caching for GraphQL requests.

Open up http://localhost:4000 in Chrome's Incognito mode. You will see the index page for 2018 Michelin-Star Restaurants in New York City with two dropdown filters (stars and types). Follow the steps below to verify if a service worker is installed correctly:

  • Open up Chrome's DevTools
  • Go to Application tab and select Service Workers You shall see a service worker (sw.js) is installed and running.
  • Select IndexedDB, and this shall be empty if it's the first time opening up the web app.

In the index page, try changing one filter (e.g. changing Michelin Stars to 3). This will make an GraphQL POST request to the backend. The service worker will cache it in the IndexedDB based on the query.

Now, follow the steps below to verify the cached POST responses in IndexedDB.

  • Refresh the index page.
  • In the Chrome's DevTools, select IndexedDB > GraphQL-Cache > post_cache.
  • In the IndexedDB table, you will see a row with key and value.
  • Expand the value data, and you shall see a key composed of query string, a response JSON based on the POST request, and a timestamp.

Try a different combination of filters and see if it updates the IndexedDB cache.

Questions and Issues?

Please file issues at https://github.com/jonchenn/sw-graphql/issues

Reference

sw-graphql's People

Contributors

jonchenn avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.