-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DataLoader functionality (#1566)
- Loading branch information
1 parent
8c1e8a3
commit 05e981e
Showing
80 changed files
with
3,123 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright (c) 2023 WSO2 LLC. (http://www.wso2.com) All Rights Reserved. | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/graphql; | ||
import ballerina/websocket; | ||
import ballerina/test; | ||
|
||
@test:Config { | ||
groups: ["dataloader", "query"], | ||
after: resetDispatchCounters | ||
} | ||
isolated function testDataLoaderWithQuery() returns error? { | ||
graphql:Client graphqlClient = check new ("localhost:9090/dataloader"); | ||
string document = check getGraphqlDocumentFromFile("dataloader_with_query"); | ||
json response = check graphqlClient->execute(document); | ||
json expectedPayload = check getJsonContentFromFile("dataloader_with_query"); | ||
assertJsonValuesWithOrder(response, expectedPayload); | ||
assertDispatchCountForAuthorLoader(1); | ||
assertDispatchCountForBookLoader(1); | ||
} | ||
|
||
@test:Config { | ||
groups: ["dataloader", "query"], | ||
after: resetDispatchCounters | ||
} | ||
isolated function testDataLoaderWithDifferentAliasForSameField() returns error? { | ||
graphql:Client graphqlClient = check new ("localhost:9090/dataloader"); | ||
string document = check getGraphqlDocumentFromFile("dataloader_with_different_alias_for_same_field"); | ||
json response = check graphqlClient->execute(document); | ||
json expectedPayload = check getJsonContentFromFile("dataloader_with_different_alias_for_same_field"); | ||
assertJsonValuesWithOrder(response, expectedPayload); | ||
assertDispatchCountForAuthorLoader(1); | ||
assertDispatchCountForBookLoader(1); | ||
} | ||
|
||
@test:Config { | ||
groups: ["dataloader", "subscription"], | ||
after: resetDispatchCounters | ||
} | ||
isolated function testDataLoaderWithSubscription() returns error? { | ||
string document = check getGraphqlDocumentFromFile("dataloader_with_subscription"); | ||
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]}; | ||
websocket:Client wsClient = check new ("ws://localhost:9090/dataloader", config); | ||
check initiateGraphqlWsConnection(wsClient); | ||
check sendSubscriptionMessage(wsClient, document, "1"); | ||
json[] authorSequence = check getJsonContentFromFile("data_loader_with_subscription").ensureType(); | ||
foreach int i in 0 ..< 5 { | ||
json expectedMsgPayload = {data: {authors: authorSequence[i]}}; | ||
check validateNextMessage(wsClient, expectedMsgPayload, id = "1"); | ||
} | ||
assertDispatchCountForBookLoader(5); | ||
} | ||
|
||
@test:Config { | ||
groups: ["dataloader", "mutation"], | ||
dependsOn: [testDataLoaderWithQuery, testDataLoaderWithSubscription], | ||
after: resetDispatchCounters | ||
} | ||
isolated function testDataLoaderWithMutation() returns error? { | ||
graphql:Client graphqlClient = check new ("localhost:9090/dataloader"); | ||
string document = check getGraphqlDocumentFromFile("dataloader_with_mutation"); | ||
json response = check graphqlClient->execute(document); | ||
json expectedPayload = check getJsonContentFromFile("dataloader_with_mutation"); | ||
assertJsonValuesWithOrder(response, expectedPayload); | ||
assertDispatchCountForUpdateAuthorLoader(1); | ||
assertDispatchCountForBookLoader(1); | ||
} | ||
|
||
@test:Config { | ||
groups: ["dataloader", "interceptor"], | ||
after: resetDispatchCounters | ||
} | ||
isolated function testDataLoaderWithInterceptors() returns error? { | ||
graphql:Client graphqlClient = check new ("localhost:9090/dataloader_with_interceptor"); | ||
string document = check getGraphqlDocumentFromFile("dataloader_with_interceptor"); | ||
json response = check graphqlClient->execute(document); | ||
json expectedPayload = check getJsonContentFromFile("dataloader_with_interceptor"); | ||
assertJsonValuesWithOrder(response, expectedPayload); | ||
assertDispatchCountForAuthorLoader(1); | ||
assertDispatchCountForBookLoader(1); | ||
} | ||
|
||
@test:Config { | ||
groups: ["dataloader", "dispatch-error"], | ||
after: resetDispatchCounters | ||
} | ||
isolated function testBatchFunctionReturningErrors() returns error? { | ||
graphql:Client graphqlClient = check new ("localhost:9090/dataloader"); | ||
string document = check getGraphqlDocumentFromFile("batch_function_returing_errors"); | ||
json response = check graphqlClient->execute(document); | ||
json expectedPayload = check getJsonContentFromFile("batch_function_returing_errors"); | ||
assertJsonValuesWithOrder(response, expectedPayload); | ||
assertDispatchCountForAuthorLoader(1); | ||
assertDispatchCountForBookLoader(0); | ||
} | ||
|
||
@test:Config { | ||
groups: ["dataloader", "dispatch-error"], | ||
after: resetDispatchCounters | ||
} | ||
isolated function testBatchFunctionReturingNonMatchingNumberOfResults() returns error? { | ||
graphql:Client graphqlClient = check new ("localhost:9090/dataloader_with_faulty_batch_function"); | ||
string document = check getGraphqlDocumentFromFile("batch_function_returning_non_matcing_number_of_results"); | ||
json response = check graphqlClient->execute(document); | ||
json expectedPayload = check getJsonContentFromFile("batch_function_returning_non_matcing_number_of_results"); | ||
assertJsonValuesWithOrder(response, expectedPayload); | ||
} | ||
|
||
isolated function resetDispatchCounters() { | ||
lock { | ||
dispatchCountOfAuthorLoader = 0; | ||
} | ||
lock { | ||
dispatchCountOfBookLoader = 0; | ||
} | ||
lock { | ||
dispatchCountOfUpdateAuthorLoader = 0; | ||
} | ||
} | ||
|
||
isolated function assertDispatchCountForBookLoader(int expectedCount) { | ||
lock { | ||
test:assertEquals(dispatchCountOfBookLoader, expectedCount); | ||
} | ||
} | ||
|
||
isolated function assertDispatchCountForUpdateAuthorLoader(int expectedCount) { | ||
lock { | ||
test:assertEquals(dispatchCountOfUpdateAuthorLoader, expectedCount); | ||
} | ||
} | ||
|
||
isolated function assertDispatchCountForAuthorLoader(int expectedCount) { | ||
lock { | ||
test:assertEquals(dispatchCountOfAuthorLoader, expectedCount); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) 2023 WSO2 LLC. (http://www.wso2.com) All Rights Reserved. | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
isolated function authorLoaderFunction(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 => authorTable.hasKey(key)).cloneReadOnly(); | ||
return keys.length() != validKeys.length() ? error("Invalid keys found for authors") | ||
: validKeys.'map(key => authorTable.get(key)); | ||
} | ||
}; | ||
|
||
isolated function bookLoaderFunction(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 bookTable.'filter(book => book.author == key).toArray().clone(); | ||
} | ||
}); | ||
}; | ||
|
||
isolated function authorUpdateLoaderFunction(readonly & anydata[] idNames) returns AuthorRow[]|error { | ||
readonly & [int, string][] idValuePair = check idNames.ensureType(); | ||
// Simulate batch udpate | ||
lock { | ||
dispatchCountOfUpdateAuthorLoader += 1; | ||
} | ||
lock { | ||
foreach [int, string] [key, _] in idValuePair { | ||
if !authorTable.hasKey(key) { | ||
return error(string `Invalid key author key found: ${key}`); | ||
} | ||
} | ||
|
||
AuthorRow[] updatedAuthorRows = []; | ||
foreach [int, string] [key, name] in idValuePair { | ||
AuthorRow authorRow = {id: key, name}; | ||
authorTable.put(authorRow); | ||
updatedAuthorRows.push(authorRow.clone()); | ||
} | ||
return updatedAuthorRows.clone(); | ||
} | ||
}; | ||
|
||
isolated function faultyAuthorLoaderFunction(readonly & anydata[] ids) returns AuthorRow[]|error { | ||
readonly & int[] keys = check ids.ensureType(); | ||
lock { | ||
readonly & int[] validKeys = keys.'filter(key => authorTable.hasKey(key)).cloneReadOnly(); | ||
// This method may return an array of size not equal to the input array (ids) size. | ||
return validKeys.'map(key => authorTable.get(key)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
ballerina-tests/tests/resources/documents/batch_function_returing_errors.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
query { | ||
authors(ids: [1, 2, 3, 4, 5, 6]) { | ||
name | ||
books { | ||
id | ||
title | ||
} | ||
} | ||
} |
Oops, something went wrong.