@kiwicom/graphql-resolve-wrapper

1.0.5 • Public • Published

This package allows you to do basically whatever you want with the resolvers.

Do you like our open source? We are looking for skilled JavaScript developers to help us build it. Check our open positions: https://jobs.kiwi.com/

Installation

yarn add @kiwicom/graphql-resolve-wrapper

Usage

Note: this function mutates the GraphQL schema (it replaces the resolve function).

const Schema = new GraphQLSchema({
  query: RootQuery,
  mutation: RootMutation,
});

wrapResolvers(Schema, resolveFn => async (...args) => {
  const value = await resolveFn(...args);
  return typeof value === 'string' ? value.toUpperCase() : value;
});

export default Schema;

This wrapper would uppercase every string in the response. There are of course more interesting and practical use-cases:

  • error masking (wrap in try/catch and mask only server-side errors)
  • timing (measure duration of every resolver)
  • custom resolve function behavior

The simplest resolver wrapper is this (it just calls the resolver):

function defaultWrapper(resolveFn) {
  return (...args) => resolveFn(...args);
}

Query Timing (advanced example)

Query:

query Test($search: String!) {
  allLocations(first: 10, search: $search) {
    edges {
      node {
        id
        name
      }
    }
  }
}

Wrapper:

wrapResolvers(Schema, resolveFn => async (ancestor, args, context, info) => {
  const startAt = process.hrtime.bigint();
  const response = await resolveFn(ancestor, args, context, info);
  console.warn(process.hrtime.bigint() - startAt, JSON.stringify(info.path));
  return response;
});

Output:

567425567n '{"key":"allLocations"}'
301331n '{"prev":{"prev":{"prev":{"prev":{"key":"allLocations"},"key":"edges"},"key":0},"key":"node"},"key":"id"}'
250083n '{"prev":{"prev":{"prev":{"prev":{"key":"allLocations"},"key":"edges"},"key":0},"key":"node"},"key":"name"}'
237656n '{"prev":{"prev":{"prev":{"prev":{"key":"allLocations"},"key":"edges"},"key":1},"key":"node"},"key":"id"}'
262972n '{"prev":{"prev":{"prev":{"prev":{"key":"allLocations"},"key":"edges"},"key":1},"key":"node"},"key":"name"}'
280750n '{"prev":{"prev":{"prev":{"prev":{"key":"allLocations"},"key":"edges"},"key":2},"key":"node"},"key":"id"}'
305839n '{"prev":{"prev":{"prev":{"prev":{"key":"allLocations"},"key":"edges"},"key":2},"key":"node"},"key":"name"}'
325893n '{"prev":{"prev":{"prev":{"prev":{"key":"allLocations"},"key":"edges"},"key":3},"key":"node"},"key":"id"}'
351468n '{"prev":{"prev":{"prev":{"prev":{"key":"allLocations"},"key":"edges"},"key":3},"key":"node"},"key":"name"}'

Dependents (0)

Package Sidebar

Install

npm i @kiwicom/graphql-resolve-wrapper

Weekly Downloads

2

Version

1.0.5

License

MIT

Unpacked Size

7.08 kB

Total Files

7

Last publish

Collaborators

  • mvidalgarcia
  • dsil
  • jakubzaba
  • robincsl_kiwi
  • kiwicom.platform
  • dinodsaurus