Skip to content

Commit

Permalink
feat(graphql): upgrade normalize to 0.4.2
Browse files Browse the repository at this point in the history
Should also fix some implicit issues with recordOptimisticTransaction
and type policies
  • Loading branch information
micimize committed Oct 12, 2020
1 parent 45cac17 commit 4655e7d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 28 deletions.
45 changes: 26 additions & 19 deletions packages/graphql/lib/src/cache/_normalizing_data_proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ typedef DataIdResolver = String Function(Map<String, Object> object);
/// [readNormalized] and [writeNormalized] must still be supplied by the implementing class
abstract class NormalizingDataProxy extends GraphQLDataProxy {
/// `typePolicies` to pass down to `normalize`
Map<String, TypePolicy> typePolicies;
Map<String, TypePolicy> get typePolicies;

/// Optional `dataIdFromObject` function to pass through to [normalize]
DataIdResolver get dataIdFromObject;

/// Whether to add `__typename` automatically.
///
Expand All @@ -37,9 +40,6 @@ abstract class NormalizingDataProxy extends GraphQLDataProxy {
@visibleForTesting
bool broadcastRequested = false;

/// Optional `dataIdFromObject` function to pass through to [normalize]
DataIdResolver dataIdFromObject;

/// Read normaized data from the cache
///
/// Called from [readQuery] and [readFragment], which handle denormalization.
Expand All @@ -64,14 +64,16 @@ abstract class NormalizingDataProxy extends GraphQLDataProxy {
Request request, {
bool optimistic = true,
}) =>
denormalize(
reader: (dataId) => readNormalized(dataId, optimistic: optimistic),
query: request.operation.document,
operationName: request.operation.operationName,
variables: sanitizeVariables(request.variables),
denormalizeOperation(
// provided from cache
read: (dataId) => readNormalized(dataId, optimistic: optimistic),
typePolicies: typePolicies,
addTypename: addTypename ?? false,
returnPartialData: returnPartialData,
addTypename: addTypename ?? false,
// provided from request
document: request.operation.document,
operationName: request.operation.operationName,
variables: sanitizeVariables(request.variables),
);

Map<String, dynamic> readFragment(
Expand All @@ -80,13 +82,13 @@ abstract class NormalizingDataProxy extends GraphQLDataProxy {
}) =>
denormalizeFragment(
// provided from cache
reader: (dataId) => readNormalized(dataId, optimistic: optimistic),
read: (dataId) => readNormalized(dataId, optimistic: optimistic),
typePolicies: typePolicies,
dataIdFromObject: dataIdFromObject,
returnPartialData: returnPartialData,
addTypename: addTypename ?? false,
// provided from request
fragment: fragmentRequest.fragment.document,
document: fragmentRequest.fragment.document,
idFields: fragmentRequest.idFields,
fragmentName: fragmentRequest.fragment.fragmentName,
variables: sanitizeVariables(fragmentRequest.variables),
Expand All @@ -97,14 +99,18 @@ abstract class NormalizingDataProxy extends GraphQLDataProxy {
Map<String, dynamic> data,
bool broadcast = true,
}) {
normalize(
writer: (dataId, value) => writeNormalized(dataId, value),
query: request.operation.document,
normalizeOperation(
// provided from cache
write: (dataId, value) => writeNormalized(dataId, value),
read: (dataId) => readNormalized(dataId),
typePolicies: typePolicies,
dataIdFromObject: dataIdFromObject,
// provided from request
document: request.operation.document,
operationName: request.operation.operationName,
variables: sanitizeVariables(request.variables),
// data
data: data,
typePolicies: typePolicies,
dataIdFromObject: dataIdFromObject,
);
if (broadcast ?? true) {
broadcastRequested = true;
Expand All @@ -118,11 +124,12 @@ abstract class NormalizingDataProxy extends GraphQLDataProxy {
}) {
normalizeFragment(
// provided from cache
writer: (dataId, value) => writeNormalized(dataId, value),
write: (dataId, value) => writeNormalized(dataId, value),
read: (dataId) => readNormalized(dataId),
typePolicies: typePolicies,
dataIdFromObject: dataIdFromObject,
// provided from request
fragment: request.fragment.document,
document: request.fragment.document,
idFields: request.idFields,
fragmentName: request.fragment.fragmentName,
variables: sanitizeVariables(request.variables),
Expand Down
9 changes: 8 additions & 1 deletion packages/graphql/lib/src/cache/_optimistic_transactions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ class OptimisticPatch {
/// [GraphQLCache.recordOptimisticTransaction].
///
/// Implements, and is exposed as, a [GraphQLDataProxy].
/// It's `optimistic` paraemeters default to `true`,
/// It's `optimistic` parameters default to `true`,
/// but the user can override them to read directly from the `store`.
class OptimisticProxy extends NormalizingDataProxy {
OptimisticProxy(this.cache);

GraphQLCache cache;

/// `typePolicies` to pass down to `normalize` (proxied from [cache])
get typePolicies => cache.typePolicies;

/// Optional `dataIdFromObject` function to pass through to [normalize]
/// (proxied from [cache])
get dataIdFromObject => cache.dataIdFromObject;

@override
SanitizeVariables get sanitizeVariables => cache.sanitizeVariables;

Expand Down
9 changes: 7 additions & 2 deletions packages/graphql/lib/src/cache/data_proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ abstract class GraphQLDataProxy {
/// Writes a GraphQL query to the root query id,
/// then [broadcast] changes to watchers unless `broadcast: false`
///
/// [normalize] the given [data] into the cache using graphql metadata from [request]
///
/// [normalize] the given [data] into the cache using graphql metadata from [request].
/// Conceptually, this can be thought of as providing a manual execution result
/// in the form of [data]
///
/// For complex `normalize` type policies that involve custom reads,
/// `optimistic` will be the default.
void writeQuery(
Request request, {
Map<String, dynamic> data,
Expand All @@ -128,6 +130,9 @@ abstract class GraphQLDataProxy {
/// If there is more than one fragment in the provided document
/// then a `fragmentName` must be provided to `fragmentRequest.fragment`
/// to select the correct fragment.
///
/// For complex `normalize` type policies that involve custom reads,
/// `optimistic` will be the default.
void writeFragment(
FragmentRequest fragmentRequest, {
Map<String, dynamic> data,
Expand Down
6 changes: 2 additions & 4 deletions packages/graphql/lib/src/utilities/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:convert';
import 'package:gql/ast.dart';
import 'package:gql/language.dart';
import 'package:http/http.dart' show MultipartFile;
import 'package:normalize/normalize.dart';
import 'package:normalize/utils.dart';

bool notNull(Object any) {
return any != null;
Expand Down Expand Up @@ -59,9 +59,7 @@ Map<String, dynamic> deeplyMergeLeft(
/// So you should probably include an [AddTypenameVistor] [transform]
DocumentNode gql(String document) => transform(
parseString(document),
[
AddTypenameVisitor(),
],
[AddTypenameVisitor()],
);

/// Converts [MultipartFile]s to a string representation containing hashCode. Default argument to [variableSanitizer]
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
gql_error_link: ^0.1.0
gql_dedupe_link: ^1.0.10
hive: ^1.3.0
normalize: ^0.2.4
normalize: ^0.4.2
http: ^0.12.1
collection: ^1.14.12
websocket: ^0.0.5
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql/test/graphql_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ void main() {
document: gql(
r'''{
someField {
__typename,
id,
myField
}
Expand All @@ -412,6 +411,7 @@ void main() {
);

final queryData = {
'__typename': 'Query',
'someField': {
...idFields,
'myField': 'originalValue',
Expand Down Expand Up @@ -465,6 +465,7 @@ void main() {
);

final updatedQueryData = {
'__typename': 'Query',
'someField': {
...idFields,
'myField': 'updatedValue',
Expand Down

0 comments on commit 4655e7d

Please sign in to comment.