How to use Graphql fragment on multiple types with reusable component?

System Information
  • Strapi Version: 3.0.6
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

Hi everyone, I’ve got a graphql question :slight_smile:

I have different single types that are using a reusable component called BannerComponent which has multiple fields.

Ideally I don’t want to repeat the same query just for different types.

I tried fragment but it doesn’t work for different content types. (see example below)

Do you guys have a solution for that :pray:

query MyQuery {
  strapiHomepage {
   ...bannerFields
  }
  strapiAbout {
...bannerFields2
  }
}
fragment bannerFields on StrapiHomepage {
  BannerComponent {
caption
title
location
location_url
  }
}
fragment bannerFields2 on StrapiAbout {
  BannerComponent {
caption
title
location
location_url
  }
}

Hey @Azuri, currently trying to achieve the same thing. Have you found a solution to this?

Hello @lmartins unfortunately I didn’t find a better way.

If you do find a way please let me know :wink:

1 Like