0

I am new to Graphql and I want to update relational types, but I have no idea how to do it.

My type definitions are as following:

type User {
    email: String!
    username: String!
    password: String!
    registered_at: Date!
    role: String!
    Questionnaire: [Questionnaire!]! @relationship(type: "has_questionnaire", direction: OUT)
}

type Questionnaire {
    User: [User!]! @relationship(type: "has_questionnaire", direction: IN)
    id: ID!
    ClosedQuestions: [Answer!]! @relationship(type: "has_answered", direction: OUT)
    OpenQuestions: [BigInt]
}

type Answer {
    Questionnaire: Questionnaire! @relationship(type: "has_answered", direction: IN)
    id: ID!
    component: String!
    scope: String!
    answer: String!
    answered_at: Date!
}
  1. How would I write my mutation if I want to update the OpenQuestions list from a specific Questionnaire (id) from a specific user (username). My attempt was the following:
mutation {
    updateUsers(
        where: {
            username: „Kilian“
            id: „Project 1“      ##This id refers to the Questionnaire id
        }
        update: {
            OpenQuestions: [2, 3]
        }
    ) {
        users {
            Questionnaire {
                OpenQuestions
            }
        }
    }
}

/\ But this does not work...

  1. One step further/deeper: How would I write my mutation if I want to update a specific answer (id) within a specific questionnaire from a specific user?

Many thanks for your help! ✌️

1 Answers1

1

To update nested properties, you need to add the updated data and filtering on those properties within the where input

To achieve what you want, you can run a query similar to this:

mutation {
  updateUsers(
    where: {
      username: "Kilian"
    }
    update: {
      Questionnaire: {
        where: { node: { id: "Project 1" } }
        update: { node: { OpenQuestions: [2, 3] } }
      }
    }
  ) {
    users {
      Questionnaire {
        OpenQuestions
      }
    }
  }
}

In the query above, we are updating only users with username Kilian. Of those users, we are updating the relationship Questionnaire. From these related nodes, we are filtering only those with id Project 1. From these filtered nodes we update the OpenQuestions property.

Note that we need to add node to nested where and update. This is because we can update the relationship properties (if there were any) using edge.

To update a specific answer, you can follow the same pattern inside the nested update something like:

...
update: { node: { 
     OpenQuestions: [2, 3]
     ClosedQuestions: {
       where: { 
          id: "answerId"
        }
       update: { node: { answer: "my updated answer" } }
  } 
}
...
angrykoala
  • 3,774
  • 6
  • 30
  • 55
  • Thanks a lot for your answer! It looks very correct but it still throws an error. So there is no error when compiling, but after I send the request in my graphql sandbox I get the following error: "There is no procedure with the name `apoc.do.when` registered for this database instance. Please ensure you've spelled the procedure name correctly and that the procedure is properly deployed." -- Any ideas? I will mark your answer as correct/closed soon. :) – Kilian Kramer Jul 28 '22 at 11:08
  • 1
    To use @neo4j/graphql you need to have apoc enabled in your database https://neo4j.com/labs/apoc/4.1/installation/#:~:text=APOC%20Full%20can%20be%20installed,see%20the%20%22Installed%22%20message. Please make sure it is installed and check if the error persists – angrykoala Jul 28 '22 at 11:42
  • 1
    Many thanks. It works now perfectly! For others: You can run a neo4j instance and need the apoc plugin installed - via docker just type in the terminal: "docker run \ -p 7474:7474 -p 7687:7687 \ -v $PWD/data:/data -v $PWD/plugins:/plugins \ --name neo4j-apoc \ -e NEO4J_apoc_export_file_enabled=true \ -e NEO4J_apoc_import_file_enabled=true \ -e NEO4J_apoc_import_file_use__neo4j__config=true \ -e NEO4JLABS_PLUGINS=\[\"apoc\"\] \ neo4j:4.0" See https://github.com/neo4j-contrib/neo4j-apoc-procedures or (link in comment before) for more information. – Kilian Kramer Jul 28 '22 at 13:17