neo4j-graphql-js
neo4j-graphql-js copied to clipboard
What's the best way to preprocess mutations?
This maybe something I missed in the docs, but I didn't see a way to intercept a mutation call before it generates the required cypher statement or statements? Some of my node mutations need to generate and reformat the input document and generate edge modifications as well. So I need to be able to preprocess the mutation call. Obviously I could patch the schema myself, but I was wondering if there is a provision for some kind of "middlewhere" between the resolver and the final cypher generator and driver calls.
You may want to take a look at graphql-middleware. Middleware functions let you hook into the resolver before and after it fires. The lib is fully compatible with neo4j-graphql-js
-- you just use applyMiddleware
on your schema as shown here. I use it with graphql-shielf because the built-in authentication mechanism isn't suitable for my use case.
One other approach is to manually implement that logic in the resolver, like so:
const resolverMap = {
Mutation: {
myComplexMutation(obj, params, ctx, resolveInfo) {
// do extra stuff,
// e.g. create something and provide an ID ref as an argument
// to the cypher defined in your schema
params.additionalArg = newThingID;
// now proceed with rest of regular execution
// as per the cypher you specified in your mutation
return neo4jgraphql(obj, params, ctx, resolveInfo);
}
}
}
https://github.com/neo4j-graphql/neo4j-graphql-js/issues/608