cancel
Showing results for 
Search instead for 
Did you mean: 

Can I tell Neo4j GraphQL to ignore variables if they are null?

geronimo4j
Graph Buddy

Hi there - Currently I have a a large mutation that creates Posts that could optionally have attributes which sometimes come in the form of linked nodes. So a post might have a body, created_date, and other properties but might also have a creator which is a connected node.

Sometimes these have ratings which creates and connects to new Rating nodes. The problem is, my mutation accepts these variables either way, so whether I have a value for the rating or not, it seems the mutation will create a new Rating. There are other examples, Rating is not the only type of connected node that may or may not appear in Posts.

So my question is: other than creating a new mutation for every combination of variables (there are very many), is there a way to tell the mutation to totally ignore the field if there's no value submitted?

mutation NEW_POST($body: String, $rating: Float) {
  createPosts(input: { body: $body, rating: { create: { node: { value: $rating }}}}){

Here is a shortened version of what the NEW_POST looks like. I truncated it for simplicity. Essentially, regardless of $rating being a value or null, it still creates and connects to a new Rating node. Any way to tell the system to only create if $rating exists? It looks like it just creates an empty node when $rating is empty.

1 ACCEPTED SOLUTION

geronimo4j
Graph Buddy

Actually this one was solved by passing in variables that were empty objects rather than Float type variables. Instead of having { create: { node: { value: $rating }}} live in the mutation itself, I optionally send that entire thing when I send my variables. So if no $rating value exists, the mutation receives {} instead of a full create action with no value.

View solution in original post

1 REPLY 1

geronimo4j
Graph Buddy

Actually this one was solved by passing in variables that were empty objects rather than Float type variables. Instead of having { create: { node: { value: $rating }}} live in the mutation itself, I optionally send that entire thing when I send my variables. So if no $rating value exists, the mutation receives {} instead of a full create action with no value.

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.