Is it possible to create an ad-hoc / dynamic relationship using the graphql library?

I would like to create an additional relationship when I create a node. It needs to be dynamic with the day's date as part of the relationship name like this: (User)-[:WROTE_ON_3_9_2023]->(Article) . Is that possible in the graphql library? If not, how would I go about doing this? This is to support an "activity feed" of articles sorted by date. Thanks.

1 Like

You could do this by using a Cypher directive Mutation field to define some custom logic using Cypher. Something like this:

type Mutation {
  createArticleWithDateRelationship(userId: String!, articleText: String!, dateString: String!): Article
  @cypher(statement: """
    MATCH (u:User {userId: $userId})
    MERGE (a:Article {articleText: $articleText})
    MERGE (u)-[:WROTE]->(a)
    WITH *
    CALL apoc.create.relationship(u, "WROTE_ON_" + $dateString, {}, a) YIELD rel
    RETURN a
  """
}
1 Like

Thanks for the help - I'm new for sure. Why do you use "CALL apoc.create.relationship " rather than another MERGE statement?

1 Like

I used the apoc.create.relationship procedure because it allows us to dynamically create the relationship type using string concatenation (WROTE_ON + $dateString)

1 Like