How to filter GraphQL results with Controllers?

I’m working on a project using Strapi v5, and I’ve noticed some odd behavior around GraphQL queries and controllers.

In Strapi v4, there was an issue where GraphQL queries didn’t trigger controller actions, unlike the REST API. I found a thread from December 2021 they mention GraphQL queries bypass controller logic: Are GraphQL queries supposed to trigger controller actions in Strapi v4?

I’m wondering if this behavior has changed in v5.

I’ve found that REST requests trigger the expected controller actions, but for GraphQL, the parameters passed to the controllers are different
So instead of controller, I implemented middleware that intercepts GraphQL requests and processes them how I wanted to do in the controller:

  extensionService.use({
    resolversConfig: {
      "Query.posts": {
        middlewares: [
          async (resolve, parent, args, context, info) => {
            //if user is not authenticated
            if (!context.state.user) {
              /**
               * Update the selection set
               */
              // If user is not authenticated, remove sensitive fields
              info.fieldNodes[0].selectionSet.selections.forEach((field) => {
                if (field.name.value === "specialContent") {
                  field.selectionSet.selections =
                    field.selectionSet.selections.filter(
                      (subField) => subField.name.value !== "premium",
                    );
                }
              });

              /**
               * Update the graphql filters
               */
              // Force filter free: true when user is not authenticated
              args.filters = args.filters || {};
              args.filters.free = { eq: true };

              console.log(args)
            }

            return resolve(parent, args, context, info);
          },
        ],
      },

This topic has been created from a Discord post (1294928614550142976) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

that’s the problem: [v4] Overriding controller doesn't work in GraphQL · Issue #12256 · strapi/strapi · GitHub