cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass a Json object to update single label on a node (Mutation) using Graphql?

john3haris
Node Clone

Lets say I have created a user and I want to update their display name or bio using a mutation, Is there any way to pass a Json object to the mutation and for Neo4j to know exactly what to update?

7 REPLIES 7

MuddyBootsCode
Graph Steward

You can pass the updated user object to the UpdateUser mutation. So like:

UpdateUser({
    variables: { ...updatedUserObject}
})

Will work.

What about the cypher function I'm using a custom Mutation here.

You can create a custom input type to pass the mutation. https://graphql.org/graphql-js/mutations-and-input-types/ that will accomplish the same thing

Here's my mutation;

mutation CreateUser(variables: {$userObject}){
  CreateUser(variables: {$userObject}){
    user_id
   }
}

Trying to build it gives me an error. I'm trying to implement it using Dart so I'm using Artemis Dart package to generate for me some code that I will use but i won't build this Mutation.

Here's one that is working successfully;

mutation CreatePost($author_id: String, $post_id: String, $text: String, $post_created_at: String){
  createPost(author_id: $author_id, post_id: $post_id, text: $text, post_created_at: $post_created_at){
    post_id
    author_id
   }
}

MuddyBootsCode
Graph Steward

So if you were using an input type you would do something in your schema like:

input CreateUserInput {
   author_id: ID!
   post_id: ID!
  etc...
}

Then in your custom mutation you'd have:

mutation CreatePost( $user_input: CreateUsterInput ){
   CreatePost(author_id: $user_input.author_id, etc.....

I'm not sure about dart, I haven't used it but with javascript that works pretty well.

I've tried an input type and it seems to be working fine when creating a node but I found an issue when I try to update the node.. The null properties seem to delete the pre-existing properties with the same name, hence why it took me 5 days to reply, I've been trying all sorts of cypher tricks and trying to modify the Json object to delete the null properties so it can update only the ones it carries, yet with no avail.

I just want to say thank you for your help, it did lead me to the right path, now all I have left to do is to find a way to modify the Json object right before I pass it to the graphql mutation.

Thank you so much for your time @MuddyBootsCode I appreciate it.

P.S I'm using SET user += $userInput to update I hope that's the right way to do it.

MuddyBootsCode
Graph Steward

Glad it's working out for you. You might perform some kind of check in your front end code that only updates changed fields or provides a default you can live with. Sometimes the auto-generated mutations, etc. are great and sometimes the only way to go about anything is make it work on your own.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.