npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-resolve-adapters

v0.0.2

Published

Supports GraphQL custom directives that hook into the field execution.

Downloads

5

Readme

graphql-resolve-adapters

NPM version Build Status Coverage Status Maintainability codebeat badge devDependency Status JavaScript Style Guide

NPM Downloads

Hooks into GraphQL field execution like Express middleware in a chained way. It can be used for validation or transformation of the resulting field values.

Synopsis

const { adaptFieldResolver } = require('graphql-resolve-adapters')

// Stores durations of each field execution in milliseconds.
const durations = {}
function computeDurations(resolve, source, args, context, info) {
  const start = process.hrtime()
  const result = await resolve()
  const duration = process.hrtime(start)
  durations[info.fieldName] = duration[0] * 1000 + duration[1] / 1000000
  return result
}

// A GraphQL schema computing durations of each field execution.
const schema = ...
const resolveAdapters = [computeDurations]
visitFields(schema, field => adaptFieldResolver(field, resolveAdapters))

// Run the query and print the results with field execution durations.
const response = graphql(schema, '{ ... }')
console.log(response, durations)

Installation

This module can be installed in your project using NPM or Yarn. Make sure, that you use Node.js version 8 or newer.

$ npm i graphql-resolve-adapters -S
$ yarn add graphql-resolve-adapters

Description

Field Resolve Adapter

A field resolve adapter is a function with almost the same prototype as the usual resolve method, only with an additional first parameter - the next resolver to perform or not:

fieldResolveAdapter(resolve, source, args, context, info)

The resolve parameter allows calling the next resolver in chain to inspect or modify its result. The last resolver in the chain is the original field resolve method or the default one.

function validate(resolve, source, args, context, info) {
  const result = await resolve()
  if (!result) {
    throw new Error(`The field "${info.fieldName}" was not truthy.`)
  }
  return result
}

function toLowerCase(resolve, source, args, context, info) {
  const result = await resolve()
  return result.toLowerCase()
}

adaptFieldResolver(field, schema)

Enables hooking into the field execution by a a chain of resolve method adapters for the specified field.

  • field has to be a field configuration object
  • schema has to be an object instance of the type GraphQLSchema

Field configurations are usually obtained from a schema by a field visitor like graphql-field-visitor, for example.

const { adaptFieldResolver } = require('graphql-resolve-adapters')
const { visitFields } = require('graphql-field-visitor')

const schema =  ...
const resolveAdapters = [validate, toLowerCase]
visitFields(schema, field => adaptFieldResolver(field, schema))

If multiple resolve adapters are provided, they will be chained. The last one will be called and its value will be set to the field. The others in the chain may be called depending on their implementations. The first one in the chain is the original field resolve method.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2019-08-18 v0.0.1 Initial release

License

Copyright (c) 2019 Ferdinand Prantl

Licensed under the MIT license.