Skip to content

Commit

Permalink
[GitHub Actions] Commit generated GraphQL client
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Sep 27, 2022
1 parent 6257be5 commit 2feeca7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions client/geo_data_client.bal
Original file line number Diff line number Diff line change
@@ -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<anydata> variables = {"name": name};
json graphqlResponse = check self.graphqlClient->executeWithType(query, variables);
return <DistrictAndCityByProvinceResponse> check performDataBinding(graphqlResponse, DistrictAndCityByProvinceResponse);
}
}
20 changes: 20 additions & 0 deletions client/types.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public type DistrictAndCityByProvinceResponse record {|
map<json?> __extensions?;
record {|
record {|
record {|
string name_en;
|} name;
record {|
record {|
string name_en;
|} name;
record {|
record {|
string name_en;
|} name;
|}[] cities;
|}[] districts;
|} province;
|} geo;
|};
23 changes: 23 additions & 0 deletions client/utils.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import ballerina/graphql;

type OperationResponse record {| anydata...; |}|record {| anydata...; |}[]|boolean|string|int|float|();

type DataResponse record {|
map<json?> __extensions?;
OperationResponse ...;
|};

isolated function performDataBinding(json graphqlResponse, typedesc<DataResponse> targetType)
returns DataResponse|graphql:RequestError {
do {
map<json> responseMap = <map<json>>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);
}
}

0 comments on commit 2feeca7

Please sign in to comment.