I think this is still a problem, I was trying to modify the controller to filter results from graphQL queries - for e.g. hide a premium field if the user is not signed in.
My solution for GraphQL was modifying the query in the middleware, so instead of filtering the results in the controller like REST, I force the query to change:
const extensionService = strapi.plugin("graphql").service("extension");
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);
},
],
},