GraphQL data modifications

System Information
  • Strapi Version: 4.20.1
  • Operating System: Windows 10
  • Database: mysql
  • Node Version: 18.18.2
  • NPM Version: 9.8.1
  • Yarn Version:

Hi together,

i would like to modify the strapis REST and GraphQL data by an own created javascript class. Both ways (REST and GraphQL) with the same class.
For the REST way it works (Modification is done inside a middleware). But for GraphQL it is not working at the moment, because the data structure is different (compared to REST).
And that is my problem. I need the same structure - otherwise i can’t use the same class/code for data modifications.

Here are a few example code snippets and data responses for better explanation.
I have following collection type and REST response:

Collection Type Bar
Relation: Bar belongs to many Bars (as displayed in strapi Content-Type Builder)

Example response (REST - find):

{
   "data":[
      {
         "id":1,
         "attributes":{
            "field1":"1",
            "field2":"2",
            "relationA":{
               "data":[
                  {
                     "id":3,
                     "attributes":{
                        "field1":"1",
                        "field2":"2",
                        "relationA":{
                           "data":[]
                        },
                        "relationB":{
                           "data":{
                              "id":1,
                              "attributes":{
                                 "field1":"1",
                                 "field2":"2"
                              }
                           }
                        }
                     }
                  }
               ]
            },
            "relationB":{
               "data":null
            }
         }
      }
	  ...
   ]
}

To add my response modifications to GraphQL too, i’ve used the extensionService inside src/index.ts file and created a custom resolver as shown below:

register({ strapi }) {
    const extensionService = strapi.plugin('graphql').service('extension');

    extensionService.use({
      typeDefs: `
        type Query {
          bars: BarEntityResponseCollection
        }
      `,
      resolvers: {
        Query: {
          bars: {
            async resolve(parent, args, context) {
              const data = await strapi.entityService.findMany('api::bar.bar', {
                ...args,
                populate: {
                  Relation_A: true,
                  Relation_B: true
                },
              });

              const { toEntityResponseCollection } = strapi.plugin('graphql').service('format').returnTypes;
              const formattedResponse = toEntityResponseCollection(data, {
                args,
                resourceUID: 'api::bar.bar'
              })
			  
              console.log('formattedResponse: ', formattedResponse);

              return formattedResponse;
            },
          },
        },
      },
    });
  }

My assumption was, that the logged ‘formattedResponse’ looks the same as above like the REST response, but that is not the case.
I don’t have ‘data’ objects/arrays and also no ‘attributes’. The formattedResponse is more flatten and looks different.
Example log for ‘formattedResponse’ below:

{
  nodes: [
    {
      id: 1,
      field1: '1',
      field2: '2',
      Relation_A: [Array],
      Relation_B: null
    },
    {
      id: 3,
      field1: '1',
      field2: '2',
      Relation_A: [],
      Relation_B: [Object]
    },
	...
  ],
  info: {
    args: { pagination: {}, sort: [], publicationState: 'live' },
    resourceUID: 'api::bar.bar'
  }
}

But in GraphQL playground (localhost:1337/graphql) i can see these missing data/attributes
Is there a way to get the same structure in GraphQL - before the data response reaches the client (e.g. inside a resolver) compared to the REST way?
Maybe by another transformation function like ‘toEntityResponseCollection’?

Can anybody help?

Thank you!