|
|
|
|
|
by exogen
3159 days ago
|
|
One thing I keep running into is that for a few of my app ideas, I'm not really interested in just having static queries known at build time, like Relay and Apollo are designed for. I specifically want them to be dynamic and actually based on what components get rendered. Consider this query from the article: query ProfilePageData {
user(handle: "manifoldco") {
...HeaderData
...SidebarData
...TweetListData
}
}
That's great if the ProfilePage component knows that it only contains the Header, Sidebar, and TweetList components. Since they're designed for static queries, both Relay and Apollo suffer from this shortcoming: the parent query needs to know about every component that could possibly add fragments ahead of time, and pull them all in. In my opinion, this really fails at fulfilling the promise of components – the parent shouldn't need to know what all its descendants do.What if the rendered components are more dynamic? Putting @skip directives on every field is not really an option (and then you need a matching query $variable for every directive.) If you think of the query as more of a template, then you could have portions of the query that are like "template blocks" that other components could extend, e.g.: query ProfilePageData {
user(handle: "manifoldco") {
${userFragments}
}
}
...then descendant components could have access to `userFragments` as an extension point. I'm not yet sure if it's a terrible idea, but I started a project just the other day to experiment with it: https://github.com/exogen/apollo-dynamic-queries |
|
That way the parent can just render whatever components it wants without having to worry about what data they require.