Hi Bertrand,

I'm mostly referring to server-side queries. Here are my thoughts:
1. In case we provide hooks for SlingDataFetchers we will end up with 
fine-grained cache hints (for eg 
https://github.com/graphql-java/graphql-java/blob/c40fc1d50e91f5584cd8995b46c464d3692f20b3/src/main/java/graphql/cachecontrol/CacheControl.java
 could be used). After getting these hints into the ExecutionResult we would 
need a mechanism that combines them into an overall cache policy for the 
response. But the major limitation is that this can be used only in case of GET 
requests. Now the downside in this is that many CDNs and caching proxies only 
cache GET requests (not POST requests) and may have a limit on the size of a 
GET URL. So if we hit the limit it won't work and unfortunately GraphQL queries 
can be very long. Also I think that building such a mechanism is complicated.

2. Now if we think of using POST requests that are not cached by CDN the only 
option is application level caching. And the extension point that can be added 
to sling graphql is a KeyValueCache interface that will work as a service and 
will be implemented by the client so that the client has control on the cache 
implementation as you said. If the key is an Object it's even better because it 
helps setting the scope of the cache to PRIVATE or PUBLIC. One client can use a 
combination of UserId + locale + query or sessionId + query or sessionId + 
excutionInput for the key. And for using the cache we could update 
https://github.com/apache/sling-org-apache-sling-graphql-core/blob/master/src/main/java/org/apache/sling/graphql/core/engine/GraphQLResourceQuery.java#L101
 to check the cache first and if not execute the query. But anyway in case of 
POST requests I don't see the need for caching hints.

Andreea

[0] https://www.apollographql.com/docs/apollo-server/performance/caching/

On 06/08/2020, 18:04, "Bertrand Delacretaz" <bdelacre...@apache.org> wrote:

    [Hi Andreea,
    
    On Thu, Aug 6, 2020 at 12:02 PM Andreea Miruna Moise
    <san...@adobe.com.invalid> wrote:
    > What would be the recommended way of approaching caching in case of 
GraphQL?...
    
    I haven't given much thought to that so far, and reading [1] [2] and
    [3] it looks like the best way to cache GraphQL query responses is to
    run those queries server-side, driven by GET requests and use
    traditional HTTP caching.
    
    The GraphQL core module does support server-side queries, without
    caching HTTP headers so far but that could be added.
    
    But I suppose you are more looking at client-driven GraphQL queries.
    [3] mentions an interesting (if a bit hacky) way of moving queries to
    the server-side to make them easier to cache. I suppose that only
    works if you control the client and that's probably not a standard.
    
    > ...would it make sense to have an application caching layer at Sling 
level that would support private
    > caching of the ExecutionResult?..
    
    What we might do is provide hooks in the GraphQL Core for
    SlingDataFetchers to supply caching hints, along with an extension
    point where that caching can happen - would that work for your use
    cases?
    
    If the answer is yes, suggestions on how those hooks can look are very 
welcome!
    
    Although we do have caching services in Sling [4] I'm not sure if they
    are in active use at the moment, their code doesn't seem to have been
    touched in a long time. But if we provide somewhat abstract hooks,
    people can use whatever caching mechanism they want.
    
    -Bertrand
    
    
    [1] 
https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.apollographql.com%2Fblog%2Fgraphql-caching-the-elephant-in-the-room-11a3df0c23ad%2F&amp;data=02%7C01%7Csandru%40adobe.com%7C10564da5b3a94cb5c2ac08d83a19ffa3%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637323230626053305&amp;sdata=Eh%2Ba8ZBlOrJEv%2BGe4MfmT0JmE3AauMEjfGAe8fV4HhM%3D&amp;reserved=0
    [2] 
https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.apollographql.com%2Fdocs%2Fapollo-server%2Fperformance%2Fcaching%2F%23adding-cache-hints-statically-in-your-schema&amp;data=02%7C01%7Csandru%40adobe.com%7C10564da5b3a94cb5c2ac08d83a19ffa3%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637323230626053305&amp;sdata=LKF9VSqnyXk3jcNqmvQYVd2u6rMWfgwvnrnxq3ZOXWw%3D&amp;reserved=0
    [3] 
https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fapollographql%2Fapollo-link-persisted-queries&amp;data=02%7C01%7Csandru%40adobe.com%7C10564da5b3a94cb5c2ac08d83a19ffa3%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637323230626053305&amp;sdata=BDqMOoCm3MeaontiJVsCf9oSZRhQu7m17seFeaYc%2Fcw%3D&amp;reserved=0
    [4] 
https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsling.apache.org%2Fdocumentation%2Fbundles%2Fcaching-services.html&amp;data=02%7C01%7Csandru%40adobe.com%7C10564da5b3a94cb5c2ac08d83a19ffa3%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C0%7C637323230626053305&amp;sdata=hq9GDy%2FYWFbidPxB%2FhD1UIBsGJ1Ya1AY969jc1KkFQ4%3D&amp;reserved=0
    

Reply via email to