Skip to content

Commit

Permalink
Merge pull request #4947 from MohamedSabthar/ctx-mstr
Browse files Browse the repository at this point in the history
[Master] Move contextInit function definition outside the graphql:ServiceConfig annotation
  • Loading branch information
MohamedSabthar committed Sep 25, 2023
2 parents ded7348 + e774e57 commit 39475f4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
13 changes: 7 additions & 6 deletions examples/custom-prefetch-methods/custom_prefetch_methods.bal
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ isolated function bookLoaderFunction(readonly & anydata[] ids) returns BookRow[]
}

@graphql:ServiceConfig {
contextInit: isolated function(http:RequestContext requestContext, http:Request request)
returns graphql:Context {
graphql:Context ctx = new;
ctx.registerDataLoader("bookLoader", new dataloader:DefaultDataLoader(bookLoaderFunction));
return ctx;
}
contextInit
}
service /graphql on new graphql:Listener(9090) {
resource function get authors() returns Author[] {
Expand Down Expand Up @@ -84,3 +79,9 @@ public isolated distinct service class Author {
select {id: bookRow.id, title: bookRow.title};
}
}

isolated function contextInit(http:RequestContext requestContext, http:Request request) returns graphql:Context {
graphql:Context ctx = new;
ctx.registerDataLoader("bookLoader", new dataloader:DefaultDataLoader(bookLoaderFunction));
return ctx;
}
28 changes: 14 additions & 14 deletions examples/graphql-context/graphql_context.bal
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,7 @@ type Profile record {|

@graphql:ServiceConfig {
// Initialization of the `graphqlContext` should be provided to the `contextInit` field.
contextInit: isolated function(http:RequestContext requestContext, http:Request request)
returns graphql:Context|error {

// Initialize the `graphql:Context` object.
graphql:Context context = new;

// Retrieves the header named `scope` from the `http:request` and set it to the context with
// the `scope` key. If the header does not exist, this will return an `error`, and thereby,
// the request will not be processed.
context.set("scope", check request.getHeader("scope"));

// Finally, the context object should be returned.
return context;
}
contextInit
}
service /graphql on new graphql:Listener(9090) {
// Defines a `Profile` field inside the service.
Expand All @@ -50,3 +37,16 @@ service /graphql on new graphql:Listener(9090) {
return error("Permission denied");
}
}

isolated function contextInit(http:RequestContext requestContext, http:Request request) returns graphql:Context|error {
// Initialize the `graphql:Context` object.
graphql:Context context = new;

// Retrieves the header named `scope` from the `http:request` and set it to the context with
// the `scope` key. If the header does not exist, this will return an `error`, and thereby,
// the request will not be processed.
context.set("scope", check request.getHeader("scope"));

// Finally, the context object should be returned.
return context;
}
17 changes: 9 additions & 8 deletions examples/graphql-dataloader/graphql_dataloader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@ isolated function bookLoaderFunction(readonly & anydata[] ids) returns BookRow[]
}

@graphql:ServiceConfig {
contextInit: isolated function(http:RequestContext requestContext, http:Request request)
returns graphql:Context {
graphql:Context ctx = new;
// Register the dataloader with the context using a unique name.
// A defult implementation of the dataloader is used here.
ctx.registerDataLoader("bookLoader", new dataloader:DefaultDataLoader(bookLoaderFunction));
return ctx;
}
contextInit
}
service /graphql on new graphql:Listener(9090) {
resource function get authors() returns Author[] {
Expand Down Expand Up @@ -93,3 +86,11 @@ public isolated distinct service class Author {
select {id: bookRow.id, title: bookRow.title};
}
}

isolated function contextInit(http:RequestContext requestContext, http:Request request) returns graphql:Context {
graphql:Context ctx = new;
// Register the dataloader with the context using a unique name.
// A defult implementation of the dataloader is used here.
ctx.registerDataLoader("bookLoader", new dataloader:DefaultDataLoader(bookLoaderFunction));
return ctx;
}

0 comments on commit 39475f4

Please sign in to comment.