Skip to content

Commit

Permalink
Merge pull request #5342 from ThisaruGuruge/master
Browse files Browse the repository at this point in the history
Fix GraphQL BBE issues
  • Loading branch information
ThisaruGuruge authored Apr 16, 2024
2 parents 940babf + 15993b0 commit d1b31f5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/graphql-dataloader/graphql_dataloader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final readonly & table<BookRow> key(id) bookTable = table [
// 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();
final int[] keys = check ids.ensureType();
log:printInfo("executing bookLoaderFunction", keys = keys);
// Implement the batching logic.
return keys.'map(isolated function(readonly & int key) returns BookRow[] {
Expand Down
4 changes: 2 additions & 2 deletions examples/graphql-documentation/graphql_documentation.bal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ballerina/graphql;

// All the types that are used in the GraphQL service can have doc comments to add as documentation.
// All the types that are used in the GraphQL service can have documentation.
# Represents a profile.
# + name - The name of the profile
# + age - The age of the profile
Expand All @@ -11,7 +11,7 @@ type Profile record {|

service /graphql on new graphql:Listener(9090) {

// Add doc comments to reflect them in the generated GraphQL schema.
// Add documentation to reflect them in the generated GraphQL schema.
# Returns a profile using the provided ID.
# + id - The ID of the profile
# + return - The profile with the requested ID
Expand Down
2 changes: 1 addition & 1 deletion examples/graphql-documentation/graphql_documentation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# GraphQL service - Documentation

The Ballerina `graphql` module allows adding documentation to the `graphql:Service` and its subsequent types. To add documentation, use the Ballerina doc comments for the `graphql:Service`, `resource`/`remote` methods, types, and `enum`s. Add the documentation to include the descriptions to the generated GraphQL schema.
The Ballerina `graphql` module allows adding documentation to the generated GraphQL schema and its subsequent types. To add documentation, use the Ballerina documentation for the `graphql:Service`, `resource`/`remote` methods, types, and `enum`s. The Ballerina documentation will be automatically added as the documentation in the generated GraphQL schema.

::: code graphql_documentation.bal :::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ readonly service class LogInterceptor {
log:printInfo(string `Field "${fieldName}" execution started!`);

// The `context.resolve()` function can be used to invoke the next interceptor. If all the
// interceptors were executed, and it invokes the actual resolver function. The function
// interceptors were executed, then it invokes the actual resolver function. The function
// returns an `anydata` type value that includes the execution result of the next
// interceptor or the actual resolver. To call the `context.resolve()` function, the
// `graphql:Field` value should be provided as the argument.
Expand Down
36 changes: 27 additions & 9 deletions examples/graphql-subscriptions/graphql_subscriptions.bal
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import ballerina/graphql;
import ballerina/lang.runtime;
import ballerina/random;

service /graphql on new graphql:Listener(9090) {
// Defines a `string` array in the service.
private string[] names;

function init() {
// Initialize the array.
self.names = ["Walter White", "Jesse Pinkman", "Skyler White"];
}
private final readonly & string[] names = ["Walter White", "Jesse Pinkman", "Saul Goodman"];

resource function get names() returns string[] {
return self.names;
Expand All @@ -16,7 +12,29 @@ service /graphql on new graphql:Listener(9090) {
// A resource method with the `subscribe` accessor represents a field in the root
// `Subscription` operation. It must always return a stream. Since the stream is of type
// `string`, the resulting field in the generated GraphQL schema will be of type `String!`.
resource function subscribe names() returns stream<string> {
return self.names.toStream();
resource function subscribe names() returns stream<string, error?> {
// Create a `NameGenerator` object.
NameGenerator nameGenerator = new (self.names);
// Create a stream using the `NameGenerator` object.
stream<string, error?> names = new (nameGenerator);
return names;
}
}

// Defines a stream implementor that can be used to create a stream of strings. This will pick a random name from
// the list of names and return it with a delay to demonstrate a stream of values.
class NameGenerator {
private final string[] names;

isolated function init(string[] names) {
self.names = names;
}

// The `next` method picks a random name from the list and returns it.
public isolated function next() returns record {|string value;|}|error? {
// Sleep for 1 second to simulate a delay.
runtime:sleep(1);
int index = check random:createIntInRange(0, self.names.length());
return {value: self.names[index]};
}
}

0 comments on commit d1b31f5

Please sign in to comment.