Skip to content

[Request]: customising graphQl #535

@violabg

Description

@violabg

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

Metadata

Metadata

Labels

issue: added to backlogissue: doc requestIssues that require adding new content, possibly with some prior researchsource: Dev DocsPRs/issues targeting the Developer Docstarget: v4Documentation PRs/issues targeting content from docs.strapi.io (main branch).

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions