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!
}
- 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...
- 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! ✌️
To update nested properties, you need to add the updated data and filtering on those properties within the
whereinputTo achieve what you want, you can run a query similar to this:
In the query above, we are updating only users with
usernameKilian. Of those users, we are updating the relationshipQuestionnaire. From these related nodes, we are filtering only those with idProject 1. From these filtered nodes we update theOpenQuestionsproperty.Note that we need to add
nodeto nestedwhereandupdate. This is because we can update the relationship properties (if there were any) usingedge.To update a specific answer, you can follow the same pattern inside the nested update something like: