@graphql-ez/plugin-dataloader
TypeScript icon, indicating that this package has built-in type declarations

0.8.1 • Public • Published

@graphql-ez/plugin-dataloader

Integration with DataLoader

Check https://www.graphql-ez.com for more information

Usage

This plugin adds an extra registerDataLoader in your ezApp, which will automatically add the specified dataloader in your GraphQL EZ Application Context.

This EZ Plugin uses @envelop/dataloader plugin seamlessly while you code

import { ezDataLoader } from '@graphql-ez/plugin-dataloader';

const ezApp = CreateApp({
  ez: {
    plugins: [
      ezDataLoader(),
      // ...
    ],
  },
  // ...
});

// ...

import type { InferDataLoader } from '@graphql-ez/plugin-dataloader';

const multiplierDataLoader = ezApp.registerDataLoader(
  'Multiplier',
  DataLoader =>
    new DataLoader<number, number>(async numbers => {
      return numbers.map(v => v * 2);
    })
);

// This snippet add automatically the type of your DataLoader in your EZContext
declare module 'graphql-ez' {
  interface EZContext extends InferDataLoader<typeof multiplierDataLoader> {}
}

// And if you are using https://www.graphql-ez.com/plugins/graphql-modules
// You could use it like this:

ezApp.registerModule(
  gql`
    extend type Query {
      multiply(n: Float!): Float!
    }
  `,
  {
    resolvers: {
      Query: {
        multiply(_root, { n }, { Multiplier }) {
          return Multiplier.load(n);
        },
      },
    },
  }
);

Dependencies (3)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @graphql-ez/plugin-dataloader

    Weekly Downloads

    17

    Version

    0.8.1

    License

    MIT

    Unpacked Size

    7.41 kB

    Total Files

    6

    Last publish

    Collaborators

    • pablosz