Mathematical operators

Mathematical operators are a handy way to update numerical fields based on their original values in a single DB transaction.

Specific operators are available on different numerical types (Int, Float, BigInt).

Mathematical operators are supported in Update Mutations within these entities:

  • Node

  • Nested Nodes

  • Relationship properties

  • Interfaces

Int and BigInt operators

For Int and BigInt types, the following operators are available:

  • _DECREMENT

  • _INCREMENT

Float operators

For Float type, the following operators are available:

  • _ADD

  • _SUBTRACT

  • _MULTIPLY

  • _DIVIDE

Examples

The social platform schema

In this section, we use the following GraphQL schema:

type Video {
  id: ID @id
  views: Int
  ownedBy: User @relationship(type: "OWN_VIDEO", properties: "OwnVideo", direction: IN)
}
type User {
  id: ID @id
  ownVideo: [Video!]! @relationship(type: "OWN_VIDEO", properties: "OwnVideo", direction: OUT)
}
interface OwnVideo @relationshipProperties {
  revenue: Float
}

Simple increment operation

Let’s say that a user views a video, so we want to increment viewersCount for that video by 1.

mutation incrementViewCountMutation {
  updateVideos(
    where: { id: "VideoID" }
    update: { views_INCREMENT: 1 }
  ) {
    videos {
      id
      views
    }
  }
}

Nested example

Now, let’s say that the video platform wants to reward the user with 0.01 dollars for viewing the video. To do that, we have to update the relationship property revenue.

mutation addRevenueMutation {
  updateUsers(
    where: { id: "UserID" },
    update: { ownVideo: [{ update: { edge: { revenue_ADD: 0.01 } } }] }
  ) {
    users {
      id
      ownVideoConnection {
        edges {
          revenue
        }
      }
    }
  }
}

Optional fields

Operators remain available for optional fields, this means that if a mathematical operator has been used in a field not defined then a GraphQL error is raised.