Skip to content

Commit

Permalink
Merge pull request #1735 from DimuthuMadushan/server-side-cache
Browse files Browse the repository at this point in the history
Introduce GraphQL Server-side Caching
  • Loading branch information
DimuthuMadushan authored Feb 12, 2024
2 parents 0d05087 + 188b858 commit 33c9bfd
Show file tree
Hide file tree
Showing 168 changed files with 3,837 additions and 96 deletions.
2 changes: 1 addition & 1 deletion ballerina-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
org = "ballerina"
name = "graphql_tests"
version = "1.10.1"
version = "1.11.0"

12 changes: 7 additions & 5 deletions ballerina-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "constraint"
version = "1.4.0"
version = "1.5.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -44,7 +44,7 @@ modules = [
[[package]]
org = "ballerina"
name = "crypto"
version = "2.5.0"
version = "2.6.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
Expand All @@ -67,9 +67,11 @@ modules = [
[[package]]
org = "ballerina"
name = "graphql"
version = "1.10.1"
version = "1.11.0"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "file"},
{org = "ballerina", name = "http"},
{org = "ballerina", name = "io"},
Expand All @@ -96,7 +98,7 @@ modules = [
[[package]]
org = "ballerina"
name = "graphql_tests"
version = "1.10.1"
version = "1.11.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "file"},
Expand All @@ -119,7 +121,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.5"
version = "2.10.6"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down
2 changes: 0 additions & 2 deletions ballerina-tests/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.apache.tools.ant.taskdefs.condition.Os

/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
Expand Down
7 changes: 7 additions & 0 deletions ballerina-tests/custom_prefix_without_import.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
// specific language governing permissions and limitations
// under the License.

import ballerina/graphql;

service /custom_prefix on graphqlListener {
@graphql:ResourceConfig {
cacheConfig: {
enabled: true
}
}
resource function get greeting() returns string {
return "Hello, world";
}
Expand Down
250 changes: 250 additions & 0 deletions ballerina-tests/tests/44_server_caches.bal

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions ballerina-tests/tests/batch_load_functions.bal
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,29 @@ isolated function faultyAuthorLoaderFunction(readonly & anydata[] ids) returns A
return validKeys.'map(key => authorTable.get(key));
}
};

isolated function authorLoaderFunction2(readonly & anydata[] ids) returns AuthorRow[]|error {
readonly & int[] keys = check ids.ensureType();
// Simulate query: SELECT * FROM authors WHERE id IN (...keys);
lock {
dispatchCountOfAuthorLoader += 1;
}
lock {
readonly & int[] validKeys = keys.'filter(key => authorTable2.hasKey(key)).cloneReadOnly();
return keys.length() != validKeys.length() ? error("Invalid keys found for authors")
: validKeys.'map(key => authorTable2.get(key));
}
};

isolated function bookLoaderFunction2(readonly & anydata[] ids) returns BookRow[][]|error {
final readonly & int[] keys = check ids.ensureType();
// Simulate query: SELECT * FROM books WHERE author IN (...keys);
lock {
dispatchCountOfBookLoader += 1;
}
return keys.'map(isolated function(readonly & int key) returns BookRow[] {
lock {
return bookTable2.'filter(book => book.author == key).toArray().clone();
}
});
};
2 changes: 1 addition & 1 deletion ballerina-tests/tests/interceptors.bal
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ readonly service class ServiceLevelInterceptor {
}

isolated function grantAccess(string fieldName) returns boolean {
string[] grantedFields = ["profile", "books", "setName", "person", "setAge", "customer", "newBooks"];
string[] grantedFields = ["profile", "books", "setName", "person", "setAge", "customer", "newBooks", "updatePerson", "person"];
if grantedFields.indexOf(fieldName) is int {
return true;
}
Expand Down
107 changes: 107 additions & 0 deletions ballerina-tests/tests/object_types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,110 @@ distinct service class NestedField {
],
@graphql:ID string[] j = ["id1"]) returns string? => ();
}

public type HumanService FriendService|EnemyService;

