DEV Community

Discussion on: A Better Way to use GraphQL Fragments in React

Collapse
 
andrewingram profile image
andrewingram

I'm glad you found this pattern. I'm not sure if you're aware or not, but you've stumbled upon what is arguably the way GraphQL was intended to be used (at least with React and other component-based frameworks). This is the way Relay promotes working with GraphQL, i.e. treating fragments as a first-class citizen. Whilst possible with Apollo (and other libraries like URQL), it gets a lot less attention because it's not the primary API. When GraphQL was first shown to the world in early 2015, it was in tandem with Relay, so the idea of having components be able to define their own data requirements was the big wow factor that got everyone excited. It's an unfortunate quirk of history that people aren't as aware of it these days, because it's such a great way of working.

Collapse
 
aleccool213 profile image
Alec Brunelle

Wow 😯 Thanks for the informative comment! That’s so neat that FB/Relay started off with implementing this pattern. I’ve been using it for a while now and wish it was integrated into Apollo more.

One thing, regardless of library, I wish the GraphQL spec supported are paramaterized fragments. That would make this pattern even more viable!

Collapse
 
andrewingram profile image
andrewingram

Yeah. Relay’s rewrite in 2017 added support for parameterised fragments, which makes marrying GraphQL with the component paradigm a lot more successful, but it does some client-side processing to support it. If it were supported everywhere it would be great.

I wouldn’t advocate a big rewrite in Relay because the learning curve is steep. But it’s worth checking out for its ideas.