update
This is the documentation of the GraphQL Library version 6. For the long-term support (LTS) version 5, refer to GraphQL Library version 5 LTS. |
Consider the following type definitions:
type Post @node {
id: ID! @id
content: String!
creator: User! @relationship(type: "HAS_POST", direction: IN)
}
type User @node {
id: ID! @id
name: String
posts: [Post!]! @relationship(type: "HAS_POST", direction: OUT)
}
These update
mutations and response types are generated:
type UpdatePostsMutationResponse {
posts: [Post!]!
}
type UpdateUsersMutationResponse {
users: [User!]!
}
type Mutation {
updatePosts(
where: PostWhere
update: PostUpdateInput
): UpdatePostsMutationResponse!
updateUsers(
where: UserWhere
update: UserUpdateInput
): UpdateUsersMutationResponse!
}
The |
Single update
You can update the content of a Post
by executing the following GraphQL statement:
mutation {
updatePosts(
where: {
id_EQ: "892CC104-A228-4BB3-8640-6ADC9F2C2A5F"
}
update: {
content: "Some new content for this Post!"
}
) {
posts {
content
}
}
}
This updates the post by adding the sentence "Some new content for this Post!".
Nested create
using update
Instead of creating a Post
with the create
mutation and then connecting it to a User
, you can update a User
and create
a Post
as part of the mutation:
mutation {
updateUsers(
where: { name_EQ: "John Doe" }
update: {
posts: {
create: [
{ node: { content: "An interesting way of adding a new Post!" } }
]
}
}
) {
users {
id
name
posts {
content
}
}
}
}
connectOrCreate
relationships
Consider the example provided in the create
page:
mutation {
createActors(input: {
name: "Tom Hanks",
movies: {
connectOrCreate: {
where: { node: { id_EQ: "1234" } }
onCreate: { node: { title: "Forrest Gump" } }
}
}
}) {
info {
nodesCreated
}
}
}
connectOrCreate
is also available in update
operations:
mutation {
updateActors(
update: {
movies: {
connectOrCreate: {
where: { node: { id_EQ: "1234" } }
onCreate: { node: { title: "Forrest Gump" } }
}
}
},
where: { name_EQ: "Tom Hanks" }
) {
info {
nodesCreated
}
}
}
This operation is equivalent to the previous example.
Array methods
Array methods allow the modification of existing property arrays in update
mutations within these entities:
-
Node
-
Relationship properties
-
Interfaces
For that, the following operators are available:
-
_PUSH
-
_POP
_PUSH
_PUSH
conforms to the type of input defined in the type definition.
Consider the following type definitions, a Movie
with a property array of String
types called tags
:
type Movie @node {
title: String
tags: [String]
}
You can push tags to the tags
property array:
_PUSH
mutation {
updateMovies (update: { tags_PUSH: "another tag" }) {
movies {
title
tags
}
}
}
Before | After |
---|---|
|
|
Or push multiple elements in a single update:
_PUSH
mutation {
updateMovies (update: { tags_PUSH: ["another tag", "one more tag"] }) {
movies {
title
tags
}
}
}
Before | After |
---|---|
|
|
Similarly, you can have multiple array property fields and update them in the same query:
type Movie @node {
title: String
tags: [String]
moreTags: [String]
}
You can also push to both the tags
and moreTags
property arrays:
.Mutation with _PUSH
to two different arrays
mutation {
updateMovies (update: { tags_PUSH: "another tag", moreTags_PUSH: "a different tag" }) {
movies {
title
tags
moreTags
}
}
}
Before | After |
---|---|
|
|
_POP
_POP
expects a single Int
value as input.
Consider the following type definitions, a Movie
with a property array called tags
:
type Movie @node {
title: String
tags: [String]
}
You can pop from this tags
property array:
_POP
mutation {
updateMovies (update: { tags_POP: 1 }) {
movies {
title
tags
}
}
}
Before | After |
---|---|
|
|
Or, for more than one property from the array:
_POP
mutation {
updateMovies (update: { tags_POP: 2 }) {
movies {
title
tags
}
}
}
Before | After |
---|---|
|
|
Similarly, you can have multiple array property fields and update them in the same query:
type Movie @node {
title: String
tags: [String]
moreTags: [String]
}
Then, you can pop from both the tags
and moreTags
property arrays:
_POP
from two different arraysmutation {
updateMovies (update: { tags_POP: 1, moreTags_POP: 2 }) {
movies {
title
tags
moreTags
}
}
}
Before | After |
---|---|
|
|
Mathematical operators
You can use mathematical operators to update numerical fields based on their original values in a single DB transaction.
For that, specific operators are available on different numerical types: Int
, Float
and BigInt
.
They are supported within these entities:
-
Nodes
-
Relationship properties
-
Interfaces
For the Int
and BigInt
types, the following operators are available:
-
_INCREMENT
-
_DECREMENT
For the Float
type, the following operators are available:
-
_ADD
-
_SUBTRACT
-
_MULTIPLY
-
_DIVIDE
Operators remain available as optional fields. If a mathematical operator has been used in an undefined field, it causes a GraphQL error. |
For example, consider the following GraphQL schema for a social video platform:
type Video @node {
id: ID @id
views: Int
ownedBy: User @relationship(type: "OWN_VIDEO", properties: "OwnVideo", direction: IN)
}
type User @node {
id: ID @id
ownVideo: [Video!]! @relationship(type: "OWN_VIDEO", properties: "OwnVideo", direction: OUT)
}
type OwnVideo @relationshipProperties {
revenue: Float
}
Suppose a user watched a video in this platform, and you want to increment viewersCount
for that video by 1
.
Here is how you can do that:
mutation incrementViewCountMutation {
updateVideos(
where: { id_EQ: "VideoID" }
update: { views_INCREMENT: 1 }
) {
videos {
id
views
}
}
}
Now, suppose you want the social platform to reward the user with 0.01 dollars for watching the video.
To do that, you have to update the relationship property revenue
:
mutation addRevenueMutation {
updateUsers(
where: { id_EQ: "UserID" },
update: { ownVideo: [{ update: { edge: { revenue_ADD: 0.01 } } }] }
) {
users {
id
ownVideoConnection {
edges {
revenue
}
}
}
}
}