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?
6 Answers
Reset to default
1
This is a known pain point for many, but no immediate official solution in sight. Gotta deal with it.
2
-
Anyone have a good work around?
– JakeJun 23, 2022 at 3: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 }) => ( <Record key={id} {…{ …attributes, id }} /> ))} “`
– JakeJun 23, 2022 at 4:11
1
My friend, that’s the way now:
user.data.attributes.company.data.attributes.location.data
0
For solve this you can install the strapi-plugin-transformer
And pass the needed configurations to remove data
and attributes
from the response.
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.
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
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
Not the answer you're looking for? Browse other questions tagged
or ask your own question.
or ask your own question.
|