graphql-resolve-adapters
v0.0.2
Published
Supports GraphQL custom directives that hook into the field execution.
Downloads
3
Maintainers
Readme
graphql-resolve-adapters
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 objectschema
has to be an object instance of the typeGraphQLSchema
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.