graphqlthegraph

Showing subgraph timestamp results in human-readable format


My subgraph as a UNIX timestamp as seconds defined as the following

type TokenSwap @entity {
  id: ID!

  # UNIX timestamp of block in which the swap was created
  timestamp: BigInt!
}

When I am experimenting in TheGraph playground, is there a way for me to display this as UTC time, instead of raw seconds since the UNIX epoch? This would make the playground output more readable.


Solution

  • The single responsibility of Graphql is to fetch a defined query, it doesn't support formatting data so you won't be able to do that in the playground.

    It is possible to define a custom scalar type for a Date to control how it is serialized, deserialized, and validated source

    Unfortunately, a date type is not included in the scalar types supported by The Graph source

    One solution would be to add an additional field with the human readable timestamp for convenience.

    // schema 
    type TokenSwap @entity {
      id: ID!
    
      # ISO String of block in which the swap was created
      isoString: String!
    }
    
    // handler
    tokenswap.isoString = new Date(1665360258621).toISOString()