Skip to content

Instantly share code, notes, and snippets.

@ca057
Created April 3, 2018 07:09
Show Gist options
  • Save ca057/4c2abb4768719ff8d710ed0933f1ae1c to your computer and use it in GitHub Desktop.
Save ca057/4c2abb4768719ff8d710ed0933f1ae1c to your computer and use it in GitHub Desktop.
Apollo HOF to conditionally resolve with an empty list
/**
* Curried higher-order function taking a predicate and an actual resolver function. In case the
* predicate returns true, an empty list is returned, otherwise the result of the actual resolver function.
*
* Both the predicate and the resolver function will get the all arguments as a standard resolver function.
*/
const condResolveEmptyList = predicate => resolver => (
data,
args,
context,
info
) => {
if (predicate(data, args, context, info)) return [];
return resolver(data, args, context, info);
};
module.exports = {
condResolveEmptyList,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment