Custom hook for mutation

What would be nice is a custom hook that I can use for onUpdate, onAdd, onDelete that will take the pass the uid that I could then use in a custom directive. Sort of like how query has the custom directive for a field. This would allow me for instance to add extra input that is not saved but is used in the custom directive mutation hook.

type Note @onAdd(http: {
  url: ...
  method: ...
  body: ... # can use $uid of added node and/or other fields like $note/$contactGuid
  # or DQL, JavaScript (however that is getting implemented)
}) @onUpdate(...) @onDelete(...) {
  is:ID
  note: String
  contactGuid: String @input # used only for input field for hook usage and not stored like @remote for querying. 
}
2 Likes

We are currently looking into add support for some pre and post processing. If you could execute some pre and post JS hooks before and after the mutation, would that work for you?

3 Likes

If the pre/post processing provided the uid (the actual uid from dgraph even if the @id directive is used for remote id strings) and other fields if wanted/needed, then the only thing missing from this request would be additional input that is not saved into the database but used for that pre/post processing only.

Also this pre/post processing would need to be able to be defined on add/update/delete independently. In cases where I want a pre/post process on a update but not on a deletion or vice versa.

Use Case: Linking a node through a non-unique field. I imagine this being done with a @input directive or something similiar. It would work sort of like the @remote directive but instead of it not saving data and not being in the input, this directive would allow data to be in a mutation input but used somewhere in a processing script and then not saved in the database past that point,

type Note @postAdd( # or whatever this directive is named.
  # be able to use Dgraph's uid and any input variables such as authorGuid (see below)
  # of course this would be something different than a console.log function but rather a script to find any Person type that have the authorGuid value and then fire off another mutation linking that author to this new note by it's uid.
  javascript: """
    console.log(UID)
    console.log(authorGuid)
  """
  variables: { "UID":$uid "authorGuid":$authorGuid }
) {
  id: ID
  guid: String!
  note: String
  authorGuid: String @input # not in any query types used only for input not saved as Note.authorGuid
}

type Person @postUpdate({
  javascript: """
    // a script here to update Person.updated field of the UID node to now()
  """
  variables: { "UID":$uid }
}) {
  id: ID
  guid: String!
  name: String
  updated: DateTime
}

###

mutation {
  addNote(input: [{
    guid: "1234-abcd" # generated uuid not by dgraph.
    note: "Just a note here"
    authorGuid: "9876-zyxv" # variable to use in post processing.
  }]) {
    numUids
  }
}

I will most likely be running DQL, GraphQL to same endpoint, and some JS logic to wrap it all together to conditionally do more DQL/GraphQL queries/mutation.

1 Like

@amaster507 please have a look at the following RFC(Implement custom JS resolvers in GraphQL ). Feel free to chip in there, for discussion.

1 Like