Skip to content

[graphql] controller bypassed when querying nested relations #8735

@8byr0

Description

@8byr0

Bug report

Describe the bug

When using custom processing in a model controller to filter nested relations, it's bypassed by graphql plugin.

Steps to reproduce the behavior

  1. Create a model B
  2. Create a model A with a field entities (one to many : B)
  3. Edit api/A/controllers/a.js and add a custom findOne controller (but same behaviour with all controllers) :
module.exports = {
    lifecycles: {
        async findOne(ctx) {
           const data = // query data
           data.entities = [];
           return data;
        }
    }
}
  1. query you model via graphql
query MyModelA {
  projectState(id:"azertyuiop"){
        id
  	entities {
          id
        }
    }
}

Expected behavior

The data returned by graphql plugin should be the one returned by controller:

{
  "data": {
    "a": {
      "id": "azertyuiop",
      "entities": []
    }
  }
}

Instead we receive this:

{
  "data": {
    "a": {
      "id": "azertyuiop",
      "entities": [
        {
          "id": "abcdefgh",
        },
        {
          "id": "ijklmnop",
        },
      ]
    }
  }
}

Screenshots

N/A

Code snippets

If applicable, add code samples to help explain your problem.

System

  • Node.js version: 10.19.0
  • YARN version: 1.22.5
  • Strapi version: 3.3.3
  • Database: Mongo DB
  • Operating system: Ubuntu 20.04

Additional context

After investigating a bit, it seems that graphql plugin queries manually nested relations without using controller for each queried relation:

    // strapi-plugin-graphql/services/data-loader.js:159
    return strapi.plugins['content-manager'].services['contentmanager'].fetchAll(modelUID, params);

This results in a query to ORM model (mongoose, here) instead of controller.

I'm not really familiar with apollo server and the way it's been implemented here but wouldn't it be possible to query nested relations via their controller ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue: enhancementIssue suggesting an enhancement to an existing featureseverity: lowIf the issue only affects a very niche base of users and an easily implemented workaround can solvesource: plugin:graphqlSource is plugin/graphql packagestatus: confirmedConfirmed by a Strapi Team member or multiple community members

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions