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!
}
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...
Many thanks for your help! ✌️
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" } }
}
}
...