0
votes

I am using neo4j dB and apollo-graphql server for my test project. I am trying to create a Q&A app where a user could create a question (with unique id) and then it would be answered by other user (similar to stackoverflow but with very minimum features).I want to create relationship between "Questions" node and "Users" node in my neo4j db via mutation.

so, in the mutations I created User Type and Question Type. But when I am trying to create relation between the user (who created question) and the question, the mutation is not working as expected.

Here is the graphql schema...

  type Question {
  questionID: Int! # unique id for a particular question
  title: String! # Question Title
  details: String!
  createdBy: User!
   }

 type User {
   userID: Int!
   name: String!
   email: String
   questions: [Question] # specific questions related to the user
 }

  type Mutation {
    createUser(name: String!, userID: Int!): [User]
    createQuestion(questionID: Int!, title: String!, details: String!,userID: Int!  ): Question

And here is the mutation snippet from resolver...

  Mutation: {
      createQuestion(_,params){
      let session = driver.session();
      let query = "MERGE (q:Questions {questionID:{questionID},title:
       {title},details:{details},userID:{userID}})-[:CREATED_BY]->
       (u:Users{userID:{userID}}) RETURN q;"

   return session.run(query,params)
   .then( result => {
          return result.records.map( record => {
             return record.get("q").properties
        }
       )
      }
     )
    },
   }

I had already inserted a dummy user with unique userID "1" in neo4j db before running this mutation query. And I ran this "createQuestion" mutation in graphiQL to create a question for the existing user and so I passed userID "1" as argument,

//createQuestion(questionID: Int!, title: String!, details: String!,userID: Int! )

And after running this mutation query, I was hoping to get a relation of [:CREATED_BY] between the existing user and the question created, But my database now has 2 users with same user id "1" (i.e. one more user with same userid "1" has been inserted and the relationship is now present between this newly inserted user and question node).

What I need is to avoid to create new duplicate user with same id , and rather just to create relationship between the existing user and the question. So could someone please let me know what I have mistaken here ?

1

1 Answers

1
votes

You must change your query:

 let query = "
 MERGE (u:Users{userID:{userID}})
 MERGE (q:Questions {questionID:{questionID},title:
   {title},details:{details},userID:{userID}})
 MERGE (q)-[:CREATED_BY]->(u)
    RETURN q;"

As you have the questionID already in place, which i'm guessing is a unique identifier I would change the query to:

let query = "
 MERGE (u:Users{userID:{userID}})
 MERGE (q:Questions {questionID:{questionID})
 ON CREATE SET q.title=
   {title},q.details={details}
 MERGE (q)-[:CREATED_BY]->(u)
    RETURN q;"

Notice I also removed userID property from question as you do not need it as you already create a relationship from User to Question

Edit:

You can solve the problem from the comment in two ways:

by adding UserID property to question:

let query = "
MERGE (u:Users{userID:{userID}})
MERGE (q:Questions {questionID:{questionID},userID:{userID}})
ON CREATE SET q.title= {title},q.details={details},
MERGE (q)-[:CREATED_BY]->(u)
RETURN q;"

Or with local merges:

let query = "
MERGE (u:Users{userID:{userID}})
MERGE (u)<-[:CREATED_BY]-(q:Questions {questionID:{questionID}})
ON CREATE SET q.title= {title},q.details={details},
RETURN q;"