2.0.0 • Published 10 months ago

jsonapi-resolvers v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

jsonapi-resolvers

Build a resolving function to help you resolve JSON-API entities. With support for selecting specific fields & recursively include-ing related documents.

const { createResolver } = require('jsonapi-resolvers');
// Or
import { createResolver } from 'jsonapi-resolvers';

const resolve = createResolver({
  posts(ids) {
    // Fetch posts by ID in the JSONAPI format
  },
  users(ids) {
    // Fetch users by ID in the JSONAPI format
  },
});

// Fetch a single post by ID
const { data } = await resolve('posts', '1234');
// { data: { type: 'posts', id: '1234', ... } }

// Fetch multiple posts by ID
const { data } = await resolve('posts', ['1234', '5678']);
// { data: [
//    { type: 'posts', id: '1234', ... },
//    { type: 'posts', id: '5678', ... } ] }

// Fetch a single post by ID, including users too
const { data } = await resolve('posts', '1234', {
  include: ['author'],
});
// { data: { type: 'posts', id: '1234', ... },
//   included: [ { type: 'users', id: 'abcd' } ]}

// Fetch multiple posts by ID, including users too
const { data } = await resolve('posts', ['1234', '5678']);
// { data: [
//    { type: 'posts', id: '1234', ... },
//    { type: 'posts', id: '5678', ... } ],
//   included: [ { type: 'users', id: 'abcd' } ]}

Install

$ npm install jsonapi-resolvers
# or
$ yarn add jsonapi-resolvers

API

createResolver(fetchers, opts?) => resolve

Create a resolve function for a set of types.

ArgumentTypeDescription
fetchersRecord<string, ResolveFunction>(Required) A dictionary of resolvers, where the name of each function should be the name of the type.
opts?.links?.baseUrlstringIf set, if links are included in the resolver responses they will be prepended by this baseUrl value. See Rewriting links.
  • Description of Resolve functions

resolve(type, id, opts?)

ArgumentTypeDescription
typestring(Required) The entry type you wish to resolve.
id/idsstring/string[](Required) The entry ID or IDs you wish to resolve.
opts?.includestring[]An array of relationships you wish to expand
opts?.fieldsRecord<string, string[]>A list of fields you'd like to return for each type, see selecting fields.

Notes

2.0.0-beta.2

10 months ago

2.0.0-beta.1

10 months ago

2.0.0-beta.0

10 months ago

2.0.0

10 months ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago