How to create versions on the object level using @versioned directive

Let’s start looking at new topics unfolding new features implemented by Hypi!

In this post, we will see how to create versions on the object level using @versioned directive.

Before that, let’s throw some light on Hypi directives again…

  • Hypi directives help to customize the behavior of the data fields.
  • @ character depicts the Hypi directive. It is followed by a series of characters.
  • A directive may have a list of optional named arguments.
  • Disable directive by adding # in front of it.

@versioned directive tracks the version of each object.

  • The data type has to be declared using @versioned directive
type Object @versioned {
    fld: Int
}
  • It generates a version of each object created with a data type
mutation {
  upsert(values: { Object: { fld: 1 } }) {
    id
  }
}

## Retrieve Version of the object

{
  get(type: Object, id: "01gdjf5t13g57g9ssyna18spz7") {
    ... on Object {
      hypi {
        id
        version
      }
      fld
    }
  }
}

# 1st Version

{
  "data": {
    "get": {
      "hypi": {
        "id": "01gdjf5t13g57g9ssyna18spz7",
        "version": 890197337350217700
      },
      "fld": 1
    }
  }
}

# Update Same Object

mutation {
  upsert(
    values: { Object: { fld: 2, hypi: { id: "01gdjf5t13g57g9ssyna18spz7" } } }
  ) {
    id
  }
}

# 2nd Version

{
  "data": {
    "get": {
      "hypi": {
        "id": "01gdjf5t13g57g9ssyna18spz7",
        "version": 890197654485737500
      },
      "fld": 2
    }
  }
}
  • For a specific version number, create the object by providing the number in the version field of hypi object.
mutation {
  upsert(values: { Object: { fld: 3, hypi: { version: 3 } } }) {
    id
  }
}

In the next post, we will see how to roll back or retrieve the version changes.