Increment and decrement field operators

Yoav Lavi
Yoav Lavi
Jamie Barton
Jamie Barton
Increment and decrement field operators

We just released atomic increment and decrement operations for numeric fields!

Consider the following model for CartItem that has the Int field quantity:

type CartItem @model {
  name: String!
  quantity: Int! @default(value: 0)
}

The input type has been updated to use the new [Int|Float]OperationsInput input type.

input CartItemUpdateInput {
  quantity: IntOperationsInput
  # ...
}

The IntOperationsInput is used because the field is a Int.

This new input type contains 3 new fields to set, increment, or decrement the field value:

input IntOperationsInput {
  set: Int
  increment: Int
  decrement: Int
}

This means you can now pass the value you want to increment or decrement by to the field in the GraphQL mutation:

mutation {
  cartItemUpdate(by: { id: "..." }, input: { quantity: { increment: 1 } }) {
    cart {
      items(first: 50) {
        edges {
          node {
            quantity
          }
        }
      }
    }
  }
}

Previously users would have to calculate the new numeric value and pass it directly which often resulted with incorrect field values. We now calculate the new value before insert to make sure your backend is always using the latest value to increment or decrement.

We also made some additional updates in this release.

The id field for models is no longer required for any @model type in the schema.

type CartItem @model {
  name: String!
  quantity: Int! @default(value: 0)
}
  • Fixes incorrect JSON scalar serialization
  • The edge cursor value is now base64 encoded.
  • The default ordering of paginated collections are now sorted oldest first.
  • Grafbase CLI 0.14.1 has been released to include all of the above.

We'd love to hear your feedback and ideas, so join us on Discord.