Skip to content

[v4] Overriding controller doesn't work in GraphQL #12256

@teinett

Description

@teinett

Bug report

Describe the bug

I develop a blog with posts in Russian language.
It's nice to have correct looking quote marks, so I am using npm package Typograf to override quote marks in posts's controller.

Overriding controller works in REST API, but doesn't work in GraphQL.

It worked nice in Strapi v3, and recently in Strapi v4, but it doesn't work now.

Steps to reproduce the behavior

  1. Go to src/api/post/controllers/posts.js
  2. Add code to change quote marks (the code is below)
  3. Go to GraphQL and request the one post or posts
  4. See error: the quote marks are not changed.
  5. Go to REST API and request the one post or posts
  6. There is no error: quote marks are replaced.

Expected behavior

The quote marks should be replaced from " to « and ».
It's the way to understand that Typograf works.

Screenshots

GraphQL: the error

2022-01-20 13-43-53

REST API: no error

2022-01-20 13-50-35

The NextJS app with the request from GraphQL: the error

2022-01-20 13-52-34

Code snippets

My code of the post's controller:

'use strict';
// const { sanitizeEntity } = require('strapi-utils');
const Typograf = require('typograf');


/**
 *  post controller
 */

const { createCoreController } = require('@strapi/strapi').factories;

const tp = new Typograf({locale: ['ru', 'en-US']});

// module.exports = createCoreController('api::post.post');

module.exports = createCoreController('api::post.post', ({ strapi }) =>  ({

  async find(ctx) {
    // some custom logic here

    // Calling the default core action
    const { data, meta } = await super.find(ctx);

    // I don't know how to do the same as in findOne part, but it should be here

    return { data, meta };
  },

  // Method 3: Replacing a core action
  async findOne(ctx) {
    const { id } = ctx.params;
    const { query } = ctx;

    const entity = await strapi.service('api::post.post').findOne(id, query);
    // console.log(entity);

    if (entity) {
      if (entity.title) {entity.title = tp.execute(entity.title);}
      if (entity.subtitle) {entity.subtitle = tp.execute(entity.subtitle);}
      if (entity.description) {entity.description = tp.execute(entity.description);}
      if (entity.content) {entity.content = tp.execute(entity.content);}
    }
    const sanitizedEntity = await this.sanitizeOutput(entity, ctx);

    return this.transformResponse(sanitizedEntity);
  }

}));

System

  • Node.js version: 14.17.5
  • NPM version: 6.14.14
  • Strapi version: 4.0.5
  • Database: SQLite
  • Operating system: MacOS

Additional context

Overriding controller worked at 4.0.2 and probably 4.0.3, but it doesn't work at 4.0.4 and 4.0.5.

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue: bugIssue reporting a bugseverity: lowIf the issue only affects a very niche base of users and an easily implemented workaround can solvesource: plugin:graphqlSource is plugin/graphql packagestatus: pending reproductionWaiting for free time to reproduce the issue, or more information

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions