Earlier we showed you some main problems you can face using GraphQL and some solutions to deal with them. Meanwhile, StackShare have been experimenting with GraphQL for some new features and encountered a couple of gaps in the Ruby GraphQL implementation… There was no appropriate caching solution! That was the reason to create an open source solution as graphql-cache – the new graphql-ruby gem! So today we’re introducing GraphQL Cache!
You can do implementing cache yourself with several lines of code and cache block, and key in each resolver to be cached. But this is a maintenance nightmare in terms of cache keys and is just plain ugly Ruby.
And from today you can avoid all of these by using just one graphql-ruby gem.
A custom middleware for graphql-ruby that handles key construction and cache reads/writes transparently was implemented. Now when we want to cache a field, all we have to do is add a keyword parameter to the field:
field :my_field, Int, cache: true
That’s all! The value is cached under a key that uniquely identifies this field for this object. Try it on GitHub.