documentation icon indicating copy to clipboard operation
documentation copied to clipboard

[Request]: customising graphQl

Open violabg opened this issue 4 years ago • 3 comments

Summary

in backend-customization/

https://docs.strapi.io/developer-docs/latest/development/backend-customization/controllers.html#adding-a-new-controller

you have this example:

// path: ./src/api/restaurant/controllers/restaurant.js

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

module.exports = createCoreController('api::restaurant.restaurant', ({ strapi }) =>  ({
  // Method 1: Creating an entirely custom action
  async exampleAction(ctx) {
    try {
      ctx.body = 'ok';
    } catch (err) {
      ctx.body = err;
    }
  },

  // Method 2: Wrapping a core action (leaves core logic in place)
  async find(ctx) {
    // some custom logic here
    ctx.query = { ...ctx.query, local: 'en' }
    
    // Calling the default core action
    const { data, meta } = await super.find(ctx);

    // some more custom logic
    meta.date = Date.now()

    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::restaurant.restaurant').findOne(id, query);
    const sanitizedEntity = await this.sanitizeOutput(entity, ctx);

    return this.transformResponse(sanitizedEntity);
  }
});

how to can we do the same for GraphQL? both adding a new method (exampleAction) and extending existing ones like find and findMany

Why is it needed?

I want to use GraphQL but can't figure it out, how to extend it. in particular I'm interested in create a method to return an entity via slug with graphQL, whit a new custom method like getEntityBySlug(slug) that returns a single entity, and not using find(filters)

Suggested solution(s)

No response

Related issue(s)/PR(s)

No response

violabg avatar Dec 01 '21 11:12 violabg

+1 I am constantly trying to ask about this on Discord and Strapi forum, but I'm always left with no response. In v3 it was pretty straightforward - graphQL queries and mutations were resolved using controllers by default, so all you had to do was to customize the controller.

kadlinobit avatar Dec 13 '21 20:12 kadlinobit

This issue has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/are-graphql-queries-supposed-to-trigger-controller-actions-in-strapi-v4/13474/3

derrickmehaffy avatar Dec 16 '21 10:12 derrickmehaffy

Thank you for opening this issue, and sorry for not replying earlier. The documentation team, unfortunately, didn't have time to focus on issues before now.

I'm adding @Convly to the conversation. As he worked on the GraphQL rewrite for Strapi v4, he might have some solutions for you that we could add to the documentation.

pwizla avatar Jun 21 '22 09:06 pwizla

is there any solution for this?

fernandomor avatar Jan 02 '23 16:01 fernandomor

Any updates on this?

ashbuilds avatar Feb 21 '23 19:02 ashbuilds

Hello. I'm sorry that you were having troubles and that this issue didn't get more attention sooner. I'm cleaning up old documentation issues and I will close this issue now, but it has been added to our internal backlog, and it will be worked on as soon as engineers can find some time to help the technical writers with it.

pwizla avatar Jul 10 '23 15:07 pwizla