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

**URL:** <https://community.neo4j.com/t/is-it-possible-to-create-an-ad-hoc-dynamic-relationship-using-the-graphql-library/61368>\
**Category:** GraphQL & GRANDstack\
**Tags:** cypher, graphql\
**Created:** [March 10, 2023, 8:24pm UTC](https://community.neo4j.com/t/is-it-possible-to-create-an-ad-hoc-dynamic-relationship-using-the-graphql-library/61368 "2023-03-10T20:24:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/sgo/32/34799_2.png) [@sgo](https://community.neo4j.com/u/sgo)\
**Post date:** [March 10, 2023, 8:24pm UTC](https://community.neo4j.com/t/is-it-possible-to-create-an-ad-hoc-dynamic-relationship-using-the-graphql-library/61368/1 "2023-03-10T20:24:26Z")

</div>

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.

 ![arrows](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/6/6/66f7283850ad63405c1b2d4a3861b191ab7bd4f4.png)

---

<div class="post-metadata">

**Author:** ![lyonwj](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lyonwj/32/27358_2.png) [@lyonwj](https://community.neo4j.com/u/lyonwj)\
**Post date:** [March 13, 2023, 7:39pm UTC](https://community.neo4j.com/t/is-it-possible-to-create-an-ad-hoc-dynamic-relationship-using-the-graphql-library/61368/2 "2023-03-13T19:39:44Z")

</div>

You could do this by using a [Cypher directive](https://neo4j.com/docs/graphql-manual/current/type-definitions/cypher/) Mutation field to define some custom logic using Cypher. Something like this:

```plaintext
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
  """
}

```

---

<div class="post-metadata">

**Author:** ![sgo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/sgo/32/34799_2.png) [@sgo](https://community.neo4j.com/u/sgo)\
**Post date:** [March 14, 2023, 2:53am UTC](https://community.neo4j.com/t/is-it-possible-to-create-an-ad-hoc-dynamic-relationship-using-the-graphql-library/61368/3 "2023-03-14T02:53:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lyonwj](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lyonwj/32/27358_2.png) [@lyonwj](https://community.neo4j.com/u/lyonwj)\
**Post date:** [March 14, 2023, 3:39pm UTC](https://community.neo4j.com/t/is-it-possible-to-create-an-ad-hoc-dynamic-relationship-using-the-graphql-library/61368/4 "2023-03-14T15:39:57Z")

</div>

I used the [`apoc.create.relationship` procedure](https://neo4j.com/docs/apoc/current/overview/apoc.create/apoc.create.relationship/) because it allows us to dynamically create the relationship type using string concatenation (`WROTE_ON` + `$dateString`)
