0.3.8 • Published 5 years ago

@specialblend/graphql-pipe v0.3.8

Weekly downloads
64
License
ISC
Repository
github
Last release
5 years ago

@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)
0.3.6

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.1

6 years ago

0.2.2

6 years ago

0.2.0

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.4

6 years ago

0.1.5

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.39

6 years ago

0.0.38

6 years ago

0.0.37

6 years ago

0.0.36

6 years ago

0.0.35

6 years ago

0.0.34

6 years ago

0.0.33

6 years ago

0.0.32

6 years ago

0.0.31

6 years ago

0.0.30

6 years ago

0.0.29

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago