Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #324 from tuannyharumi/feat/execute-with-context
Browse files Browse the repository at this point in the history
Accept and return gql context in client's methods
  • Loading branch information
comigor authored Jun 17, 2021
2 parents 795aa7f + 6365901 commit af71aad
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 7.0.0-beta.14

- Support gql context in client's execute and stream methods

## 7.0.0-beta.13

- fix for https://github.com/comigor/artemis/issues/177
Expand Down
14 changes: 10 additions & 4 deletions lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,45 @@ class ArtemisClient {

/// Executes a [GraphQLQuery], returning a typed response.
Future<GraphQLResponse<T>> execute<T, U extends JsonSerializable>(
GraphQLQuery<T, U> query,
) async {
GraphQLQuery<T, U> query, {
Context context = const Context(),
}) async {
final request = Request(
operation: Operation(
document: query.document,
operationName: query.operationName,
),
variables: query.getVariablesMap(),
context: context,
);

final response = await _link.request(request).first;

return GraphQLResponse<T>(
data: response.data == null ? null : query.parse(response.data ?? {}),
errors: response.errors,
context: response.context,
);
}

/// Streams a [GraphQLQuery], returning a typed response stream.
Stream<GraphQLResponse<T>> stream<T, U extends JsonSerializable>(
GraphQLQuery<T, U> query,
) {
GraphQLQuery<T, U> query, {
Context context = const Context(),
}) {
final request = Request(
operation: Operation(
document: query.document,
operationName: query.operationName,
),
variables: query.getVariablesMap(),
context: context,
);

return _link.request(request).map((response) => GraphQLResponse<T>(
data: response.data == null ? null : query.parse(response.data ?? {}),
errors: response.errors,
context: response.context,
));
}

Expand Down
4 changes: 4 additions & 0 deletions lib/schema/graphql_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class GraphQLResponse<T> {
/// If this response has any error.
bool get hasErrors => errors != null && errors!.isNotEmpty;

/// The [Context] of the [Response]
final Context? context;

/// Instantiates a GraphQL response.
const GraphQLResponse({
this.data,
this.errors,
this.context,
});
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: artemis
version: 7.0.0-beta.13
version: 7.0.0-beta.14

description: Build dart types from GraphQL schemas and queries (using Introspection Query).
homepage: https://github.com/comigor/artemis
Expand Down

0 comments on commit af71aad

Please sign in to comment.