-
-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(client): Fragment and FragmentRequest for more normalized api
- Loading branch information
Showing
7 changed files
with
195 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import "package:collection/collection.dart"; | ||
import "package:gql/ast.dart"; | ||
import 'package:graphql/client.dart'; | ||
import "package:meta/meta.dart"; | ||
|
||
/// A fragment in a [document], optionally defined by [fragmentName] | ||
@immutable | ||
class Fragment { | ||
/// Document containing at least one [FragmentDefinitionNode] | ||
final DocumentNode document; | ||
|
||
/// Name of the fragment definition | ||
/// | ||
/// Must be specified if [document] contains more than one [FragmentDefinitionNode] | ||
final String fragmentName; | ||
|
||
const Fragment({ | ||
@required this.document, | ||
this.fragmentName, | ||
}) : assert(document != null); | ||
|
||
List<Object> _getChildren() => [ | ||
document, | ||
fragmentName, | ||
]; | ||
|
||
@override | ||
bool operator ==(Object o) => | ||
identical(this, o) || | ||
(o is Fragment && | ||
const ListEquality<Object>( | ||
DeepCollectionEquality(), | ||
).equals( | ||
o._getChildren(), | ||
_getChildren(), | ||
)); | ||
|
||
@override | ||
int get hashCode => const ListEquality<Object>( | ||
DeepCollectionEquality(), | ||
).hash( | ||
_getChildren(), | ||
); | ||
|
||
@override | ||
String toString() => | ||
"Fragment(document: $document, fragmentName: $fragmentName)"; | ||
|
||
/// helper for building a [FragmentRequest] | ||
@experimental | ||
FragmentRequest asRequest({ | ||
@required Map<String, dynamic> idFields, | ||
Map<String, dynamic> variables = const <String, dynamic>{}, | ||
}) => | ||
FragmentRequest(fragment: this, idFields: idFields, variables: variables); | ||
} | ||
|
||
/// Cache access request of [fragment] with [variables]. | ||
@immutable | ||
class FragmentRequest { | ||
/// [Fragment] to be read or written | ||
final Fragment fragment; | ||
|
||
/// Variables of the fragment for this request | ||
final Map<String, dynamic> variables; | ||
|
||
/// Map which includes all identifying data (usually `{__typename, id }`) | ||
final Map<String, dynamic> idFields; | ||
|
||
const FragmentRequest({ | ||
@required this.fragment, | ||
@required this.idFields, | ||
this.variables = const <String, dynamic>{}, | ||
}) : assert(fragment != null), | ||
assert(idFields != null); | ||
|
||
List<Object> _getChildren() => [ | ||
fragment, | ||
variables, | ||
idFields, | ||
]; | ||
|
||
@override | ||
bool operator ==(Object o) => | ||
identical(this, o) || | ||
(o is FragmentRequest && | ||
const ListEquality<Object>( | ||
DeepCollectionEquality(), | ||
).equals( | ||
o._getChildren(), | ||
_getChildren(), | ||
)); | ||
|
||
@override | ||
int get hashCode => const ListEquality<Object>( | ||
DeepCollectionEquality(), | ||
).hash( | ||
_getChildren(), | ||
); | ||
|
||
@override | ||
String toString() => | ||
"FragmentRequest(fragment: $fragment, variables: $variables)"; | ||
} | ||
|
||
extension OperationRequestHelper on Operation { | ||
/// helper for building a [Request] | ||
@experimental | ||
Request asRequest({ | ||
Map<String, dynamic> variables = const <String, dynamic>{}, | ||
}) => | ||
Request(operation: this, variables: variables); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.