folkloreinc / laravel-graphql

Facebook GraphQL for Laravel 5. It supports Relay, eloquent models, validation and GraphiQL.
1.76k stars 233 forks source link

How to cache query result together with relations #383

Open drmax24 opened 6 years ago

drmax24 commented 6 years ago

It is not clear for me how can I cache output result of a complex query. Let's say I query car accessories together with related cars and other related stuff:

    public function resolve($root, $args, $context, $info)
    {
...
 $result = Accessories
                ::where($where)
                ->get();

        if (Cache::store('redis')->has('all_accessories')) {
            $result = Cache::store('redis')->get('all_accessories');
        } else {
            Cache::store('redis')->put('all_accessories', $result, 10080);
        }

return $result;
    }

If I cache it like this then all relations won't be cached. Because the result does not contain any of them at the moment of caching. How should i cache it?

ikudosi commented 6 years ago

In my application I am utilizing https://github.com/GeneaLabs/laravel-model-caching/. It covers majority of my caching needs. So far the only downside (and it's been noted) is that it currently does not support belongsToMany caching when being lazy-loaded.