Skip to content

Commit

Permalink
Merge pull request #1033 from domesticmouse/beta
Browse files Browse the repository at this point in the history
Use `flutter pub add` to add dependencies
  • Loading branch information
vincenzopalazzo authored and budde377 committed Feb 13, 2022
2 parents c9ca534 + 0a6b3f7 commit 1cc9635
Show file tree
Hide file tree
Showing 21 changed files with 486 additions and 267 deletions.
5 changes: 2 additions & 3 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ As of `v4`, it is built on foundational libraries from the [gql-dart project], i

First, depend on this package:

```yaml
dependencies:
graphql: ^4.0.0-beta
```console
$ flutter pub add graphql
```

And then import it inside your dart code:
Expand Down
16 changes: 9 additions & 7 deletions packages/graphql/lib/src/core/_base_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import 'package:gql/ast.dart';

import 'package:graphql/client.dart';
import 'package:graphql/src/core/result_parser.dart';
import 'package:meta/meta.dart';

/// TODO refactor into [Request] container
/// Base options.
@immutable
abstract class BaseOptions<TParsed> extends MutableDataClass {
BaseOptions({
required this.document,
Expand All @@ -30,22 +32,22 @@ abstract class BaseOptions<TParsed> extends MutableDataClass {
));

/// Document containing at least one [OperationDefinitionNode]
DocumentNode document;
final DocumentNode document;

/// Name of the executable definition
///
/// Must be specified if [document] contains more than one [OperationDefinitionNode]
String? operationName;
final String? operationName;

/// A map going from variable name to variable value, where the variables are used
/// within the GraphQL query.
Map<String, dynamic> variables;
final Map<String, dynamic> variables;

/// An optimistic result to eagerly add to the operation stream
Object? optimisticResult;
final Object? optimisticResult;

/// Specifies the [Policies] to be used during execution.
Policies policies;
final Policies policies;

FetchPolicy? get fetchPolicy => policies.fetch;

Expand All @@ -54,9 +56,9 @@ abstract class BaseOptions<TParsed> extends MutableDataClass {
CacheRereadPolicy? get cacheRereadPolicy => policies.cacheReread;

/// Context to be passed to link execution chain.
Context context;
final Context context;

ResultParserFn<TParsed> parserFn;
final ResultParserFn<TParsed> parserFn;

// TODO consider inverting this relationship
/// Resolve these options into a request
Expand Down
33 changes: 33 additions & 0 deletions packages/graphql/lib/src/core/mutation_options.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: deprecated_member_use_from_same_package
import 'dart:async';
import 'dart:math';
import 'package:graphql/client.dart';
import 'package:graphql/src/cache/cache.dart';
import 'package:graphql/src/core/_base_options.dart';
import 'package:graphql/src/core/observable_query.dart';
Expand All @@ -12,6 +14,7 @@ import 'package:graphql/src/exceptions.dart';
import 'package:graphql/src/core/query_result.dart';
import 'package:graphql/src/utilities/helpers.dart';
import 'package:graphql/src/core/policies.dart';
import 'package:meta/meta.dart';

typedef OnMutationCompleted = FutureOr<void> Function(dynamic data);
typedef OnMutationUpdate = FutureOr<void> Function(
Expand All @@ -20,6 +23,7 @@ typedef OnMutationUpdate = FutureOr<void> Function(
);
typedef OnError = FutureOr<void> Function(OperationException? error);

@immutable
class MutationOptions<TParsed> extends BaseOptions<TParsed> {
MutationOptions({
required DocumentNode document,
Expand Down Expand Up @@ -53,6 +57,35 @@ class MutationOptions<TParsed> extends BaseOptions<TParsed> {
@override
List<Object?> get properties =>
[...super.properties, onCompleted, update, onError];

MutationOptions<TParsed> copyWithPolicies(Policies policies) =>
MutationOptions(
document: document,
operationName: operationName,
variables: variables,
fetchPolicy: policies.fetch,
errorPolicy: policies.error,
cacheRereadPolicy: policies.cacheReread,
context: context,
optimisticResult: optimisticResult,
onCompleted: onCompleted,
update: update,
onError: onError,
parserFn: parserFn,
);

WatchQueryOptions<TParsed> asWatchQueryOptions() =>
WatchQueryOptions<TParsed>(
document: document,
operationName: operationName,
variables: variables,
fetchPolicy: fetchPolicy,
errorPolicy: errorPolicy,
cacheRereadPolicy: cacheRereadPolicy,
fetchResults: false,
context: context,
parserFn: parserFn,
);
}

/// Handles execution of mutation `update`, `onCompleted`, and `onError` callbacks
Expand Down
13 changes: 9 additions & 4 deletions packages/graphql/lib/src/core/observable_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,21 +330,26 @@ class ObservableQuery<TParsed> {
scheduler!.stopPollingQuery(queryId);
}

options.pollInterval = pollInterval;
options = options.copyWithPollInterval(pollInterval);
lifecycle = QueryLifecycle.polling;
scheduler!.startPollingQuery(options, queryId);
}

void stopPolling() {
if (isCurrentlyPolling) {
scheduler!.stopPollingQuery(queryId);
options.pollInterval = null;
options = options.copyWithPollInterval(null);
lifecycle = QueryLifecycle.pollingStopped;
}
}

set variables(Map<String, dynamic> variables) =>
options.variables = variables;
set variables(Map<String, dynamic> variables) {
options = options.copyWithVariables(variables);
}

set optimisticResult(Object? optimisticResult) {
options = options.copyWithOptimisticResult(optimisticResult);
}

/// [onData] callbacks have het to be run
///
Expand Down
8 changes: 3 additions & 5 deletions packages/graphql/lib/src/core/query_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,10 @@ class QueryManager {
/// Refetch the [ObservableQuery] referenced by [queryId],
/// overriding any present non-network-only [FetchPolicy].
Future<QueryResult<TParsed>?> refetchQuery<TParsed>(String queryId) {
final WatchQueryOptions<TParsed> options =
queries[queryId]!.options.copy() as WatchQueryOptions<TParsed>;
WatchQueryOptions<TParsed> options =
queries[queryId]!.options as WatchQueryOptions<TParsed>;
if (!willAlwaysExecuteOnNetwork(options.fetchPolicy)) {
options.policies = options.policies.copyWith(
fetch: FetchPolicy.networkOnly,
);
options = options.copyWithFetchPolicy(FetchPolicy.networkOnly);
}

// create a new request to execute
Expand Down
124 changes: 113 additions & 11 deletions packages/graphql/lib/src/core/query_options.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// ignore_for_file: deprecated_member_use_from_same_package
import 'dart:mirrors';

import 'package:graphql/src/core/_base_options.dart';
import 'package:graphql/src/core/result_parser.dart';
import 'package:graphql/src/utilities/helpers.dart';

import 'package:gql/ast.dart';

import 'package:graphql/client.dart';
import 'package:meta/meta.dart';

/// Query options.
@immutable
class QueryOptions<TParsed> extends BaseOptions<TParsed> {
QueryOptions({
required DocumentNode document,
Expand All @@ -33,7 +37,7 @@ class QueryOptions<TParsed> extends BaseOptions<TParsed> {
);

/// The time interval on which this query should be re-fetched from the server.
Duration? pollInterval;
final Duration? pollInterval;

@override
List<Object?> get properties => [...super.properties, pollInterval];
Expand All @@ -52,8 +56,22 @@ class QueryOptions<TParsed> extends BaseOptions<TParsed> {
optimisticResult: optimisticResult,
parserFn: this.parserFn,
);

copyWithPolicies(Policies policies) => QueryOptions(
document: document,
operationName: operationName,
variables: variables,
fetchPolicy: policies.fetch,
errorPolicy: policies.error,
cacheRereadPolicy: policies.cacheReread,
optimisticResult: optimisticResult,
pollInterval: pollInterval,
context: context,
parserFn: parserFn,
);
}

@immutable
class SubscriptionOptions<TParsed> extends BaseOptions<TParsed> {
SubscriptionOptions({
required DocumentNode document,
Expand All @@ -76,11 +94,20 @@ class SubscriptionOptions<TParsed> extends BaseOptions<TParsed> {
optimisticResult: optimisticResult,
parserFn: parserFn,
);

/// An optimistic first result to eagerly add to the subscription stream
Object? optimisticResult;
copyWithPolicies(Policies policies) => QueryOptions(
document: document,
operationName: operationName,
variables: variables,
fetchPolicy: policies.fetch,
errorPolicy: policies.error,
cacheRereadPolicy: policies.cacheReread,
optimisticResult: optimisticResult,
context: context,
parserFn: parserFn,
);
}

@immutable
class WatchQueryOptions<TParsed> extends QueryOptions<TParsed> {
WatchQueryOptions({
required DocumentNode document,
Expand Down Expand Up @@ -111,21 +138,95 @@ class WatchQueryOptions<TParsed> extends QueryOptions<TParsed> {
);

/// Whether or not to fetch results
bool fetchResults;
final bool fetchResults;

/// Whether to [fetchResults] immediately on instantiation.
/// Defaults to [fetchResults].
bool eagerlyFetchResults;
final bool eagerlyFetchResults;

/// carry forward previous data in the result of errors and no data.
/// defaults to `true`.
bool carryForwardDataOnException;
final bool carryForwardDataOnException;

@override
List<Object?> get properties =>
[...super.properties, fetchResults, eagerlyFetchResults];

WatchQueryOptions<TParsed> copy() => WatchQueryOptions<TParsed>(
WatchQueryOptions<TParsed> copyWithFetchPolicy(
FetchPolicy? fetchPolicy,
) =>
WatchQueryOptions<TParsed>(
document: document,
operationName: operationName,
variables: variables,
fetchPolicy: fetchPolicy,
errorPolicy: errorPolicy,
cacheRereadPolicy: cacheRereadPolicy,
optimisticResult: optimisticResult,
pollInterval: pollInterval,
fetchResults: fetchResults,
eagerlyFetchResults: eagerlyFetchResults,
carryForwardDataOnException: carryForwardDataOnException,
context: context,
parserFn: parserFn,
);
WatchQueryOptions<TParsed> copyWithPolicies(
Policies policies,
) =>
WatchQueryOptions<TParsed>(
document: document,
operationName: operationName,
variables: variables,
fetchPolicy: policies.fetch,
errorPolicy: policies.error,
cacheRereadPolicy: policies.cacheReread,
optimisticResult: optimisticResult,
pollInterval: pollInterval,
fetchResults: fetchResults,
eagerlyFetchResults: eagerlyFetchResults,
carryForwardDataOnException: carryForwardDataOnException,
context: context,
parserFn: parserFn,
);

WatchQueryOptions<TParsed> copyWithPollInterval(Duration? pollInterval) =>
WatchQueryOptions<TParsed>(
document: document,
operationName: operationName,
variables: variables,
fetchPolicy: fetchPolicy,
errorPolicy: errorPolicy,
cacheRereadPolicy: cacheRereadPolicy,
optimisticResult: optimisticResult,
pollInterval: pollInterval,
fetchResults: fetchResults,
eagerlyFetchResults: eagerlyFetchResults,
carryForwardDataOnException: carryForwardDataOnException,
context: context,
parserFn: parserFn,
);

WatchQueryOptions<TParsed> copyWithVariables(
Map<String, dynamic> variables) =>
WatchQueryOptions<TParsed>(
document: document,
operationName: operationName,
variables: variables,
fetchPolicy: fetchPolicy,
errorPolicy: errorPolicy,
cacheRereadPolicy: cacheRereadPolicy,
optimisticResult: optimisticResult,
pollInterval: pollInterval,
fetchResults: fetchResults,
eagerlyFetchResults: eagerlyFetchResults,
carryForwardDataOnException: carryForwardDataOnException,
context: context,
parserFn: parserFn,
);

WatchQueryOptions<TParsed> copyWithOptimisticResult(
Object? optimisticResult) =>
WatchQueryOptions<TParsed>(
document: document,
operationName: operationName,
variables: variables,
Expand All @@ -148,6 +249,7 @@ class WatchQueryOptions<TParsed> extends QueryOptions<TParsed> {
/// it is easy to make mistakes in writing [updateQuery].
///
/// To mitigate this, [FetchMoreOptions.partial] has been provided.
@immutable
class FetchMoreOptions {
FetchMoreOptions({
this.document,
Expand All @@ -171,13 +273,13 @@ class FetchMoreOptions {
updateQuery: partialUpdater(updateQuery),
);

DocumentNode? document;
final DocumentNode? document;

Map<String, dynamic> variables;
final Map<String, dynamic> variables;

/// Strategy for merging the fetchMore result data
/// with the result data already in the cache
UpdateQuery updateQuery;
final UpdateQuery updateQuery;

/// Wrap an [UpdateQuery] in a [deeplyMergeLeft] of the `previousResultData`.
static UpdateQuery partialUpdater(UpdateQuery update) =>
Expand Down
Loading

0 comments on commit 1cc9635

Please # to comment.