Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use gql query as string instead of the internal dsl #2

Merged
merged 4 commits into from
Jul 20, 2017
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ pubspec.lock
# Directory created by dartdoc
# If you don't generate documentation locally you can remove this line.
doc/api/

# GraphQL IDE completion
graphql.config.json
16 changes: 8 additions & 8 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ GQL query you built and is handling reconciliation between the HTTP response and
## Roadmap
- `QueryBuilder`:
- [ ] Builder
- [ ] Fields
- [ ] Alias
- [ ] Arguments
- [x] Fields
- [x] Arguments
- [ ] Aliases
- [ ] Fragments
- [x] Fragments
- [ ] Variables
- [ ] Directives
- [ ] **Optionnal** Inline Fragments
- [x] Directives
- [x] **Optionnal** Inline Fragments
- `VariableBuilder`:
- [ ] Builder
- `MutationBuilder`:
Expand All @@ -33,16 +32,17 @@ GQL query you built and is handling reconciliation between the HTTP response and
- [ ] Variables
- `QueryReconcilier`:
- [ ] Reconciler
- [ ] Reconcile Aliases
- `Scalar Types`:
- [ ] Implements default types
- `GraphQLAnnotations` (used to define queries and mutations):
- [ ] Field arguments
- [ ] Field directive
- [ ] Field alias
- `GraphQLClient`:
- [ ] Factory
- [x] Factory
- [ ] GraphQL Response
- [ ] **Optionnal** GraphQL dart classes generation from a GQL schema
- [ ] **Optionnal** Translate GQL queries from String to `graphql_client` DSL
- [x] **Optionnal** Translate GQL queries from String to `graphql_client` DSL

[1]: http://facebook.github.io/graphql/
14 changes: 5 additions & 9 deletions example/github_declarations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import 'package:graphql_client/graphql_client.dart';

