How to check past and future dates using Hypi Directives

In the previous post, we have seen how to insert only unique values to a field using @unique directive.
Now we will see how to check past and future dates using Hypi Directives.(@past and @future)

Again a brief info on Hypi Directives:

  • Hypi directives help to customise the behaviour 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.

Let’s check the @past and @future directives!
past: [email protected]
future: [email protected]

  • Use the @past directive to check if a date specified in a ‘DateTime’ field is a past date.
  • Use the @future directive to check if a date specified in a ‘DateTime’ field is a future date.
  • Both directives return an error if the inserted date is not within set limits.
  • The format accepted by the DateTime field has to be ISO 8601 standard – UTC date format.

We will work with the below schema.

type DirectiveObj {
  pastDate: DateTime @past
  futureDate: DateTime @future
} 

Sample Query

Let’s see how the @past and @future directives work.

mutation {
  upsert(
    values: {
      DirectiveObj: {
        pastDate: "2021-11-27T04:19:02Z"
        futureDate: "2020-03-12T04:19:02Z"
      }
    }
  ) {
    id
  }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01FNDXYJYMR0PMT6TJ8JCWSNAE"
      }
    ]
  },
  "errors": [
    {
      "message": "'2021-11-27T04:19:02Z' at 'DirectiveObj.pastDate' is required to be in the past but was '2021-11-27T04:19:02.000Z'",
      "extensions": {}
    },
    {
      "message": "'2020-03-12T04:19:02Z' at 'DirectiveObj.futureDate' is required to be in the future but was '2020-03-12T04:19:02.000Z'",
      "extensions": {}
    }
  ]
}

Check the POSTMAN collection for the @past/@future directive in different programming languages! Click </> and choose the programming language of your choice.
Don’t forget to insert your own Authorization key and Hypi Domain under Headers to test the results!

Run in Postman