graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
708 stars 94 forks source link

How to implement caching properly? #353

Open Mokin711 opened 3 years ago

Mokin711 commented 3 years ago

I am currently using both the graphQL-compose-mongoose with apollo server express and traditional mongoose in my application. Graphql is mainly responsible for query and most of the updates happen through traditional mongoose operations such as create and update.

Does anyone have any good suggestions regarding how to implement caching properly? Thanks!

Mokin711 commented 3 years ago

I have been thinking about stickying redis into the mongoose exec function, will this work with this package? It is calling mongoose under the hood so technically it should work right? My only concern is what happens to the document when we are only querying some of the fields. Maybe the cache will only register those fields and another query to the same document with different fields will get returned the same data that does not match.

yoadsn commented 3 years ago

I am not sure how the Redis store/get logic is implemented on your implementation. But indeed since GQL would selectively get some fields depending on the query - DB level caching gets complex and the "cache key" should be aware of the projection. But not only - what about paging? Sorting? All of those can of course require a different "cache key".

It's not impossible, but we are not doing anything like that.

We are deploying the caching on the GQL operation level where the "key" is composed of the serialization+hashing of the entire query request: operation name, variable values, and even the query string itself. Then the results of the DB access (and other system access in some cases) are all stored in a cache before returning it to the client. Apollo server has already a mechanism to do that for you and an ability to implement advanced use cases with a plugin if you need to. See here: https://www.apollographql.com/docs/apollo-server/performance/caching/

Mokin711 commented 3 years ago

Thx for the response! I looked into the apollo server options. The first thing I was confused about is how do we define these field caches when the schema is generated by graphql compose instead of me manually writing them? And the redis option for apollo server seems to support the data sources, which I think is for the external API instead of mongoose? Maybe I read it the wrong way, feel free to correct me!

Mokin711 commented 3 years ago

My bad, it does support database as a data source, but I am confused about how will this work with this package? I meant didn't we converted mongoose into query and mutations as a schema?

Mokin711 commented 3 years ago

I am attempting to cache the full GQL response rn, which uses apollo-server-plugin-response-cache and apollo-server-cache-redis because this seems to be the most reasonable solution atm. Is this something closer to what you mentioned about GQL operation level @yoadsn ?