class Query {
class GithubGraphQLSchema implements Schema {
Viewer viewer;

Query({this.viewer});
Query.fromJSON(Map data) : viewer = new Viewer.fromJSON(data['viewer']);
GithubGraphQLSchema.fromJSON(Map data)
: viewer = new Viewer.fromJSON(data['viewer']);

@override
String toString() {
Expand All @@ -20,15 +20,12 @@ ${indentLines(viewer.toString(), 4)}
}
}

class Viewer {
class Viewer implements Schema {
GraphQLString login;
@GraphQLArguments('size: 200')
GraphQLString avatarUrl;
GraphQLString bio;
@GraphQLArguments('last: 2')
GraphQLConnection<Gist> gists;

Viewer({this.login, this.avatarUrl, this.bio, this.gists});
Viewer.fromJSON(Map data)
: login = new GraphQLString(data['login']),
avatarUrl = new GraphQLString(data['avatarUrl']),
Expand All @@ -47,11 +44,10 @@ ${indentLines(gists.toString(), 2)}
}
}

class Gist {
class Gist implements Schema {
GraphQLString name;
GraphQLString description;

Gist({this.name, this.description});
Gist.fromJSON(Map data)
: name = new GraphQLString(data['name']),
description = new GraphQLString(data['description']);
Expand Down
46 changes: 28 additions & 18 deletions example/graphql_client_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,34 @@ main() async {
client: client,
logger: logger,
endPoint: endPoint,
);

Query builtQuery = new Query(
viewer: new Viewer(
avatarUrl: new GraphQLString(),
login: new GraphQLString(),
bio: new GraphQLString(),
gists: new GraphQLConnection<Gist>(
nodes: new Gist(
name: new GraphQLString(),
description: new GraphQLString(),
),
),
),
);

await graphQLClient.execute<Query>(
builtQuery,
)..loadSchema(GithubGraphQLSchema);

//language=GraphQL
String gqlQuery = '''
query {
viewer {
avatarUrl(size: 200)
login
bio @include(if: false)
gists(first: 5) {
nodes {
...ShortGist
}
}
}
}

fragment ShortGist on Gist {
name
description
}
''';

var res = await graphQLClient.execute<GithubGraphQLSchema>(
gqlQuery,
headers: {'Authorization': 'bearer $apiToken'},
);

print(res.viewer.avatarUrl.value);
print(res.viewer.gists.nodes.first.name.value);
}
2 changes: 1 addition & 1 deletion lib/graphql_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export 'utils.dart';
export 'annotations.dart';
export 'scalar_types.dart';
export 'connection.dart';
export 'query_builder.dart';
export 'query_reconcilier.dart';
export 'schema.dart';
1 change: 1 addition & 0 deletions lib/query_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
@Deprecated('Not used anymore')
library graphql_client.query_builder;

export 'src/query_builder.dart';
10 changes: 10 additions & 0 deletions lib/schema.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright Thomas Hourlier. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.

/// Support for doing something awesome.
///
/// More dartdocs go here.
library graphql_client.schema;

export 'src/schema.dart';
28 changes: 19 additions & 9 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,49 @@ library graphql_client.src.client;

import 'dart:async';
import 'dart:convert';
import 'dart:mirrors';

import 'package:http/http.dart';
import 'package:logging/logging.dart';

import 'query_builder.dart';
import 'query_reconcilier.dart';
import 'schema.dart';

class GraphQLClient {
Client client;
String endPoint;
Logger logger;
ClassMirror _schemaMirror;

GraphQLClient({
this.client,
this.endPoint,
this.logger,
});

Future<T> execute<T>(T query,
void loadSchema(Type schemaClass) {
_schemaMirror = reflectClass(schemaClass);
}

Future<T> execute<T extends Schema>(String gqlQuery,
{Map<String, String> headers = const {}}) async {
String gqlQuery = queryBuilder<T>(query);
if (_schemaMirror == null) {
throw new StateError("You must load a schema before executing a query");
}

logger.finest('Query: $gqlQuery');

var response = await client.post(endPoint,
headers: headers,
body: JSON.encode(
{'query': gqlQuery},
));
var response = await client.post(
endPoint,
headers: headers,
body: JSON.encode(
{'query': gqlQuery},
),
);

logger.finest('Response: ${response.body}');

T reconciliedQuery = reconcileResponse<T>(query, response.body);
T reconciliedQuery = reconcileResponse<T>(_schemaMirror, response.body);

logger.finest('Result: \n$reconciliedQuery');

Expand Down
2 changes: 2 additions & 0 deletions lib/src/query_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'dart:mirrors';
import 'annotations.dart';
import 'connection.dart';

@Deprecated('Not used anymore')
String buildInstanceQuery(InstanceMirror instanceMirror) {
ClassMirror classMirror = instanceMirror.type;
var classDeclarations = classMirror.declarations;
Expand Down Expand Up @@ -60,6 +61,7 @@ String buildInstanceQuery(InstanceMirror instanceMirror) {
return '{ ${gqlQuery.trim()} }';
}

@Deprecated('Not used anymore')
String queryBuilder<T>(T query) {
InstanceMirror queryInstanceMirror = reflect(query);
String gqlQuery = buildInstanceQuery(queryInstanceMirror);
Expand Down
5 changes: 2 additions & 3 deletions lib/src/query_reconcilier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ library graphql_client.src.query_reconcilier;
import 'dart:mirrors';
import 'dart:convert';

T reconcileResponse<T>(T query, String response) {
T reconcileResponse<T>(ClassMirror schemaMirror, String response) {
Map jsonResponse = JSON.decode(response);
ClassMirror classMirror = reflect(query).type;

return classMirror
return schemaMirror
.newInstance(const Symbol('fromJSON'), [jsonResponse['data']]).reflectee;
}
9 changes: 9 additions & 0 deletions lib/src/schema.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright Thomas Hourlier. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.

library graphql_client.src.schema;

abstract class Schema {
Schema.fromJSON(Map data);
}