Sitemap
Andrej Ocenas

Andrej Ocenas

Press enter or click to view image in full size
“timelapse photo of total lunar eclipse” by Jake Hills on Unsplash

Working with Apollo

4 min readOct 26, 2018

--

In my current work we decided to try GraphQL to speed up our development a bit. We were sold by the good people of the internet on productivity benefits and ease of use compared to RESTish APIs.

After some research, Apollo was picked as library to use both server side and client side and we went on to try rewriting one feature to test the waters. There is lots of tutorials, explanations and enumerations of benefits of either Apollo or GraphQL in general. This will be more critical and will try to describe some issues I encountered while implementing our proof of concept.

Even though this will probably sound more critical and negative, that is only because decided not to repeat the good parts here only the bad ones. Despite the flaws we still plan to invest more time into Apollo and use it for other features.

Also some of the issues are real bugs that can be fixed by the time you read this so always check the current state of issues on Github.

Server side performance

This is a general thing with GraphQL and how you write resolvers and not specific to Apollo but I thought I would mention it.

In naive implementation, you create a resolver for each node in a query graph which can lead to N+1 queries as each item in a child node is queried separately. For example querying Users and then Articles of each user would mean doing an Articles query for each user.

This can be mitigated by dataloader (which you probably want to use by default) but the way it works, you will still do one query for Users and one for Articles.

Get Andrej Ocenas’s stories in your inbox

Join Medium for free to get updates from this writer.

99% of time this is fine, GraphQL actually can help with performance because you usually need less http requests from the client but if you have some super optimised queries, you could have hard time porting them to your resolvers.

Bugs

For a library where lots of components are over 1.0.0 version there seems to be quite a few bugs that I would not expect. This is an open thread listing bugs for a networkPolicy option when doing queries. Most are fixed but for example setting networkPolicy to network-only still renders from cache first in the same way as cache-and-network is behaving. This is still open issue in a 2.4.3 version of the apollo-client.

So before using Apollo I would recommend checking the current status of open issues. We do not use features like subscriptions or manual cache updates at this moment so I am not sure how stable those features are. We were able to work around all issues for now but we will reevaluate if we find more bugs or if they become blocking.

React API

For usage with React you can use both higher order component functions or a component with a render prop. I am not a big fan of render prop component in these kind of usages. I rarely need to change the props of the data loading component due to render and if you need to nest multiple mutations it can get ugly pretty fast.

<Mutation ...>
{mutate1 =>
<Mutation ...>
{mutate2 => ...
}
</Mutation>
}
</Mutation>

You can use some helpers to compose the components but at that point it seems like HOC is better way to do that. One problem with that is that mutation HOC does not pass mutation result as a prop and so you can get result only by promise chaining on the provided mutate function. This means your component now needs to be stateful and you need to check mounted state of your component which makes for more error prone code.

It is trivial to create a custom HOC and fix this but it can surprise you when you first try to use it.

Documentation

Apollo documentation is not the worst I have seen but could be improved. For example some parameters are explained only in an API reference for the HOC, while there is no link form other parts where the same param is used. There are some omissions mentioned in the Github issues which are long outstanding. As a result I had to go into source code on multiple occasions.

Again this is not such a big issue but for how mature Apollo looks from the outside, it was surprising.

As I mentioned we will still continue to use Apollo despite the flaws but my initial enthusiasm was curbed because of them. As with any kind of technology you should always look for stability and maturity of a software. I feel like Apollo looks much more mature on the outside than it actually is so keep that in mind when considering it.

--

--