From 77ba81ae5f01e795ab2bf248d7f2fee818b00a0f Mon Sep 17 00:00:00 2001 From: smkhalsa Date: Wed, 12 Feb 2020 13:56:47 -0800 Subject: [PATCH] use built_value for options --- build.yaml | 4 + lib/src/cache/cache.dart | 19 +- lib/src/cache/cache_proxy.dart | 4 +- lib/src/cache/options.dart | 161 +++++---- lib/src/cache/options.g.dart | 601 +++++++++++++++++++++++++++++++++ lib/src/client/client.dart | 18 +- pubspec.yaml | 14 +- 7 files changed, 732 insertions(+), 89 deletions(-) create mode 100644 lib/src/cache/options.g.dart diff --git a/build.yaml b/build.yaml index 0bf124de..de35fd80 100644 --- a/build.yaml +++ b/build.yaml @@ -1,3 +1,7 @@ +targets: + $default: + sources: + - lib/** builders: req_builder: import: "package:gql_client/src/req_builder/req_builder.dart" diff --git a/lib/src/cache/cache.dart b/lib/src/cache/cache.dart index 63e70b2d..40bb8a47 100644 --- a/lib/src/cache/cache.dart +++ b/lib/src/cache/cache.dart @@ -9,6 +9,7 @@ import '../store/store.dart'; class GQLCache { final Map typePolicies; + final bool addTypename; final Store _dataStore; final BehaviorSubject>>> @@ -19,6 +20,7 @@ class GQLCache { GQLCache({ Store dataStore, this.typePolicies = const {}, + this.addTypename = true, Map>> seedOptimisticPatches, }) : _dataStore = dataStore ?? MemoryStore(), _optimisticPatchesStream = BehaviorSubject.seeded( @@ -39,13 +41,16 @@ class GQLCache { } Stream> watchQuery( - WatchQueryOptions options, + ReadQueryOptions options, ) => - (options.optimistic ? _optimisticDataStream : _dataStore.watch()).map( + ((options.optimistic ?? true) + ? _optimisticDataStream + : _dataStore.watch()) + .map( (data) => denormalize( reader: (dataId) => data[dataId], query: options.document, - addTypename: options.addTypename, + addTypename: addTypename, operationName: options.operationName, variables: options.variables, typePolicies: typePolicies, @@ -56,21 +61,21 @@ class GQLCache { ReadQueryOptions options, ) => denormalize( - reader: (dataId) => options.optimistic + reader: (dataId) => (options.optimistic ?? true) ? _optimisticDataStream.value[dataId] : _dataStore.get(dataId), query: options.document, - addTypename: options.addTypename, operationName: options.operationName, variables: options.variables, typePolicies: typePolicies, + addTypename: addTypename, ); Map readFragment( ReadFragmentOptions options, ) => denormalizeFragment( - reader: (dataId) => options.optimistic + reader: (dataId) => (options.optimistic ?? true) ? _optimisticDataStream.value[dataId] : _dataStore.get(dataId), fragment: options.fragment, @@ -78,7 +83,7 @@ class GQLCache { fragmentName: options.fragmentName, variables: options.variables, typePolicies: typePolicies, - addTypename: options.addTypename, + addTypename: addTypename, ); void writeQuery( diff --git a/lib/src/cache/cache_proxy.dart b/lib/src/cache/cache_proxy.dart index 594d266b..8dd92d3e 100644 --- a/lib/src/cache/cache_proxy.dart +++ b/lib/src/cache/cache_proxy.dart @@ -17,12 +17,12 @@ class CacheProxy { Map readQuery( ReadQueryOptions options, ) => - _cache.readQuery(options); + _cache.readQuery(options.rebuild((b) => b..optimistic ??= false)); Map readFragment( ReadFragmentOptions options, ) => - _cache.readFragment(options); + _cache.readFragment(options.rebuild((b) => b..optimistic ??= false)); void writeQuery( WriteQueryOptions options, diff --git a/lib/src/cache/options.dart b/lib/src/cache/options.dart index 08a9f791..e0a253c9 100644 --- a/lib/src/cache/options.dart +++ b/lib/src/cache/options.dart @@ -1,84 +1,103 @@ -import 'package:meta/meta.dart'; import 'package:gql/ast.dart'; +import 'package:built_value/built_value.dart'; export 'package:normalize/normalize.dart' show TypePolicy, AddTypenameVisitor; -class WatchQueryOptions { - final DocumentNode document; - final String operationName; - final Map variables; - final bool optimistic; - final bool addTypename; - - const WatchQueryOptions({ - @required this.document, - this.operationName, - this.variables, - this.optimistic = true, - this.addTypename = true, - }); +part 'options.g.dart'; + +abstract class BaseQueryOptions { + DocumentNode get document; + String get operationName; + Map get variables; } -class ReadQueryOptions { - final DocumentNode document; - final String operationName; - final Map variables; - final bool addTypename; - final bool optimistic; - - const ReadQueryOptions({ - @required this.document, - this.operationName, - this.variables, - this.addTypename = true, - this.optimistic = true, - }); +abstract class ReadQueryOptions + implements + Built, + BaseQueryOptions { + @nullable + bool get optimistic; + + DocumentNode get document; + + @nullable + String get operationName; + + @nullable + Map get variables; + + ReadQueryOptions._(); + factory ReadQueryOptions([void Function(ReadQueryOptionsBuilder) updates]) = + _$ReadQueryOptions; } -class ReadFragmentOptions { - final DocumentNode fragment; - final Map idFields; - final String fragmentName; - final Map variables; - final bool addTypename; - final bool optimistic; - - const ReadFragmentOptions({ - @required this.fragment, - @required this.idFields, - this.fragmentName, - this.variables, - this.addTypename = true, - this.optimistic = true, - }); +abstract class WriteQueryOptions + implements + Built, + BaseQueryOptions { + Map get data; + + DocumentNode get document; + + @nullable + String get operationName; + + @nullable + Map get variables; + + WriteQueryOptions._(); + factory WriteQueryOptions([void Function(WriteQueryOptionsBuilder) updates]) = + _$WriteQueryOptions; } -class WriteQueryOptions { - final DocumentNode document; - final Map data; - final String operationName; - final Map variables; - - const WriteQueryOptions({ - @required this.document, - @required this.data, - this.operationName, - this.variables, - }); +abstract class BaseFragmentOptions { + DocumentNode get fragment; + Map get idFields; + String get fragmentName; + Map get variables; } -class WriteFragmentOptions { - final DocumentNode fragment; - final Map idFields; - final Map data; - final String fragmentName; - final Map variables; - - const WriteFragmentOptions({ - @required this.fragment, - @required this.idFields, - @required this.data, - this.fragmentName, - this.variables, - }); +abstract class ReadFragmentOptions + implements + Built, + BaseQueryOptions { + @nullable + bool get optimistic; + + DocumentNode get fragment; + + Map get idFields; + + @nullable + String get fragmentName; + + @nullable + Map get variables; + + ReadFragmentOptions._(); + factory ReadFragmentOptions( + [void Function(ReadFragmentOptionsBuilder) updates]) = + _$ReadFragmentOptions; +} + +abstract class WriteFragmentOptions + implements + Built, + BaseQueryOptions { + Map get data; + + DocumentNode get fragment; + + Map get idFields; + + @nullable + String get fragmentName; + + @nullable + Map get variables; + + WriteFragmentOptions._(); + factory WriteFragmentOptions( + [void Function(WriteFragmentOptionsBuilder) updates]) = + _$WriteFragmentOptions; } diff --git a/lib/src/cache/options.g.dart b/lib/src/cache/options.g.dart new file mode 100644 index 00000000..e152eda9 --- /dev/null +++ b/lib/src/cache/options.g.dart @@ -0,0 +1,601 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'options.dart'; + +// ************************************************************************** +// BuiltValueGenerator +// ************************************************************************** + +class _$ReadQueryOptions extends ReadQueryOptions { + @override + final bool optimistic; + @override + final DocumentNode document; + @override + final String operationName; + @override + final Map variables; + + factory _$ReadQueryOptions( + [void Function(ReadQueryOptionsBuilder) updates]) => + (new ReadQueryOptionsBuilder()..update(updates)).build(); + + _$ReadQueryOptions._( + {this.optimistic, this.document, this.operationName, this.variables}) + : super._() { + if (document == null) { + throw new BuiltValueNullFieldError('ReadQueryOptions', 'document'); + } + } + + @override + ReadQueryOptions rebuild(void Function(ReadQueryOptionsBuilder) updates) => + (toBuilder()..update(updates)).build(); + + @override + ReadQueryOptionsBuilder toBuilder() => + new ReadQueryOptionsBuilder()..replace(this); + + @override + bool operator ==(Object other) { + if (identical(other, this)) return true; + return other is ReadQueryOptions && + optimistic == other.optimistic && + document == other.document && + operationName == other.operationName && + variables == other.variables; + } + + @override + int get hashCode { + return $jf($jc( + $jc($jc($jc(0, optimistic.hashCode), document.hashCode), + operationName.hashCode), + variables.hashCode)); + } + + @override + String toString() { + return (newBuiltValueToStringHelper('ReadQueryOptions') + ..add('optimistic', optimistic) + ..add('document', document) + ..add('operationName', operationName) + ..add('variables', variables)) + .toString(); + } +} + +class ReadQueryOptionsBuilder + implements Builder { + _$ReadQueryOptions _$v; + + bool _optimistic; + bool get optimistic => _$this._optimistic; + set optimistic(bool optimistic) => _$this._optimistic = optimistic; + + DocumentNode _document; + DocumentNode get document => _$this._document; + set document(DocumentNode document) => _$this._document = document; + + String _operationName; + String get operationName => _$this._operationName; + set operationName(String operationName) => + _$this._operationName = operationName; + + Map _variables; + Map get variables => _$this._variables; + set variables(Map variables) => + _$this._variables = variables; + + ReadQueryOptionsBuilder(); + + ReadQueryOptionsBuilder get _$this { + if (_$v != null) { + _optimistic = _$v.optimistic; + _document = _$v.document; + _operationName = _$v.operationName; + _variables = _$v.variables; + _$v = null; + } + return this; + } + + @override + void replace(ReadQueryOptions other) { + if (other == null) { + throw new ArgumentError.notNull('other'); + } + _$v = other as _$ReadQueryOptions; + } + + @override + void update(void Function(ReadQueryOptionsBuilder) updates) { + if (updates != null) updates(this); + } + + @override + _$ReadQueryOptions build() { + final _$result = _$v ?? + new _$ReadQueryOptions._( + optimistic: optimistic, + document: document, + operationName: operationName, + variables: variables); + replace(_$result); + return _$result; + } +} + +class _$WriteQueryOptions extends WriteQueryOptions { + @override + final Map data; + @override + final DocumentNode document; + @override + final String operationName; + @override + final Map variables; + + factory _$WriteQueryOptions( + [void Function(WriteQueryOptionsBuilder) updates]) => + (new WriteQueryOptionsBuilder()..update(updates)).build(); + + _$WriteQueryOptions._( + {this.data, this.document, this.operationName, this.variables}) + : super._() { + if (data == null) { + throw new BuiltValueNullFieldError('WriteQueryOptions', 'data'); + } + if (document == null) { + throw new BuiltValueNullFieldError('WriteQueryOptions', 'document'); + } + } + + @override + WriteQueryOptions rebuild(void Function(WriteQueryOptionsBuilder) updates) => + (toBuilder()..update(updates)).build(); + + @override + WriteQueryOptionsBuilder toBuilder() => + new WriteQueryOptionsBuilder()..replace(this); + + @override + bool operator ==(Object other) { + if (identical(other, this)) return true; + return other is WriteQueryOptions && + data == other.data && + document == other.document && + operationName == other.operationName && + variables == other.variables; + } + + @override + int get hashCode { + return $jf($jc( + $jc($jc($jc(0, data.hashCode), document.hashCode), + operationName.hashCode), + variables.hashCode)); + } + + @override + String toString() { + return (newBuiltValueToStringHelper('WriteQueryOptions') + ..add('data', data) + ..add('document', document) + ..add('operationName', operationName) + ..add('variables', variables)) + .toString(); + } +} + +class WriteQueryOptionsBuilder + implements Builder { + _$WriteQueryOptions _$v; + + Map _data; + Map get data => _$this._data; + set data(Map data) => _$this._data = data; + + DocumentNode _document; + DocumentNode get document => _$this._document; + set document(DocumentNode document) => _$this._document = document; + + String _operationName; + String get operationName => _$this._operationName; + set operationName(String operationName) => + _$this._operationName = operationName; + + Map _variables; + Map get variables => _$this._variables; + set variables(Map variables) => + _$this._variables = variables; + + WriteQueryOptionsBuilder(); + + WriteQueryOptionsBuilder get _$this { + if (_$v != null) { + _data = _$v.data; + _document = _$v.document; + _operationName = _$v.operationName; + _variables = _$v.variables; + _$v = null; + } + return this; + } + + @override + void replace(WriteQueryOptions other) { + if (other == null) { + throw new ArgumentError.notNull('other'); + } + _$v = other as _$WriteQueryOptions; + } + + @override + void update(void Function(WriteQueryOptionsBuilder) updates) { + if (updates != null) updates(this); + } + + @override + _$WriteQueryOptions build() { + final _$result = _$v ?? + new _$WriteQueryOptions._( + data: data, + document: document, + operationName: operationName, + variables: variables); + replace(_$result); + return _$result; + } +} + +class _$ReadFragmentOptions extends ReadFragmentOptions { + @override + final bool optimistic; + @override + final DocumentNode fragment; + @override + final Map idFields; + @override + final String fragmentName; + @override + final Map variables; + @override + final DocumentNode document; + @override + final String operationName; + + factory _$ReadFragmentOptions( + [void Function(ReadFragmentOptionsBuilder) updates]) => + (new ReadFragmentOptionsBuilder()..update(updates)).build(); + + _$ReadFragmentOptions._( + {this.optimistic, + this.fragment, + this.idFields, + this.fragmentName, + this.variables, + this.document, + this.operationName}) + : super._() { + if (fragment == null) { + throw new BuiltValueNullFieldError('ReadFragmentOptions', 'fragment'); + } + if (idFields == null) { + throw new BuiltValueNullFieldError('ReadFragmentOptions', 'idFields'); + } + if (document == null) { + throw new BuiltValueNullFieldError('ReadFragmentOptions', 'document'); + } + if (operationName == null) { + throw new BuiltValueNullFieldError( + 'ReadFragmentOptions', 'operationName'); + } + } + + @override + ReadFragmentOptions rebuild( + void Function(ReadFragmentOptionsBuilder) updates) => + (toBuilder()..update(updates)).build(); + + @override + ReadFragmentOptionsBuilder toBuilder() => + new ReadFragmentOptionsBuilder()..replace(this); + + @override + bool operator ==(Object other) { + if (identical(other, this)) return true; + return other is ReadFragmentOptions && + optimistic == other.optimistic && + fragment == other.fragment && + idFields == other.idFields && + fragmentName == other.fragmentName && + variables == other.variables && + document == other.document && + operationName == other.operationName; + } + + @override + int get hashCode { + return $jf($jc( + $jc( + $jc( + $jc( + $jc($jc($jc(0, optimistic.hashCode), fragment.hashCode), + idFields.hashCode), + fragmentName.hashCode), + variables.hashCode), + document.hashCode), + operationName.hashCode)); + } + + @override + String toString() { + return (newBuiltValueToStringHelper('ReadFragmentOptions') + ..add('optimistic', optimistic) + ..add('fragment', fragment) + ..add('idFields', idFields) + ..add('fragmentName', fragmentName) + ..add('variables', variables) + ..add('document', document) + ..add('operationName', operationName)) + .toString(); + } +} + +class ReadFragmentOptionsBuilder + implements Builder { + _$ReadFragmentOptions _$v; + + bool _optimistic; + bool get optimistic => _$this._optimistic; + set optimistic(bool optimistic) => _$this._optimistic = optimistic; + + DocumentNode _fragment; + DocumentNode get fragment => _$this._fragment; + set fragment(DocumentNode fragment) => _$this._fragment = fragment; + + Map _idFields; + Map get idFields => _$this._idFields; + set idFields(Map idFields) => _$this._idFields = idFields; + + String _fragmentName; + String get fragmentName => _$this._fragmentName; + set fragmentName(String fragmentName) => _$this._fragmentName = fragmentName; + + Map _variables; + Map get variables => _$this._variables; + set variables(Map variables) => + _$this._variables = variables; + + DocumentNode _document; + DocumentNode get document => _$this._document; + set document(DocumentNode document) => _$this._document = document; + + String _operationName; + String get operationName => _$this._operationName; + set operationName(String operationName) => + _$this._operationName = operationName; + + ReadFragmentOptionsBuilder(); + + ReadFragmentOptionsBuilder get _$this { + if (_$v != null) { + _optimistic = _$v.optimistic; + _fragment = _$v.fragment; + _idFields = _$v.idFields; + _fragmentName = _$v.fragmentName; + _variables = _$v.variables; + _document = _$v.document; + _operationName = _$v.operationName; + _$v = null; + } + return this; + } + + @override + void replace(ReadFragmentOptions other) { + if (other == null) { + throw new ArgumentError.notNull('other'); + } + _$v = other as _$ReadFragmentOptions; + } + + @override + void update(void Function(ReadFragmentOptionsBuilder) updates) { + if (updates != null) updates(this); + } + + @override + _$ReadFragmentOptions build() { + final _$result = _$v ?? + new _$ReadFragmentOptions._( + optimistic: optimistic, + fragment: fragment, + idFields: idFields, + fragmentName: fragmentName, + variables: variables, + document: document, + operationName: operationName); + replace(_$result); + return _$result; + } +} + +class _$WriteFragmentOptions extends WriteFragmentOptions { + @override + final Map data; + @override + final DocumentNode fragment; + @override + final Map idFields; + @override + final String fragmentName; + @override + final Map variables; + @override + final DocumentNode document; + @override + final String operationName; + + factory _$WriteFragmentOptions( + [void Function(WriteFragmentOptionsBuilder) updates]) => + (new WriteFragmentOptionsBuilder()..update(updates)).build(); + + _$WriteFragmentOptions._( + {this.data, + this.fragment, + this.idFields, + this.fragmentName, + this.variables, + this.document, + this.operationName}) + : super._() { + if (data == null) { + throw new BuiltValueNullFieldError('WriteFragmentOptions', 'data'); + } + if (fragment == null) { + throw new BuiltValueNullFieldError('WriteFragmentOptions', 'fragment'); + } + if (idFields == null) { + throw new BuiltValueNullFieldError('WriteFragmentOptions', 'idFields'); + } + if (document == null) { + throw new BuiltValueNullFieldError('WriteFragmentOptions', 'document'); + } + if (operationName == null) { + throw new BuiltValueNullFieldError( + 'WriteFragmentOptions', 'operationName'); + } + } + + @override + WriteFragmentOptions rebuild( + void Function(WriteFragmentOptionsBuilder) updates) => + (toBuilder()..update(updates)).build(); + + @override + WriteFragmentOptionsBuilder toBuilder() => + new WriteFragmentOptionsBuilder()..replace(this); + + @override + bool operator ==(Object other) { + if (identical(other, this)) return true; + return other is WriteFragmentOptions && + data == other.data && + fragment == other.fragment && + idFields == other.idFields && + fragmentName == other.fragmentName && + variables == other.variables && + document == other.document && + operationName == other.operationName; + } + + @override + int get hashCode { + return $jf($jc( + $jc( + $jc( + $jc( + $jc($jc($jc(0, data.hashCode), fragment.hashCode), + idFields.hashCode), + fragmentName.hashCode), + variables.hashCode), + document.hashCode), + operationName.hashCode)); + } + + @override + String toString() { + return (newBuiltValueToStringHelper('WriteFragmentOptions') + ..add('data', data) + ..add('fragment', fragment) + ..add('idFields', idFields) + ..add('fragmentName', fragmentName) + ..add('variables', variables) + ..add('document', document) + ..add('operationName', operationName)) + .toString(); + } +} + +class WriteFragmentOptionsBuilder + implements Builder { + _$WriteFragmentOptions _$v; + + Map _data; + Map get data => _$this._data; + set data(Map data) => _$this._data = data; + + DocumentNode _fragment; + DocumentNode get fragment => _$this._fragment; + set fragment(DocumentNode fragment) => _$this._fragment = fragment; + + Map _idFields; + Map get idFields => _$this._idFields; + set idFields(Map idFields) => _$this._idFields = idFields; + + String _fragmentName; + String get fragmentName => _$this._fragmentName; + set fragmentName(String fragmentName) => _$this._fragmentName = fragmentName; + + Map _variables; + Map get variables => _$this._variables; + set variables(Map variables) => + _$this._variables = variables; + + DocumentNode _document; + DocumentNode get document => _$this._document; + set document(DocumentNode document) => _$this._document = document; + + String _operationName; + String get operationName => _$this._operationName; + set operationName(String operationName) => + _$this._operationName = operationName; + + WriteFragmentOptionsBuilder(); + + WriteFragmentOptionsBuilder get _$this { + if (_$v != null) { + _data = _$v.data; + _fragment = _$v.fragment; + _idFields = _$v.idFields; + _fragmentName = _$v.fragmentName; + _variables = _$v.variables; + _document = _$v.document; + _operationName = _$v.operationName; + _$v = null; + } + return this; + } + + @override + void replace(WriteFragmentOptions other) { + if (other == null) { + throw new ArgumentError.notNull('other'); + } + _$v = other as _$WriteFragmentOptions; + } + + @override + void update(void Function(WriteFragmentOptionsBuilder) updates) { + if (updates != null) updates(this); + } + + @override + _$WriteFragmentOptions build() { + final _$result = _$v ?? + new _$WriteFragmentOptions._( + data: data, + fragment: fragment, + idFields: idFields, + fragmentName: fragmentName, + variables: variables, + document: document, + operationName: operationName); + replace(_$result); + return _$result; + } +} + +// ignore_for_file: always_put_control_body_on_new_line,always_specify_types,annotate_overrides,avoid_annotating_with_dynamic,avoid_as,avoid_catches_without_on_clauses,avoid_returning_this,lines_longer_than_80_chars,omit_local_variable_types,prefer_expression_function_bodies,sort_constructors_first,test_types_in_equals,unnecessary_const,unnecessary_new diff --git a/lib/src/client/client.dart b/lib/src/client/client.dart index 366fc9b7..a638f36e 100644 --- a/lib/src/client/client.dart +++ b/lib/src/client/client.dart @@ -166,10 +166,11 @@ class GQLClient { Stream> _cacheResponseStream( QueryRequest queryRequest) => cache - .watchQuery(WatchQueryOptions( - document: queryRequest.operation.document, - operationName: queryRequest.operation.operationName, - variables: queryRequest.variables, + .watchQuery(ReadQueryOptions( + (b) => b + ..document = queryRequest.operation.document + ..operationName = queryRequest.operation.operationName + ..variables = queryRequest.variables, )) .map( (data) => QueryResponse( @@ -185,10 +186,11 @@ class GQLClient { if (response.data != null) cache.writeQuery( WriteQueryOptions( - document: response.queryRequest.operation.document, - operationName: response.queryRequest.operation.operationName, - variables: response.queryRequest.variables, - data: response.data?.data, + (b) => b + ..document = response.queryRequest.operation.document + ..operationName = response.queryRequest.operation.operationName + ..variables = response.queryRequest.variables + ..data = response.data?.data, ), response.optimistic, response.queryRequest.queryId, diff --git a/pubspec.yaml b/pubspec.yaml index 9199c0f3..7e1c61f8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,8 +18,9 @@ dependencies: gql: ^0.12.0 gql_link: ^0.2.2 gql_exec: ^0.2.0 - gql_build: ^0.0.2 + gql_build: ^0.0.3 built_collection: ^4.3.2 + built_value: ^7.0.9 code_builder: ^3.2.1 build: ^1.2.2 @@ -28,3 +29,14 @@ dev_dependencies: mockito: ^4.1.1 build_runner: ^1.6.7 build_test: ^0.10.12+1 + built_value_generator: ^7.0.9 + +dependency_overrides: + gql_build: + git: + url: git://github.com/gql-dart/gql.git + path: gql_build + gql_code_builder: + git: + url: git://github.com/gql-dart/gql.git + path: gql_code_builder