Introducing GraphQL Cache

Introducing GraphQL Cache
Average rating: 5
(1 votes)

Thanks! You’ve rated this material!

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.

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

    Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
    Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
    Kharkiv Otakara Yarosha 22, 61000, Kharkiv, Ukraine