Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

adeira/graphql-resolve-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Installation

yarn add @adeira/graphql-resolve-wrapper

Optionally, install TS types (Flow types are included by default):

yarn add --dev @types/adeira__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"}'

About

This repository is automatically exported from https://github.com/adeira/universe via Shipit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published