cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to deal with nested input when creating a mutation in GraphQL?

I'm trying to create a mutation that will rely on an input type that has inside of it an input type like this:

input GroupInput {
   group_id: String
   group_founder_id: String
   group_name: String
   group_theme_colors: GroupThemeColorsInput
 }

input GroupThemeColorsInput {
   primary_color: String
   secondary_color: String
   background_color: String
 }

and my mutation looks like this:

createGroup(group: GroupInput): Group
    @cypher(
      statement:"""MATCH (u:User)
      WHERE u.user_id CONTAINS group.group_founder_id
      CREATE (u)-[r:CREATED_GROUP]->(g:Group)
      SET g = $group
      RETURN g 
      """
    )

When I call the mutation from the front-end it doesn't create the node even though I see that it did call the cypher function in my Log, but if I take the off GroupThemeColorsInput it works as intended.
This is how I pass the GroupThemeColors from the front end:

"group_theme_colors": CreateGroup$GroupInput$GroupThemeColorsInput.fromJson(group_Theme_colors).toJson(),

exactly the way I pass any other variable like groupInput for example, which works as intended without the nested input type group_input : CreateGroup$GroupInput.fromJson(groupJson),.

So far I'm not sure where the issue is, is it from Neo4j not accepting nested Input types or from Graphql?

P.S. This is the generated cypher function from the Log, it seems to be working as intended but no Node gets created.

CALL apoc.cypher.doIt("MATCH (u:User)
WHERE u.user_id CONTAINS group.group_founder_id
CREATE (u)-[r:CREATED_GROUP]->(g:Group)
SET g = $group
RETURN g ", {group:$group, first:$first, offset:$offset}) YIELD value
    WITH apoc.map.values(value, [keys(value)[0]])[0] AS `group`
    RETURN `group` { .group_id } AS `group`
{
  "group": {
    "group_id": "eb037341863afe0a8ebb5b4e6111ebbf",
    "group_founder_id": "jQGZu6hdbNPPPaaT9qZuLOHXiK13",
    "group_name": "Group_name_test",
    "group_theme_colors": {
      "primary_color": "Color(0xffffffff)",
      "secondary_color": "Color(0xb3ffffff)",
      "background_color": "Color(0xff000000)"
    },
  },
  "first": -1,
  "offset": 0
}

2 REPLIES 2

William_Lyon
Graph Fellow

How would you like to model this data in the graph? What does your Group GraphQL type definition look like? The Cypher statement on the createGroup mutation is trying to set a nested object as a property on the Group node which is not valid.

Typedefs like this will generate the mutations needed for creating the nodes and relationships to represent the model (or you could adjust the Cypher statement in the @cypher schema directive to do it in a single mutation operation)

type Group {
  group_id: String
  group_founder_id: String
  group_name: String
  group_theme_colors: ThemeColor @relation(name: "HAS_THEME", direction: "OUT")
}

type ThemeColor {
  primary_color: String
  secondary_color: String
  background_color: String
}

Hey @William_Lyon I have just tried your suggestion (of creating ThemeColors as a separate Node) and it did work just like it was intended.
Thank you so much for taking time to help me, it was much appreciated.