Mutation with auto-computed fields


I haven't been able to find a solution that allows me to add auto-computed fields value (mostly metadata fields) to some module's auto-generated mutations.

Let imagine we have the following schema

type Technique {
   uid: ID! @unique 
   created: String!
   modified: String!
   type: String!
   name: String!
   description: String
   revoked: Boolean
   subtechnique_of: [Technique] @relation(name: "SUBTECHNIQUE_OF", direction: OUT)

type SUBTECHNIQUE_OF @relation(name: "SUBTECHNIQUE_OF") {
  from: Technique!
  to: Technique!
  uid: String!
  created: String!
  modified: String!
  type: String!
  description: String

The auto-generated queries are perfectly fine. But I would like to slightly modify the auto-generated mutations.

My first approach was to write the following resolver which works fine with the auto-generated mutation as it overwrite the provided values.

const resolvers = {
  // entry point to GraphQL service
  Mutation: {
    CreateTechnique(object, params, context, resolveInfo) {
      params['uid'] = "attack-pattern--" + uuidv4();
      params['created'] = new Date().toISOString();
      params['modified'] = new Date().toISOString();
      params['type'] = "attack-pattern"
      return neo4jgraphql(object, params, context, resolveInfo);

However I don't think this makes lot of sense to have the user provides the value for overwriting them.
Indeed, fields such as uid, created and modified should not be arguments for the CreatedTechnique, ModifiedTechnique, MergeTechnique mutations (same for the mutation relative to the relation SUBTECHNIQUE_OF).
I would like the user to be able to provide other fields as argument and let the API generates values for the uid, created and modified fields directly and therefore be added as node properties.

As a second approach I tried to add mutations definitions in the schema without the uid, created and modified fields, and adding the parameters within the params object in the resolver (as above). But then, the auto-generated cypher query doesn't include those fields (eventhough I add them to the params parameter of neo4jgraphql().

type Mutation {
      name: String!
      description: String
      revoked: Boolean
   ): Technique

I've investigated the resolveInfo to see if I could inject the fields I would like to be added to the the auto-generated cypher query. But haven't find my way :(

What is the best approach for this use case without needing to rewrite the whole cypher query (still being able to leverage the neo4jgraphql() function).

Thank you for your help.


Once you get to a certain point where the auto-generated mutations hit their limits. It's best to just go ahead and write custom cypher queries and mutations. You'll find it quite a bit easier. I know that nested mutations are in the pipeline soon for the Grandstack as well.

1 Like