Coder Social home page Coder Social logo

fielddataloader's Introduction

FieldDataLoader build status Coverage Status

DataLoader is a utility for batching and caching requests to backend services. FieldDataLoader is a lightweight wrapper that adds support for requesting specific fields per item. For example:

import { FieldDataLoader } from 'fielddataloader';

var userLoader = new FieldDataLoader((key, fields) => {
  return db.select(/* ... */);
});

// Then, from different places in your app:
userLoader.load(1, ['first_name']);
userLoader.load(1, ['last_name']);
userLoader.load(2, ['first_name']);

// These will be batched into a single callback per ID, for example:
//   SELECT first_name, last_name FROM users WHERE id = 1;
//   SELECT first_name FROM users WHERE id = 2;

This is especially handy for GraphQL resolvers! Use it with the included getSelection helper to load only the specific fields that were requested in the given GraphQL query from your database or backend. For example:

import { FieldDataLoader, getSelection } from 'fielddataloader';

const resolvers = {
  Query: {
    user(obj, args, context, info) => userLoader.load(args.id, getSelection(info)),
  }
}

Getting Started

First, install FieldDataLoader using npm.

npm install --save fielddataloader

To get started, create a FieldDataLoader. Each DataLoader instance represents a unique cache.

Usage

FieldDataLoader replicates most of DataLoader's API:

const dataLoader = new FieldDataLoader(batchLoadFn)

Create a new FieldDataLoader, given a batch loading function.

  • batchLoadFn: A function which accepts a key and an array of fields, and returns a Promise which resolves to an Array of values.
dataLoader.load(key, fields)

Loads fields for the given key, returning a Promise for the result.

  • key: A key value to load.
  • fields: A list of fields to load for that key.
dataLoader.clear(key)

Clears the value at key from the cache, if it exists. Returns itself for method chaining.

  • key: A key value to clear.
dataLoader.clearAll()

Clears the entire cache. To be used when some event results in unknown invalidations across this particular FieldDataLoader. Returns itself for method chaining.

getFields(info, [returnType], [path])

Given the info argument from a GraphQL resolver, returns an array containing the names of any requested fields. Supports named & inline fragments, and fields referenced by Apollo Federation's @requires directive. You can optionally pass a returnType and path if reading fields nested within a query (rather than at the root).

License

© DoSomething.org. FieldDataLoader is free software, and may be redistributed under the terms specified in the LICENSE file. The name and logo for DoSomething.org are trademarks of Do Something, Inc and may not be used without permission.

fielddataloader's People

Contributors

dfurnes avatar dependabot[bot] avatar

Watchers

James Cloos avatar Sena Heydari avatar Gleb Boundin avatar  avatar  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.