Using field policies to rewrite dates
Next, let's display the dates of both the post and comments in a human-readable format. We will do this in the time ago format, which is commonly used in social network apps:
- Install the following package in your client's project:
npm install timeago.js
- Open the
src/app/cache.ts
file and import the library:import * as timeago from 'timeago.js';
- Add two field policies for the
Post.createdAt
andComment.createdAt
fields. Let's start with thePost
type:Post: { fields: { createdAt: { read(createdAt) { return timeago.format(createdAt) } } ...