Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL Cache is Caching Fields Separately for Record Types with No Optional Values #6652

Closed
ThisaruGuruge opened this issue Jun 24, 2024 · 0 comments · Fixed by ballerina-platform/module-ballerina-graphql#1938
Assignees
Labels
module/graphql Issues related to Ballerina GraphQL module Team/PCM Protocol connector packages related issues Type/Improvement

Comments

@ThisaruGuruge
Copy link
Member

Description:
Consider the following GraphQL service:

import ballerina/graphql;
import ballerina/io;

@graphql:ServiceConfig {
    cacheConfig: {
        maxSize: 5
    },
    graphiql: {
        enabled: true
    }
}
service on new graphql:Listener(9090) {
    resource function get profiles() returns Profile[]|error {
        io:println("Fetching profiles from the database...");
        return [
            {name: "John", age: 30, contact: "0123456789"},
            {name: "Doe", age: 25, contact: "9876543210"},
            {name: "Jane", age: 35, contact: "1234567890"}
        ];
    }
}

type Profile record {|
    string name;
    int age;
    string contact;
|};

In the above example, the cache size is set to 5. But this will execute the resource function each time a request arrives because the cache size is exhausted. The reason is that each record field is cached as a separate cache entry, so there will be a total of 10 cache entries (3*3 fields + top-level resource value). Since the cache limit is 5, caches will be evicted and the subsequent responses have to be fetched again.

This behaviour can be improved only when the GraphQL resource is returning a record type with no optional fields. For other cases such as record types with optional fields or service types, there's no way to cache at the top level and not cache the sub-fields since the different requests will populate different fields which might cause some field values to become null in the cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/graphql Issues related to Ballerina GraphQL module Team/PCM Protocol connector packages related issues Type/Improvement
Projects
Archived in project
1 participant