How to set up Varnish caching for GraphQL in Django

Here is my django-benchmark project in which I implemented simple REST API and GraphQL endpoints. In front of my application I put Varnish for caching. Caching works well for Rest HTTP endpoints and does not work for GraphQL here is my Varnish configuration. What am I doing wrong?

vcl 4.1;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "0.0.0.0";
    .port = "8080";
}

sub vcl_hash {
    # For multi site configurations to not cache each other's content
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }

    if (req.url ~ "/api/graphql") {
        call process_graphql_headers;
    }
}

sub process_graphql_headers {
    if (req.http.Store) {
        hash_data(req.http.Store);
    }
    if (req.http.Content-Currency) {
        hash_data(req.http.Content-Currency);
    }
}

sub vcl_recv {
    # Authenticated requests should not be cached by default
    if (req.http.Authorization ~ "^Bearer") {
        return (pass);
    }
}

sub vcl_backend_response {
}

sub vcl_deliver {
}


Read more here: https://stackoverflow.com/questions/66483164/how-to-set-up-varnish-caching-for-graphql-in-django

Content Attribution

This content was originally published by Lukasz Dynowski at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: