JSPM

dataloader-toolbox

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q18252F
  • License MIT

dataloader toolbox

Package Exports

  • dataloader-toolbox

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dataloader-toolbox) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

dataloader-toolbox

install

npm install dataloader-toolbox

api

createDataLoaderSession

create a session and add to context when you create new context(per request). session function returns dataloader function from normal function.

code

const session = createDataLoaderSession()
// add it to context
const getData = (keys: string[]) => {
  // find data keys: ['key1', 'key2', 'key3']

  return [...data]
}

async function resolver(parent, args, context) {
  context.session(getData).load('key1')
  context.session(getData).load('key2')
  context.session(getData).load('key3')
}

dataLoaderSorter

dataloader does not support sorting, you need to sorter.

code

const resolver = (keys: string[]) => {
  type Item = {
    id: string
  }

  const sql = `
select *
from table
where id in (${keys})`
  
  // query returns Promise<Item[]>
  return query(sql).then(dataLoaderSorter<Item, 'id'>(keys, 'id'))
}

license

MIT