From 1601e8a3758bc3b3156296db47a78cf4702a001b Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 27 Sep 2022 15:16:55 +0000 Subject: [PATCH] [GitHub Actions] Commit generated GraphQL client --- client/geo_data_client.bal | 17 +++++++++++++++++ client/types.bal | 20 ++++++++++++++++++++ client/utils.bal | 23 +++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 client/geo_data_client.bal create mode 100644 client/types.bal create mode 100644 client/utils.bal diff --git a/client/geo_data_client.bal b/client/geo_data_client.bal new file mode 100644 index 0000000..f77f806 --- /dev/null +++ b/client/geo_data_client.bal @@ -0,0 +1,17 @@ +import ballerina/http; +import ballerina/graphql; + +public isolated client class Geo_dataClient { + final graphql:Client graphqlClient; + public isolated function init(string serviceUrl, http:ClientConfiguration clientConfig = {}) returns graphql:ClientError? { + graphql:Client clientEp = check new (serviceUrl, clientConfig); + self.graphqlClient = clientEp; + return; + } + remote isolated function DistrictAndCityByProvince(string name) returns DistrictAndCityByProvinceResponse|graphql:ClientError { + string query = string `query DistrictAndCityByProvince($name:String!) {geo {province(name:$name) {name {name_en} districts {name {name_en} cities {name {name_en}}}}}}`; + map variables = {"name": name}; + json graphqlResponse = check self.graphqlClient->executeWithType(query, variables); + return check performDataBinding(graphqlResponse, DistrictAndCityByProvinceResponse); + } +} diff --git a/client/types.bal b/client/types.bal new file mode 100644 index 0000000..964f2df --- /dev/null +++ b/client/types.bal @@ -0,0 +1,20 @@ +public type DistrictAndCityByProvinceResponse record {| + map __extensions?; + record {| + record {| + record {| + string name_en; + |} name; + record {| + record {| + string name_en; + |} name; + record {| + record {| + string name_en; + |} name; + |}[] cities; + |}[] districts; + |} province; + |} geo; +|}; diff --git a/client/utils.bal b/client/utils.bal new file mode 100644 index 0000000..5b0c224 --- /dev/null +++ b/client/utils.bal @@ -0,0 +1,23 @@ +import ballerina/graphql; + +type OperationResponse record {| anydata...; |}|record {| anydata...; |}[]|boolean|string|int|float|(); + +type DataResponse record {| + map __extensions?; + OperationResponse ...; +|}; + +isolated function performDataBinding(json graphqlResponse, typedesc targetType) + returns DataResponse|graphql:RequestError { + do { + map responseMap = >graphqlResponse; + json responseData = responseMap.get("data"); + if (responseMap.hasKey("extensions")) { + responseData = check responseData.mergeJson({"__extensions": responseMap.get("extensions")}); + } + DataResponse response = check responseData.cloneWithType(targetType); + return response; + } on fail var e { + return error graphql:RequestError("GraphQL Client Error", e); + } +}