From e774e57e40c599c67b6ad1dd0c3e561fcf76a17d Mon Sep 17 00:00:00 2001 From: MohamedSabthar Date: Sun, 24 Sep 2023 13:13:38 +0530 Subject: [PATCH] Move contextInit function definition outside the graphql:ServiceConfig annotation --- .../custom_prefetch_methods.bal | 13 +++++---- examples/graphql-context/graphql_context.bal | 28 +++++++++---------- .../graphql-dataloader/graphql_dataloader.bal | 17 +++++------ 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/examples/custom-prefetch-methods/custom_prefetch_methods.bal b/examples/custom-prefetch-methods/custom_prefetch_methods.bal index 8594179502..fddea034b9 100644 --- a/examples/custom-prefetch-methods/custom_prefetch_methods.bal +++ b/examples/custom-prefetch-methods/custom_prefetch_methods.bal @@ -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[] { @@ -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; +} diff --git a/examples/graphql-context/graphql_context.bal b/examples/graphql-context/graphql_context.bal index 30dc8007e4..96cc33ed0e 100644 --- a/examples/graphql-context/graphql_context.bal +++ b/examples/graphql-context/graphql_context.bal @@ -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. @@ -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; +} diff --git a/examples/graphql-dataloader/graphql_dataloader.bal b/examples/graphql-dataloader/graphql_dataloader.bal index 99246efbfc..bbc8737fa4 100644 --- a/examples/graphql-dataloader/graphql_dataloader.bal +++ b/examples/graphql-dataloader/graphql_dataloader.bal @@ -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[] { @@ -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; +}