Hello everyone and thanks for your time reading this,
I'm building a simple personal project, using @neo4j/graphql, and apollo server/client library but I'm stuck on this point:
A logged-in user want to create an article, and n unique tags that can be linked to that or other articles.
Building this functionality piece by piece is fine till i get to the tags creation;
Tags should be unique so using connectOrCreate seems to be a good solution, but i can get it to work.
this is my schema
type Article {
id: ID @id
author: User!
body: String!
comment: [Comment]
userWrote: User @relationship(type: "WROTE", direction: IN)
hasTagTag: Tag @relationship(type: "HAS_TAG", direction: OUT)
createdAt: DateTime @timestamp(operations: [CREATE])
updatedAt: DateTime @timestamp(operations: [UPDATE])
}
type Tag {
id: ID @id
topic: String @unique
article: [Article]
hasTagArticle: Article @relationship(type: "HAS_TAG", direction: IN)
createdAt: DateTime @timestamp(operations: [CREATE])
updatedAt: DateTime @timestamp(operations: [UPDATE])
}
and this my mutation
export const CREATE_ARTICLE = gql`
mutation CreateArticle($input: [ArticleCreateInput!]!) {
createArticles(input: $input) {
articles {
tag
body
}
}
}
`
schema and mutations were generated using arrows.app
and this is the submit article function
const submitArticle = (e) => {
e.preventDefault();
if (!bodyInput.value.length) return
let tagArray = tagInput.value.split(',').map(tag => tag.trim()).filter(t => t && t.length)
debugger
let trimmedBody = bodyInput.value.trim()
field({
variables: {
{
"input": [
{
"body": "body",
"userWrote": {
"connect": {
"where": {
"node": {
"email": "test"
}
}
}
},
"hasTagTag": {
"connectOrCreate": {
"where": {
"node": {
"topic": "tag" // here is where i get stuck ["tag", "gat"] is not working
}
},
"onCreate": {
"node": {
"topic": "tag" // here is where i get stuck ["tag", "gat"] is not working
}
}
}
}
}
]
}
}
});
bodyInput.value = '';
}
input from this function are correct, body is trimmed and i get and clean tags array,
what i can't wrap my head around is how to iterate over this tags array to make every tag a single node linked to the article and be sure that every new article written will be linked to the right tag if it already exist;
Is correct to use a @cypher statement and UNWIND the array or i can use some other tecnique?
I can't map directly inside the function in this fashion
hasTagTag: {
connectOrCreate: tagArray.map(tag => ({ where: { topic: tag }, create: { topic: tag } }))
},
Every help or suggestions will be very useful, if other details are needed please just ask.
Thank again for your time and interest in reading and hopefully solving this.
Ciao