Skip to content

Commit

Permalink
feat: move to DocumentNode-only documents
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the deprecated string documents are no longer supported
  • Loading branch information
klavs committed Feb 16, 2020
1 parent 2e491a7 commit 7499323
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 126 deletions.
12 changes: 6 additions & 6 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const String readRepositories = r'''

Then create a `QueryOptions` object:

> **NB:** for `documentNode` - Use our built-in help function - `gql(query)` to convert your document string to **ASTs** `documentNode`.
> **NB:** for `document` - Use our built-in help function - `gql(query)` to convert your document string to **ASTs** `document`.

In our case, we need to pass `nRepositories` variable and the document name is `readRepositories`.

Expand All @@ -119,7 +119,7 @@ In our case, we need to pass `nRepositories` variable and the document name is `
const int nRepositories = 50;
final QueryOptions options = QueryOptions(
documentNode: gql(readRepositories),
document: gql(readRepositories),
variables: <String, dynamic>{
'nRepositories': nRepositories,
},
Expand Down Expand Up @@ -166,7 +166,7 @@ Then instead of the `QueryOptions`, for mutations we will `MutationOptions`, whi
// ...
final MutationOptions options = MutationOptions(
documentNode: gql(addStar),
document: gql(addStar),
variables: <String, dynamic>{
'starrableId': repositoryID,
},
Expand Down Expand Up @@ -201,15 +201,15 @@ if (isStarred) {
### AST documents

> We are deprecating `document` and recommend you update your application to use
`documentNode` instead. `document` will be removed from the api in a future version.
`document` instead. `document` will be removed from the api in a future version.

For example:

```dart
// ...
final MutationOptions options = MutationOptions(
documentNode: gql(addStar),
document: gql(addStar),
variables: <String, dynamic>{
'starrableId': repositoryID,
},
Expand Down Expand Up @@ -238,7 +238,7 @@ import 'package:gql/add_star.ast.g.dart' as add_star;
// ...
final MutationOptions options = MutationOptions(
documentNode: add_star.document,
document: add_star.document,
variables: <String, dynamic>{
'starrableId': repositoryID,
},
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql/example/bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void query() async {
const int nRepositories = 50;

final QueryOptions options = QueryOptions(
documentNode: gql(readRepositories),
document: gql(readRepositories),
variables: <String, dynamic>{
'nRepositories': nRepositories,
},
Expand Down Expand Up @@ -70,7 +70,7 @@ void starRepository(String repositoryID) async {
final GraphQLClient _client = client();

final MutationOptions options = MutationOptions(
documentNode: gql(addStar),
document: gql(addStar),
variables: <String, dynamic>{
'starrableId': repositoryID,
},
Expand Down Expand Up @@ -103,7 +103,7 @@ void removeStarFromRepository(String repositoryID) async {
final GraphQLClient _client = client();

final MutationOptions options = MutationOptions(
documentNode: gql(removeStar),
document: gql(removeStar),
variables: <String, dynamic>{
'starrableId': repositoryID,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/lib/src/core/observable_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class ObservableQuery {
final combinedOptions = QueryOptions(
fetchPolicy: FetchPolicy.noCache,
errorPolicy: options.errorPolicy,
documentNode: fetchMoreOptions.documentNode ?? options.documentNode,
document: fetchMoreOptions.document ?? options.document,
context: options.context,
variables: {
...options.variables,
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/lib/src/core/query_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class QueryManager {
// create a new request to execute
final Request request = Request(
operation: Operation(
document: options.documentNode,
document: options.document,
operationName: options.operationName,
),
variables: options.variables,
Expand Down
67 changes: 11 additions & 56 deletions packages/graphql/lib/src/core/query_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,21 @@ class Policies {
overrides?.fetch ?? fetch,
overrides?.error ?? error,
);

operator ==(Object other) =>
other is Policies && fetch == other.fetch && error == other.error;
}

/// Base options.
class BaseOptions extends RawOperationData {
BaseOptions({
@Deprecated('The "document" option has been deprecated, use "documentNode" instead')
String document,
DocumentNode documentNode,
@required DocumentNode document,
Map<String, dynamic> variables,
this.policies,
this.context,
this.optimisticResult,
}) : super(
// ignore: deprecated_member_use_from_same_package
document: document,
documentNode: documentNode,
variables: variables,
);

Expand All @@ -108,9 +105,7 @@ class BaseOptions extends RawOperationData {
/// Query options.
class QueryOptions extends BaseOptions {
QueryOptions({
@Deprecated('The "document" option has been deprecated, use "documentNode" instead')
String document,
DocumentNode documentNode,
@required DocumentNode document,
Map<String, dynamic> variables,
FetchPolicy fetchPolicy,
ErrorPolicy errorPolicy,
Expand All @@ -119,9 +114,7 @@ class QueryOptions extends BaseOptions {
Context context,
}) : super(
policies: Policies(fetch: fetchPolicy, error: errorPolicy),
// ignore: deprecated_member_use_from_same_package
document: document,
documentNode: documentNode,
variables: variables,
context: context,
optimisticResult: optimisticResult,
Expand All @@ -139,9 +132,7 @@ typedef OnError = void Function(OperationException error);
/// Mutation options
class MutationOptions extends BaseOptions {
MutationOptions({
@Deprecated('The "document" option has been deprecated, use "documentNode" instead')
String document,
DocumentNode documentNode,
@required DocumentNode document,
Map<String, dynamic> variables,
FetchPolicy fetchPolicy,
ErrorPolicy errorPolicy,
Expand All @@ -151,9 +142,7 @@ class MutationOptions extends BaseOptions {
this.onError,
}) : super(
policies: Policies(fetch: fetchPolicy, error: errorPolicy),
// ignore: deprecated_member_use_from_same_package
document: document,
documentNode: documentNode,
variables: variables,
context: context,
);
Expand Down Expand Up @@ -253,9 +242,7 @@ class MutationCallbacks {
// ObservableQuery options
class WatchQueryOptions extends QueryOptions {
WatchQueryOptions({
@Deprecated('The "document" option has been deprecated, use "documentNode" instead')
String document,
DocumentNode documentNode,
@required DocumentNode document,
Map<String, dynamic> variables,
FetchPolicy fetchPolicy,
ErrorPolicy errorPolicy,
Expand All @@ -265,9 +252,7 @@ class WatchQueryOptions extends QueryOptions {
this.eagerlyFetchResults,
Context context,
}) : super(
// ignore: deprecated_member_use_from_same_package
document: document,
documentNode: documentNode,
variables: variables,
fetchPolicy: fetchPolicy,
errorPolicy: errorPolicy,
Expand All @@ -292,7 +277,7 @@ class WatchQueryOptions extends QueryOptions {
WatchQueryOptions a,
WatchQueryOptions b,
) {
if (a.documentNode != b.documentNode) {
if (a.document != b.document) {
return true;
}

Expand All @@ -313,7 +298,7 @@ class WatchQueryOptions extends QueryOptions {
}

WatchQueryOptions copy() => WatchQueryOptions(
documentNode: documentNode,
document: document,
variables: variables,
fetchPolicy: fetchPolicy,
errorPolicy: errorPolicy,
Expand All @@ -334,46 +319,16 @@ typedef dynamic UpdateQuery(
/// options for fetchmore operations
class FetchMoreOptions {
FetchMoreOptions({
@Deprecated('The "document" option has been deprecated, use "documentNode" instead')
String document,
DocumentNode documentNode,
@required this.document,
this.variables = const <String, dynamic>{},
@required this.updateQuery,
}) : assert(
// ignore: deprecated_member_use_from_same_package
_mutuallyExclusive(document, documentNode),
'"document" or "documentNode" options are mutually exclusive.',
),
assert(updateQuery != null),
this.documentNode =
// ignore: deprecated_member_use_from_same_package
documentNode ?? document != null ? parseString(document) : null;

DocumentNode documentNode;

/// A string representation of [documentNode]
@Deprecated(
'The "document" option has been deprecated, use "documentNode" instead')
String get document => printNode(documentNode);

@Deprecated(
'The "document" option has been deprecated, use "documentNode" instead')
set document(value) {
documentNode = parseString(value);
}
}) : assert(updateQuery != null);

DocumentNode document;

final Map<String, dynamic> variables;

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

bool _mutuallyExclusive(
Object a,
Object b, {
bool required = false,
}) =>
(!required && a == null && b == null) ||
(a != null && b == null) ||
(a == null && b != null);
42 changes: 6 additions & 36 deletions packages/graphql/lib/src/core/raw_operation_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,21 @@ import 'dart:collection' show SplayTreeMap;
import 'dart:convert' show json;

import 'package:gql/ast.dart';
import 'package:gql/language.dart';
import 'package:graphql/src/utilities/get_from_ast.dart';
import 'package:meta/meta.dart';

class RawOperationData {
RawOperationData({
@Deprecated('The "document" option has been deprecated, use "documentNode" instead')
String document,
DocumentNode documentNode,
@required this.document,
Map<String, dynamic> variables,
String operationName,
}) : assert(
// ignore: deprecated_member_use_from_same_package
document != null || documentNode != null,
'Either a "document" or "documentNode" option is required. '
'You must specify your GraphQL document in the query options.',
),
// todo: Investigate why this assertion is failing
// assert(
// (document != null && documentNode == null) ||
// (document == null && documentNode != null),
// '"document" or "documentNode" options are mutually exclusive.',
// ),
// ignore: deprecated_member_use_from_same_package
documentNode = documentNode ?? parseString(document),
_operationName = operationName,
}) : _operationName = operationName,
variables = SplayTreeMap<String, dynamic>.of(
variables ?? const <String, dynamic>{},
);

/// A GraphQL document that consists of a single query to be sent down to the server.
DocumentNode documentNode;

/// A string representation of [documentNode]
@Deprecated(
'The "document" option has been deprecated, use "documentNode" instead',
)
String get document => printNode(documentNode);

@Deprecated(
'The "document" option has been deprecated, use "documentNode" instead',
)
set document(value) {
documentNode = parseString(value);
}
DocumentNode document;

/// A map going from variable name to variable value, where the variables are used
/// within the GraphQL query.
Expand All @@ -55,7 +26,7 @@ class RawOperationData {

/// The last operation name appearing in the contained document.
String get operationName {
_operationName ??= getLastOperationName(documentNode);
_operationName ??= getLastOperationName(document);
return _operationName;
}

Expand All @@ -65,7 +36,7 @@ class RawOperationData {
// TODO remove $document from key? A bit redundant, though that's not the worst thing
String get _identifier {
_documentIdentifier ??=
operationName ?? 'UNNAMED/' + documentNode.hashCode.toString();
operationName ?? 'UNNAMED/' + document.hashCode.toString();
return _documentIdentifier;
}

Expand All @@ -81,7 +52,6 @@ class RawOperationData {
);

// TODO: document is being depracated, find ways for generating key
// ignore: deprecated_member_use_from_same_package
return '$document|$encodedVariables|$_identifier';
}
}
4 changes: 2 additions & 2 deletions packages/graphql/test/anonymous_operations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void main() {
group('query', () {
test('successful query', () async {
final WatchQueryOptions _options = WatchQueryOptions(
documentNode: parseString(readRepositories),
document: parseString(readRepositories),
variables: <String, dynamic>{},
);

Expand Down Expand Up @@ -129,7 +129,7 @@ void main() {
group('mutation', () {
test('successful mutation', () async {
final MutationOptions _options = MutationOptions(
documentNode: parseString(addStar),
document: parseString(addStar),
);

when(
Expand Down
Loading

0 comments on commit 7499323

Please sign in to comment.