I have a simple Strapi-backed CMS with a post content type. This document (content type) has fields like title, secondary title, excerpt, body, etc. Now I want to be able to record view counts for each entry in this document.
So, I have added to it a number field called views with 0 as its default value. Following instructions in the Strapi documentation, I added the following code to my api model (at /api/post/models/Post.js
):
beforeFetchAll: async (model) => {
model.views += 1;
},
I understand any snippet inside of the beforeFetch
method ought to trigger and execute before any fetch
operation which is what the post
query does in GraphQL. However, despite several fetches, the value in this field stays unchanged. Any Strapi dev who could give an idea where I'm going wrong?
The full code can be found at https://github.com/amitschandillia/proost/blob/master/dev/api/post/models/Post.js.
P.S.: The query I'm attempting to execute in GraphQL Playground at https://dev.schandillia.com/graphql is:
{
posts(limit: 1, where: {slug: "one-post"}) {
id
title
views
}
}