Counting with GraphQL Permissions Issue

System Information
  • 3.4.1:
  • MacOS 11.1:
  • GraphQL:

Hi all,

I’m following the documentation here to count using GraphQL, and it works fine for content types whose title is a single word (e.g. restaurant). But for content types with more than one word (e.g. ‘Blog Posts’ or ‘Blog Comments’, I get a permissions issue (despite having enabled ‘count’ for public users for these content types in Strapi).

My schema.graphql.js file:

`module.exports = {

definition: ,
query: commentsCount(where: JSON): Int! ,
resolver: {
Query: {
commentsCount: {
description: ‘Return the count of blog comments’,
resolverOf: ‘application::BlogComments.BlogComments.count’,
resolver: async (obj, options, ctx) => {
return await strapi.api.BlogComments.services.BlogComments.count(options.where || {});
},
},
},
},
};`

The error I get is as follows:

{
“errors”: [
{
“message”: “Forbidden”,
“locations”: [
{
“line”: 4,
“column”: 3
}
],
“path”: [
“commentsCount”
],
“extensions”: {
“code”: “INTERNAL_SERVER_ERROR”,
“exception”: {
“data”: null,
“isBoom”: true,
“isServer”: false,
“output”: {
“statusCode”: 403,
“payload”: {
“statusCode”: 403,
“error”: “Forbidden”,
“message”: “Forbidden”
},
“headers”: {}
},
“message”: “Forbidden”,
“stacktrace”: [
“Error: Forbidden”,
" at handleErrors (/Users/pftodd/Documents/Hercules/pt/backend/node_modules/strapi-plugin-users-permissions/config/policies/permissions.js:92:28)",
" at module.exports (/Users/pftodd/Documents/Hercules/pt/backend/node_modules/strapi-plugin-users-permissions/config/policies/permissions.js:79:12)",
" at processTicksAndRejections (internal/process/task_queues.js:97:5)",
" at async /Users/pftodd/Documents/Hercules/pt/backend/node_modules/strapi-plugin-graphql/node_modules/strapi-utils/lib/policy.js:68:5",
" at async /Users/pftodd/Documents/Hercules/pt/backend/node_modules/strapi-plugin-graphql/services/resolvers-builder.js:97:7"
]
}
}
}
],
“data”: null
}

I’d be grateful if you could let me know why this isn’t working!

Many thanks

Patrick

1 Like

I also encountered this problem. In the end, I found that the variable name should be added with s, some need to be added, and some can’t be added

You can try it

resolverOf: ‘application::BlogComment.BlogComments.count’,

Notice that I only removed the s from the first blogcomment

In my case, I used the wrong format of the API ID.
resolverOf value should contain exact API, e.g. ID is news-item => resolverOf: 'application::news-item.news-item.count',
And so is in the resolver,

resolver: async (obj, options) => {
    return await strapi.api['news-item'].services['news-item'].count(options.where || {});
},

You can use file structure for reference, where each chain in the call is a folder name.

api 
 - news-item
   - services
     - news-item.js