Query sever timestamps in GraphQL?

Is it possible to add and query server-generated read-only timestamps like “createdAt” and “updatedAt” in a graph graphql schema? If so, how? I looked through all the docs but could not see any mention of this.

Thanks!

Looks like you want to auto fill values for DateTime fields in a GraphQL type. For example:

type User {
  id: ID!
  name: String
  createdAt: DateTime @default(expr: $now)
}

Then when you want to add a User, the createdAt field should get automatically filled by the server with the current time on server.

We don’t have this kind of functionality at the moment. But, we are planning to add it later in the future. Not sure when we will be able to release it.

Thanks for the info! I’lll admit I am a bit surprised you don’t already have a mechanism for server generated timestamps and they are quite critical for many applications. With that said, I look forward to seeing them implemented. You may also want to look at adding directives like used here: https://github.com/genie-team/graphql-genie/blob/master/docs/sdl.md (search down for createdTimestamp and @updatedTimestamp), as I’m not sure from your example how updatedAt could be handled with $now.

Thanks for reporting this. It looks like an interesting and necessary feature. We have added this to our roadmap.

1 Like

Is it safe to assume that the way to do this in the mean time is with custom mutations?

Dgraph generates an addPost mutation from those types, but we want to do something extra. We don’t want the author to come in with the mutation, that should get filled in from the JWT of the logged in user. Also, the datePublished shouldn’t be in the input; it should be set as the current time at point of mutation. Maybe we also have some community guidelines about what might constitute an offensive title or text in a post. Maybe users can only post if they have enough community credit.

We’ll need custom code to do all that, so we can write a custom function that takes in only the title and text of the new post. Internally, it can check that the title and text satisfy the guidelines and that this user has enough credit to make a post. If those checks pass, it then builds a full post object by adding the current time as the datePublished and adding the author from the JWT information it gets from the forward header. It can then call the addPost mutation constructed by Dgraph to add the post into Dgraph and returns the resulting post as its GraphQL output.

Thanks! Looking forward to seeing server generated timestamps implemented.

Yeah! you could surely do it that way for now.

1 Like