Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Remove mandatory variable Definitions and allow access token in header #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions client/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class RestClient extends http.BaseClient {
http.Client _client;
String _endpoint;
Uri _baseUri;
String _authToken;

RestClient(this._endpoint) {
RestClient(this._endpoint, this._authToken) {
this._client = new http.Client();
this._baseUri = Uri.parse(this._endpoint);
}
Expand Down Expand Up @@ -45,7 +46,12 @@ class RestClient extends http.BaseClient {
String body = toJson(data);
Uri uri = _baseUri.replace(path: _baseUri.path + path);
http.Response response = await this.post(uri.toString(),
body: body, headers: {'Content-Type': 'application/json'});
body: body,
headers: {
'Content-Type': 'application/json',
'Authorization': _authToken
}
);
return handleJsonResponse(response);
}

Expand Down Expand Up @@ -85,7 +91,7 @@ class RestClient extends http.BaseClient {
}

class GraphqlClient extends RestClient {
GraphqlClient(String endpoint) : super(endpoint);
GraphqlClient(String endpoint, String authToken) : super(endpoint, authToken);

Future<JsonResponse> request<T>(
String query, Map<String, dynamic> variables) async {
Expand Down Expand Up @@ -118,7 +124,8 @@ main() async {
}
}""";
GraphqlClient cli = new GraphqlClient(
"http://localhost:60000/simple/v1/cj9mldxkd008c017544mu2vhw");
"http://localhost:60000/simple/v1/cj9mldxkd008c017544mu2vhw",
"test token");
var result = await cli.request(query, {"alias": "atest"});
print(result);
}
12 changes: 7 additions & 5 deletions generator/lib/src/GraphqlSetting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ GraphqlBuildSetting createSetting(
{String schemaUrl,
String method: "post",
bool postIntrospectionQuery: true,
String schemaFile}) {
return new GraphqlBuildSetting(
schemaUrl, method, postIntrospectionQuery, schemaFile);
String schemaFile,
String authToken
}) {
return new GraphqlBuildSetting(schemaUrl, method, postIntrospectionQuery, schemaFile, authToken);
}

class GraphqlBuildSetting {
final Logger log = new Logger('GraphqlSetting');

String schemaUrl;
String method;
String authToken;
bool postIntrospectionQuery;

String schemaFile;

GraphqlBuildSetting(this.schemaUrl, this.method, this.postIntrospectionQuery,
this.schemaFile);
this.schemaFile, this.authToken);

dynamic _schemaObject = null;

Expand All @@ -34,7 +36,7 @@ class GraphqlBuildSetting {
var fileContent = await new File(schemaFile).readAsString();
_schemaObject = new JsonObject.fromJsonString(fileContent);
} else if (schemaUrl != null) {
var client = new RestClient(this.schemaUrl);
var client = new RestClient(this.schemaUrl, this.authToken);
log.info("fetching schema from url:${this.schemaUrl}");
JsonResponse result;
if (method == "post") {
Expand Down
8 changes: 5 additions & 3 deletions generator/lib/src/Operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ class Operation extends BaseTypes {
generateCode(String resultClass) {
Reference graphqlQuery =
refer("GraphqlQuery", "package:graphql_fetch/graphql_fetch.dart");
List<String> variables = _context.variableDefinitions.variableDefinitions
List<String> variables = [];
if (_context.variableDefinitions != null) {
variables = _context.variableDefinitions.variableDefinitions
.map((v) => '"${v.variable.name}": ${v.variable.name}')
.toList();
}
String query = wrapStringCode(_context.span.text);

List<String> strings = _queryTypes.depFragments.map((r) => "${r
Expand All @@ -39,7 +42,7 @@ class Operation extends BaseTypes {
return "const query = $query;"
"return new ${a(graphqlQuery)}("
"${strings.join(" + ")},"
"{${variables.join(',')}},"
"${variables.length> 1 ? variables.join(','): null},"
"${resultClass}.fromMap);";
});
}
Expand All @@ -54,7 +57,6 @@ class Operation extends BaseTypes {
b.body.add(cls);
return resultClassName;
}

generateReturn(FileBuilder b) {
for (SelectionContext sel in _context.selectionSet.selections) {
String field = sel.field.fieldName.name;
Expand Down