Skip to Content

@injectCurrentDateTime directive

Published: 2022-05-23

When building APIs, we often have to deal with dateTime to store information about when a record was created or updated. A lot of ORMs make it a standard to create createdAt and updatedAt fields on every table. You could also add deletedAt to implement soft deletes.

If you're already familiar with WunderGraph, you might know that we're supporting a rich set of Database Management Systems that you can directly connect to WG. Amongst them are PostgreSQL, MySQL, SQLite, SQLServer and Planetscale.

When dealing with date and time, we've realised that not all upstreams are capable of creating these values themselves, so we've had to come up with a solution where these fields can be handled in front of the database or API.

Our solution to the problem is the @injectCurrentDateTime directive. Attach it to a field and WunderGraph automatically injects the current dateTime into the variable.

Additionally, by attaching the directive to a variable, the variable is removed from the input variables the user is allowed to supply. So, the user is no longer allowed to supply the variable, it's completely server-side generated.

By default, the dateTime String is formatted using the ISO 8601 definition, this can be adjusted.

Let's look at an example:

mutation (
$email: String!
$name: String!
$id: String! @uuid
$createdAt: DateTime! @injectCurrentDateTime
)
{
createOneUser(data: {id: $id email: $email name: $name createdAt: $createdAt}){
id
name
email
createdAt
}
}

We might also want to be able to update the record later:

mutation (
$email: String!
$name: String!
$id: String! @uuid
$updatedAt: DateTime! @injectCurrentDateTime
)
{
updateOneUser(data: {id: $id email: $email name: $name updatedAt: $createdAt}){
id
name
email
createdAt
}
}

You might want to use a different format:

mutation (
$email: String!
$name: String!
$id: String! @uuid
$updatedAt: DateTime! @injectCurrentDateTime(format: UnixDate)
)
{
updateOneUser(data: {id: $id email: $email name: $name updatedAt: $createdAt}){
id
name
email
createdAt
}
}

Here's a list of all available formats:

enum DateTimeFormat {
"""2006-01-02T15:04:05-0700"""
ISO8601
"""Mon Jan _2 15:04:05 2006"""
ANSIC
"""Mon Jan _2 15:04:05 MST 2006"""
UnixDate
"""Mon Jan 02 15:04:05 -0700 2006"""
RubyDate
"""02 Jan 06 15:04 MST"""
RFC822
"""02 Jan 06 15:04 -0700"""
RFC822Z
"""Monday, 02-Jan-06 15:04:05 MST"""
RFC850
"""Mon, 02 Jan 2006 15:04:05 MST"""
RFC1123
"""Mon, 02 Jan 2006 15:04:05 -0700"""
RFC1123Z
"""2006-01-02T15:04:05Z07:00"""
RFC3339
"""2006-01-02T15:04:05.999999999Z07:00"""
RFC3339Nano
"""3:04PM"""
Kitchen
"""Jan _2 15:04:05"""
Stamp
"""Jan _2 15:04:05.000"""
StampMilli
"""Jan _2 15:04:05.000000"""
StampMicro
"""Jan _2 15:04:05.000000000"""
StampNano
}

If your desired format is not on the list, you can also use a custom one:

mutation (
$email: String!
$name: String!
$id: String! @uuid
$updatedAt: DateTime! @injectCurrentDateTime(customFormat: "2006-01-02")
)
{
updateOneUser(data: {id: $id email: $email name: $name updatedAt: $createdAt}){
id
name
email
createdAt
}
}

Product

Comparisons

Subscribe to our newsletter!

Stay informed when great things happen! Get the latest news about APIs, GraphQL and more straight into your mailbox.

© 2022 WunderGraph