strapi-plugin-navigation icon indicating copy to clipboard operation
strapi-plugin-navigation copied to clipboard

Strapi v5: A way of fetching all navigations with GraphQL

Open michsko544 opened this issue 1 year ago • 2 comments

I would like to ask if it is currently possible to fetch all navigations at once using GraphQL?

If not, I would like to request this feature, as it would be very useful in my use case.

Use Case: In my page layout, I need to fetch multiple menus, such as the main navigation menu and the footer menus. Instead of fetching each menu individually, I would prefer to fetch all menus in a single GraphQL query and then utilize them in the appropriate sections by using their slugs.

This would simplify the data-fetching process and improve efficiency.

Thank you for considering this request!

michsko544 avatar Oct 17 '24 12:10 michsko544

GQL does not support it yet.

Would you like to fetch all with nested items ? Or just flat structure to preprocess on the FE?

cyp3rius avatar Oct 17 '24 13:10 cyp3rius

It would be great to handle both cases, e.g. via the type parameter. In my case, I use a flat structure for the mobile version and a tree structure for the desktop version

If I had to choose one, I would prefer to receive an API response with nested elements.

michsko544 avatar Oct 17 '24 13:10 michsko544

@michsko544

in graphql you can do that natively, maybe this can fit to you

#import ./fragments/NavigationItem.gql

query GetNavigation($locale: I18NLocaleCode) {
  footer: renderNavigation(locale: $locale, navigationIdOrSlug: "footer-navigation", type: TREE) {
    ...NavigationItem
  }
  navigation: renderNavigation(locale: $locale, navigationIdOrSlug: "main-navigation", type: TREE) {
    ...NavigationItem
  }
  mobile: renderNavigation(locale: $locale, navigationIdOrSlug: "mobile-navigation", type: TREE) {
    ...NavigationItem
  }
}

liqueflies avatar Dec 03 '24 09:12 liqueflies

@liqueflies That's amazing! I didn't know you could use a similar query multiple times and assign it to a variable. Thank you for the tip! @cyp3rius Perhaps we can go ahead and close this issue now?

michsko544 avatar Jan 02 '25 15:01 michsko544

I think so ;)

cyp3rius avatar Jan 02 '25 15:01 cyp3rius