Skip to content

Commit

Permalink
Address review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedSabthar committed Aug 9, 2023
1 parent 6a1af29 commit 604abe4
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public isolated distinct service class Author {

// Annotate the resource function to use a prefetch function having a custom name.
// This configuration instructs the GraphQL engine to call the `prefetchBooks` function
// before calling the resource function `books`.
// before calling the `books` resource function.
@graphql:ResourceConfig {
prefetchMethodName: "prefetchBooks"
}
Expand Down
4 changes: 3 additions & 1 deletion examples/custom-prefetch-methods/custom_prefetch_methods.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# GraphQL service - Custom prefetch methods

A prefetch method, in the context of Ballerina GraphQL is a method that is invoked before the actual resolver method. By default the prefetch method name has the convention of prefix `pre` followed by the resolver method name. If the user wants to use a custom prefetch method, the `prefetchMethodName` field of the `graphql:ResourceConfig` annotation can be used to override the default prefetch method name. This configuration is useful when the Query and Mutation operations have fields with the same name, and the user wants to use different prefetch methods for each field. To override the default prefetch method name, annotate the target field with the `graphql:ResourceConfig` annotation and specify the prefetch method name as the value of the `prefetchMethodName` field.
A prefetch method in the context of Ballerina GraphQL is a method that is invoked before the actual resolver method. By default the prefetch method name has the convention of having the prefix `pre` followed by the resolver method name.

If you want to use a custom prefetch method, the `prefetchMethodName` field of the `graphql:ResourceConfig` annotation can be used to override the default prefetch method name. This configuration is useful when the Query and Mutation operations have fields with the same name, and when it is required to use different prefetch methods for each field. To override the default prefetch method name, annotate the target field with the `graphql:ResourceConfig` annotation and specify the prefetch method name as the value of the `prefetchMethodName` field.

::: code graphql_prefetch_config.bal :::

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
description: BBE on overriding a default prefetch method name
description: BBE on overriding a default prefetch method name.
keywords: ballerina, ballerina by example, bbe, graphql, dataloader, prefetch method
6 changes: 3 additions & 3 deletions examples/graphql-dataloader/graphql_dataloader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ final readonly & table<BookRow> key(id) bookTable = table [
{id: 6, title: "The Stand", author: 2}
];

// Implement the batch load function for the data loader.
// Implement the `batch load` function for the data loader.
// This function handles fetching data in batches. The primary keys of the data to be loaded
// will be provided as the `ids` parameter to the batch load function. The expected output
// of this function is an array of results, where each element in the result array corresponds
// will be provided as the values of the `ids` parameter to the batch load function. The expected output
// of this function is an array of results in which each element in the result array corresponds
// to a primary key from the `ids` array.
isolated function bookLoaderFunction(readonly & anydata[] ids) returns BookRow[][]|error {
final readonly & int[] keys = check ids.ensureType();
Expand Down
6 changes: 4 additions & 2 deletions examples/graphql-dataloader/graphql_dataloader.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# GraphQL service - Dataloader

The Ballerina GraphQL module provides the capability to batch and cache data fetching from data sources using the `graphql.dataloader` submodule. To leverage this functionality in a GraphQL service, users must register data loaders through the `graphql:Context` object and implement the corresponding prefetch method logic and resolver method logic. A prefetch method, in the context of Ballerina GraphQL, is a method that is invoked before the actual resolver method. By default, the prefetch method name follows the convention of the prefix `pre`, followed by the resolver method name. The use of `graphql.dataloader` helps users avoid excessive data fetching, effectively addressing the GraphQL N+1 problem.
The Ballerina GraphQL module provides the capability to batch and cache data fetching from data sources using the `graphql.dataloader` submodule. To leverage this functionality in a GraphQL service, you must register data loaders through the `graphql:Context` object and implement the corresponding prefetch method logic and resolver method logic.

A prefetch method, in the context of Ballerina GraphQL, is a method that is invoked before the actual resolver method. By default, the prefetch method name follows the convention of having the prefix `pre`, followed by the resolver method name. The use of `graphql.dataloader` avoids excessive data fetching, effectively addressing the GraphQL N+1 problem.

::: code graphql_dataloader.bal :::

Expand All @@ -19,4 +21,4 @@ To send the document, execute the following cURL command in a separate terminal.
## Related links
- [`garaphql.dataloader` module - API documentation](https://lib.ballerina.io/ballerina/graphql.dataloader/latest)
- [DataLoader - Specification](/spec/graphql/#106-dataloader)
- [Utilizing Multiple DataLoaders in a GraphQL Service](/spec/graphql/#example-utilizing-multiple-dataloaders-in-a-graphql-service)
- [Utilizing multiple dataloaders in a graphql service](/spec/graphql/#example-utilizing-multiple-dataloaders-in-a-graphql-service)
2 changes: 1 addition & 1 deletion examples/graphql-dataloader/graphql_dataloader.metatags
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
description: BBE on how to engage dataloaders with graphql service
description: BBE on how to engage dataloaders with GraphQL service
keywords: ballerina, ballerina by example, bbe, graphql, dataloader, batching, caching

0 comments on commit 604abe4

Please sign in to comment.