@specialblend/graphql-pipe

functional GraphQL middleware - pipe, compose and stage resolvers

Usage no npm install needed!

<script type="module">
  import specialblendGraphqlPipe from 'https://cdn.skypack.dev/@specialblend/graphql-pipe';
</script>

README

@specialblend/graphql-pipe

functional GraphQL middleware - pipe, compose and stage resolvers

installation

npm install @specialblend/graphql-pipe

examples

pipe and compose

import { curry } from 'ramda'
import { compose, pipe } from '@specialblend/graphql-pipe'

const beforeAnyRequest = curry((next, root, args, context, info) => {
    // do something before
    return next(root, args, context, info)
})

const beforeDeleteComment = curry((next, root, args, context, info) => {
    // do something before
    return next(root, args, context, info)
})

const handleDeleteComment = (root, args, context, info) => {
    // do something
    return {}
}

/**
 * pipe resolvers
 * @type {Function}
 */
const deleteComment = pipe(beforeAnyRequest, beforeDeleteComment, handleDeleteComment)

/**
 * compose resolvers
 * @type {Function}
 */
const deleteComment2 = compose(handleDeleteComment, beforeDeleteComment, beforeAnyRequest)

map

import { curry } from 'ramda'
import { map } from '@specialblend/graphql-pipe'

const postComment = (root, args, context, info) => {
    // do something ...
}

const updateComment = (root, args, context, info) => {
    // do something ...
}

const deleteComment = (root, args, context, info) => {
    // do something ...
}

const checkIsAuthenticated = curry((next, root, args, context, info) => {
    // do something before
    return next(root, args, context, info)
})

const checkIsAuthorized = curry((next, root, args, context, info) => {
    // do something before
    return next(root, args, context, info)
})

const logRequest = curry((next, root, args, context, info) => {
    // do something before
    return next(root, args, context, info)
})

const resolvers = {
    postComment,
    updateComment,
    deleteComment,
}

const withMiddleware = map(resolvers, checkIsAuthenticated, checkIsAuthorized, logRequest)

// will call `checkIsAuthenticated`, `checkIsAuthorized`, `logRequest` before `postComment`
withMiddleware.postComment('root', 'args', 'context', 'info')


stage

import { curry } from 'ramda'
import { stage } from '@specialblend/graphql-pipe'

const beforeDeleteComment = curry((next, root, args, context, info) => {
    // do something before
    return next(root, args, context, info)
})

const afterDeleteComment = curry((response, root, args, context, info) => {
    // do something after
    return response
})

const handleDeleteComment = (root, args, context, info) => {
    // do something
    return {}
}

/**
 * wrap resolvers with before and after middleware
 */
const deleteComment = stage(beforeDeleteComment, afterDeleteComment, handleDeleteComment)