neo4j-graphql-js icon indicating copy to clipboard operation
neo4j-graphql-js copied to clipboard

What's the best way to preprocess mutations?

Open richtera opened this issue 5 years ago • 3 comments

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.

richtera avatar May 22 '19 23:05 richtera

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.

danielrearden avatar Aug 11 '19 23:08 danielrearden

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);
	}
  }
}

benjamin-rood avatar Nov 24 '19 03:11 benjamin-rood

https://github.com/neo4j-graphql/neo4j-graphql-js/issues/608

michaeldgraham avatar May 02 '21 04:05 michaeldgraham