0.3.8 • Published 4 years ago

@specialblend/graphql-pipe v0.3.8

Weekly downloads
64
License
ISC
Repository
github
Last release
4 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

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.1

4 years ago

0.2.2

4 years ago

0.2.0

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.39

4 years ago

0.0.38

5 years ago

0.0.37

5 years ago

0.0.36

5 years ago

0.0.35

5 years ago

0.0.34

5 years ago

0.0.33

5 years ago

0.0.32

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.0

5 years ago