5

I'm rewriting strapi v3 queries to v4 (GraphQL), and with new fields 'data' and 'attributes', I have a problem going too much deep into nested objects, an example of how data looks:

user {
  data {
    id
    attributes {
      company {
        data {
          id
          attributes {
            location {
              data {
                id
                ...
              }
          }
      }
    
    }
  }
}

Am I missing something in the documentation, is there a way to avoid this much nesting, should I restructure data in Model, or?

Marko B.
  • 535
  • 3
  • 8
  • 18

6 Answers6

1

This is a known pain point for many, but no immediate official solution in sight. Gotta deal with it.

https://forum.strapi.io/t/discussion-regarding-the-complex-response-structure-for-rest-graphql-developer-experience/13400/35

T1000
  • 918
  • 2
  • 15
  • 30
  • Anyone have a good work around? – Jake Jun 23 '22 at 03:55
  • 1
    So far my best solution is to just try to break up stuff in many components and flatten the data when its passed into the component. Ex. ``` {records.data.map(({ id, attributes }) => ( ))} ``` – Jake Jun 23 '22 at 04:11
1

My friend, that's the way now:

user.data.attributes.company.data.attributes.location.data 
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Nsamba Isaac
  • 357
  • 4
  • 14
0

For solve this you can install the strapi-plugin-transformer

And pass the needed configurations to remove data and attributes from the response.

Dave136
  • 36
  • 1
0

You can simplify the deeply nested structures in your Strapi v4 GraphQL queries using the strapi-flatten-graphql library (I am the author). This lightweight TypeScript library provides functions to flatten the nested GraphQL responses, making them more concise and easier to work with.

To get started, install the library using npm:

npm install strapi-flatten-graphql

Import the necessary functions from the library:

import { flattenEntityResponse } from 'strapi-flatten-graphql';

Then, use the flattenEntityResponse function to flatten your GraphQL responses:

const response = /* Your GraphQL response object */;
const flattenedResponse = flattenEntityResponse(response);

// Use the flattened response in your application
console.log(flattenedResponse);

The flattenEntityResponse function handles the transformation of the nested structure into a simpler, flattened format. It also includes TypeScript typings to ensure type safety while working with the flattened data.

By using strapi-flatten-graphql, you can streamline your Strapi v4 GraphQL queries and improve the readability and maintainability of the code.

ciekawy
  • 2,238
  • 22
  • 35
0

There is a new package to resolve this issue strapi-flatten-graphql

This will eliminate the data and attributes

Please check the package documentation for more details

Mridul
  • 31
  • 5
0

Try to use graphql

git clone https://github.com/GavinXue/strapi-study-cases.git

copy folder graphql from plugin on your project inside plugin also extension/graphql and set configuracion on config/plugins also on index

enter image description here