Skip to content
Utility which enables to wrap GraphQL resolvers with your custom functions
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.npmignore
LICENSE
README.md
package.json

README.md

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"}'
You can’t perform that action at this time.