Skip to content

Commit

Permalink
feat(graphql): add isMutation etc helpers to Options types
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed Sep 17, 2020
1 parent 7192c16 commit 04e7888
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/graphql/lib/src/core/_base_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:gql_exec/gql_exec.dart';
import 'package:graphql/client.dart';
import 'package:graphql/src/core/policies.dart';

/// TODO refactor into [Request] container
/// Base options.
abstract class BaseOptions extends MutableDataClass {
BaseOptions({
Expand Down Expand Up @@ -65,4 +66,21 @@ abstract class BaseOptions extends MutableDataClass {
policies,
context,
];

OperationType get type {
final definitions =
document.definitions.whereType<OperationDefinitionNode>().toList();
if (operationName != null) {
definitions.removeWhere(
(node) => node.name.value != operationName,
);
}
// TODO differentiate error types, add exception
assert(definitions.length == 1);
return definitions.first.type;
}

bool get isQuery => type == OperationType.query;
bool get isMutation => type == OperationType.mutation;
bool get isSubscription => type == OperationType.subscription;
}

0 comments on commit 04e7888

Please sign in to comment.