neo4jgraphqlgraphql-jsgraphql-mutationneo4j-graphql-js

How to update relational types in Graphql


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! ✌️


Solution

  • 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" } }
      } 
    }
    ...