public isolated distinct service class FriendService {
private final string name;
private final int age;
private final boolean isMarried;

public isolated function init(string name, int age, boolean isMarried) {
self.name = name;
self.age = age;
self.isMarried = isMarried;
}

@graphql:ResourceConfig {
cacheConfig: {
maxAge: 180
}
}
isolated resource function get name() returns string {
return self.name;
}

@graphql:ResourceConfig {
cacheConfig: {
maxAge: 180
}
}
isolated resource function get age() returns int {
return self.age;
}

@graphql:ResourceConfig {
cacheConfig: {
maxAge: 180
}
}
isolated resource function get isMarried() returns boolean {
return self.isMarried;
}
}

public isolated distinct service class AssociateService {
private final string name;
private final string status;

public isolated function init(string name, string status) {
self.name = name;
self.status = status;
}

isolated resource function get name() returns string {
return self.name;
}

isolated resource function get status() returns string {
return self.status;
}
}

public isolated distinct service class EnemyService {
private final string name;
private final int age;
private final boolean isMarried;

public isolated function init(string name, int age, boolean isMarried) {
self.name = name;
self.age = age;
self.isMarried = isMarried;
}

isolated resource function get name() returns string {
return self.name;
}

isolated resource function get age() returns int {
return self.age;
}

isolated resource function get isMarried() returns boolean {
return self.isMarried;
}
}

public isolated distinct service class AuthorData2 {
private final readonly & AuthorRow author;

isolated function init(AuthorRow author) {
self.author = author.cloneReadOnly();
}

isolated resource function get name() returns string {
return self.author.name;
}

isolated function preBooks(graphql:Context ctx) {
dataloader:DataLoader bookLoader = ctx.getDataLoader(BOOK_LOADER_2);
bookLoader.add(self.author.id);
}

isolated resource function get books(graphql:Context ctx) returns BookData[]|error {
dataloader:DataLoader bookLoader = ctx.getDataLoader(BOOK_LOADER_2);
BookRow[] bookrows = check bookLoader.get(self.author.id);
return from BookRow bookRow in bookrows
select new BookData(bookRow);
}
}
30 changes: 30 additions & 0 deletions ballerina-tests/tests/records.bal
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,33 @@ type InputObject2 record {|
type InputObject3 record {|
int age = 30;
|};

type Friend record {|
readonly string name;
int age;
boolean isMarried;
|};

type Enemy record {|
readonly string name;
int age;
boolean isMarried;
|};

type EnemyInput record {|
readonly string name = "Enemy6";
int age = 12;
|};

type Associate record {|
readonly string name;
string status;
|};

public type Relationship FriendService|AssociateService;

type User record {|
int id?;
string name?;
int age?;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query A {
greet
name(id:1)
}

mutation B {
updateName(name: "John", enableEvict: false)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query A {
greet
name(id:1)
}

mutation B {
updateName(name: "John", enableEvict: true)
}

mutation C {
updateName(name: "Walter White", enableEvict: false)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query A {
getFriendServices {
name
age
}
}

mutation B {
updateFriend(name: "Tyler", age: 28, isMarried: true, enableEvict: true) {
name
age
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
query A {
authors(ids: [1, 2, 3]) {
name
books {
id
title
}
}
}

mutation B {
sabthar: updateAuthorName(id: 1, name: "Sabthar", enableEvict: true) {
name
books {
id
title
}
}
mahroof: updateAuthorName(id: 2, name: "Mahroof") {
name
books {
id
title
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
query A {
authors(ids: [1, 2, 3]) {
name
books {
id
title
}
}
}

mutation B {
harari: updateAuthorName(id: 1, name: "Yual Noah Harari", enableEvict: true) {
name
books {
id
title
}
}
manson: updateAuthorName(id: 2, name: "Mark Manson") {
name
books {
id
title
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query A {
enemy
}

mutation B {
updateEnemy(name: "Snape", enableEvict: true)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query A ($names:[String!]!){
isAllMarried(names: $names)
}

mutation B {
updateEnemy(enemy: {name: "Enemy3", age: 44}, enableEvict: true) {
name
isMarried
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query A {
enemies(isMarried: null) {
name
isMarried
}
}

mutation B {
removeEnemy(name: "Enemy2", enableEvict: false) {
name
isMarried
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query A {
getFriendService(name: "Jesse Pinkman") {
name
age
}
}

mutation B {
updateFriend(name: "Jesse Pinkman", age: 30, isMarried: false, enableEvict: true) {
name
age
}
}
Loading

0 comments on commit 33c9bfd

Please sign in to comment.