update

This is the documentation of the GraphQL Library version 7. 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 id field cannot be updated as the @id directive has been used.

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: { set: "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
            }
        }
    }
}

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:

Mutation with a single push
mutation {
    updateMovies (update: { tags: { push: "another tag" } }) {
        movies {
            title
            tags
        }
    }
}
Before After

['some tag']

['some tag', 'another tag']

Or push multiple elements in a single update:

Mutation with two push
mutation {
    updateMovies (update: { tags: { push: ["another tag", "one more tag"] } }) {
        movies {
            title
            tags
        }
    }
}
Before After

['some tag']

['some tag', 'another tag', 'one more tag']

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
    tags: ['some tag']
    moreTags: []
    tags: ['some tag', 'another tag']
    moreTags ['a different tag']

_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:

Mutation with a single _POP
mutation {
    updateMovies (update: { tags: { pop: 1 } }) {
        movies {
            title
            tags
        }
    }
}
Before After
tags: ['a', 'b', 'c']
tags: ['a', 'b']

Or, for more than one property from the array:

Mutation with two _POP
mutation {
  updateMovies(update: { tags: { pop: 2 } }) {
    movies {
      title
      tags
    }
  }
}
Before After
tags: ['a', 'b', 'c']
tags: ['a']

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:

Mutation with _POP from two different arrays
mutation {
  updateMovies(update: { tags: { pop: 1 }, moreTags: { pop: 2 } }) {
    movies {
      title
      tags
      moreTags
    }
  }
}
Before After
tags: ['a', 'b', 'c']
moreTags: ['x', 'y', 'z']
    tags: ['a', 'b']
    moreTags: ['x']

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. Float and BigInt types, the following operators are available:

  • add

  • subtract

For the Float type these additional operators are also available:

  • 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: { add: 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 {
          properties {
            revenue
          }
        }
      }
    }
  }
}