Coder Social home page Coder Social logo

cachiql's Introduction

Alt text

About CachiQL

Twitter GitHub Stars GitHub license npm bundle size

CachiQL is an ultra-lightweight library designed to batch and cache graphql-js queries to reduce calls to databases. Additionally, CachiQL is written in Javascript for use in Node.js.

Note that batching and caching multiple data requests is not novel to Javascript and Node.js. Additionally, the inspiration behind CachiQL is to deeply understand the implementation of DataLoader, which was created by Lee Byron at Facebook to solve the common N +1 issue of a naive GraphQL server. Our team’s purpose is not to replace DataLoader but rather, to help others understand the rationale behind DataLoader and create a simplified and lightweight NPM package.

Installation


Install the CachiQL package using npm.

npm install --save cachiql

Now in order to use CachiQL, create a new instance of CachiQL. Each instance is created for each request using a web server, such as express.

Although CachiQL does not require any dependencies, this package requires a global Javascript run-time environment with ES6 Promise.

How to Use CachiQL


Batch processing is the cornerstone of the functionality of the CachiQL package. Because GraphQL standardizes resolvers with a depth-first-search execution query, the issue of N+1 arises. Simply put, the server then executes multiple and unnecessary trips to the database, over-fetching to waste computing resources and bandwidth.

const cachiql = require(‘cachiql’);

const schema = new GraphQLSchema({
  query: RootQueryType
});

app.use(
  '/graphql',
  graphqlHTTP({
    schema: schema,
    graphiql: true,
    context: {
      authorLoader: new Cachiql(AuthorLoader),
      bookLoader: new Cachiql(BookLoader),
      cachedData: []
    }
  })
);

Batching


The CachiQL class constructor includes a batch loader that takes in an array of keys and returns a promise for each key which eventually resolves to the return array of values or the promise is rejected.

A loader enables a load of individual values before executing the batch with all the included keys.

const batchAuthors = async (ids) => {
  try {
    const authors = await Author.find({ _id: { $in: ids } });
    return authors;
  } catch (err) {
    throw new Error('There was an error getting the Authors');
  }
};

module.exports = batchAuthors;

const batchBooks = async (ids) => {
  try {
    const books = await Book.find({ _id: { $in: ids } });
    return books;
  } catch (err) {
    throw new Error('There was an error getting the Books');
  }
};

module.exports = batchBooks;

Caching


CachiQL includes a memoized cache for the loads. Additionally, CachiQL further parses through the array of keys, removing duplicates. Finally, note that you can use application-level caches such as Redis. The idea behind CachiQL’s memoized cache is just not to repeat the same data in each request.

CachiQL and GraphQL


The utility of CachiQL is to eliminate the depth-first search structure of GraphQL standard resolvers. In other words, CachiQL aims to solve the N+1 issue that a GraphQL server creates when new database requests are issued as fields are resolved.

In the example provided, using a GraphQL standard resolver, querying a database containing 16 books by three different authors means that submitting a singular deeply nested query issues 16 trips for the information. However, using CachiQL creates more efficiency by only making two round trips to the backend.

NOTE: The array of values needs to be the same length as the array of keys.

cachiql's People

Contributors

kadenj117 avatar fahdie avatar vanessalutz avatar zapata124 avatar

Stargazers

Roman 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.