cancel
Showing results for 
Search instead for 
Did you mean: 

ConnectOrCreate

Neo4j GraphQL

When using connectOrCreate

How do I specify in the mutation a property to add to the newly connected or created edge?

Thank you

6 REPLIES 6

ThomasWiss
Neo4j
Neo4j

Hi @david_loving!

You can specify one or several properties as part of the "onCreate".

For create mutations, see the following example: https://neo4j.com/docs/graphql-manual/current/mutations/create/#_connectorcreate_relationships

For update mutations, see this example: https://neo4j.com/docs/graphql-manual/current/mutations/update/#_connectorcreate_relationships

In both cases, the property "title" with the value "Forrest Gump" is added to the newly created edge/node.

Hope that clarifies it!

<deleted>

Like this...

https://neo4j.com/docs/graphql-manual/current/type-definitions/relationships/

like createPeople, actedInMovies, connect, edge, roles but instead using connectOrCreate (instead of connect).

edge and edge properties does / do not appear to be supported in connectOrCreate

Hi David!
Sorry I misread your first post.
They are actually supported, it is however important that the relationships/edges (the directive relationship, see comments in code) have ”relationshipProperties”. Let me make an example.
Given this type definition:

 

 

type Actor {
    name: String!
    movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT, properties: "ActedIn")  // <- note the properties parameter here
}

type Movie {
    title: String
    id: ID! @id
    actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN, properties: "ActedIn”)   // <- note the properties parameter here
}

interface ActedIn @relationshipProperties {   // specify your relationship properties here as an interface
    roles: [String!]
}

 

 

 

An example "connectOrCreate" query using ”edge” properties could look like so:

 

 

mutation CreateActors {
  createActors(
    input: [
      {
        name: "Jon Doe"
        movies: {
          connectOrCreate: [
            {
              where: { node: { id: "1234" } }
              onCreate: {
                node: { title: "Forrest Gump" }
                edge: { roles: ["Harry"] }  // <- edge properties
              }
            }
          ]
        }
      }
    ]
  ) {
    actors {
      name
    }
  }
}​

 

 

 

Hope this clarifies the usage.

Thank you for trying but obviously I have not communicated the problem well enough.  This is not an acceptable solution.  I will handle with separate calls.

I'm having a similar problem. In my case, I want to create a comment in a comment section, but if the comment is the first one, the comment section may or may not already exist.

So I thought I should use connectOrCreate, because if a comment section already exists, I should use that. Otherwise I should create another one.

The problem is that a comment section only consists of an ID and relationships to other nodes, so I can't create it with only primitive types. When I tried to create a comment section in the onCreate using relationships to those other nodes, I got the same "Property values can only be of primitive types" error. So I came to the same conclusion as OP, which is that the use case just isn't supported.

The relationship properties don't solve the problem because the node is still being created with the property "title" instead of a relationship to another specific node.

In case anyone is wondering why I don't create comments directly linked to a post, it's because the same post can be shared in multiple different places, and in each place it has a different comment section. Therefore I decided that comments, comment sections and the original posts should all be separate nodes. If it's a bad practice to have a node that only consists of only relationships to other nodes, please let me know what would be a better way of doing it.

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.