'How can I add a property to a new node when using onConnectOrCreate in Neo4j GraphQL?

I'm using Neo4j's GraphQL library, trying to use connectOrCreate for a new Image, which is defined like this:

type Image {
    id: ID! @id(autogenerate: true),
    url: String! @unique, 
    creator: User! @relationship(type: "CREATED_BY", direction: OUT),
}

And based on the documentation on the Neo4j website, I should be able to say if a node with the url field is not found, then create one and set some properties. Like this:

mutation NEW_POST {
  createPosts(input: { images: { 
    connectOrCreate: [{ 
      where: { node: { url: "https://picsum.photos/600" }}
      onCreate: { where: { node: { creator: { node: { id: "userId"}}}}},
    }]}}){
  posts {
    id
    }
  }
}

However, for the onCreate Field Input, my only options are id and url. Meaning, I can't actually add the creator property like I do here.

"Field \"creator\" is not defined by type \"ImageOnCreateInput\".",

Do you think this is intended, or any idea where I should look for guidance? I'm looking at this documentation: https://neo4j.com/docs/graphql-manual/current/mutations/create/ which shows onCreate: { node: { title: "Forrest Gump" } } achieving what I hope to achieve.

enter image description here



Solution 1:[1]

If the node with the url https://picsum.photos/600 does not exist, a new node node with the information provided in the onCreate part of the mutation will be created. This "onCreate" node (with ImageOnCreateInput) will automatically connect the node that is specified as the "anchor". See my comments in the slightly modified mutation that you provided:

mutation NEW_POST {
  createPosts(
    input: {
      id: "userId"     // Note this 'id' here. This is the 'anchor' node.
      images: {
        connectOrCreate: [
          {
            where: { node: { url: "https://picsum.photos/600" } }
            onCreate: { node: { name: "the name" } }       // If the node with "https://picsum.photos/600" does not exist, this will create a Post with "the name" and also connect it to the anchor node with id "userId".
          }
        ]
      }
    }
  ) {
    posts {
      id
    }
  }
}

You can experiment and play with the typeDefs and mutation provided in the documentation (https://neo4j.com/docs/graphql-manual/current/mutations/create/#_connectorcreate_relationships) to see how the connectOrCreate relationship mutation is operating.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 angrykoala