Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for better fragment type resolution #199

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ferry_cache/lib/src/utils/operation_root_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Map<String, dynamic> operationRootData<TData, TVars>(
request.operation.operationName!,
(request.vars as dynamic).toJson(),
typePolicies,
{},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include the possible types as an argument to operationRootData and pass them in here.

);
return {
for (var fieldName in fieldNames)
Expand Down
3 changes: 3 additions & 0 deletions normalize/lib/src/config/normalization_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class NormalizationConfig {
/// Whether to accept or return partial data.
final bool allowPartialData;

final Map<String, Set<String>> possibleTypeOf;

NormalizationConfig({
required this.read,
required this.variables,
Expand All @@ -35,5 +37,6 @@ class NormalizationConfig {
required this.dataIdFromObject,
required this.addTypename,
required this.allowPartialData,
required this.possibleTypeOf,
});
}
4 changes: 2 additions & 2 deletions normalize/lib/src/denormalize_fragment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import 'package:gql/ast.dart';
import 'package:normalize/normalize.dart';

import 'package:normalize/src/config/normalization_config.dart';
import 'package:normalize/src/policies/type_policy.dart';
import 'package:normalize/src/utils/get_fragment_map.dart';
import 'package:normalize/src/utils/resolve_data_id.dart';
import 'package:normalize/src/utils/add_typename_visitor.dart';
import 'package:normalize/src/utils/exceptions.dart';
import 'package:normalize/src/denormalize_node.dart';

/// Denormalizes data for a given fragment.
Expand Down Expand Up @@ -36,6 +34,7 @@ Map<String, dynamic>? denormalizeFragment({
bool returnPartialData = false,
bool handleException = true,
String referenceKey = '\$ref',
Map<String, Set<String>> possibleTypeOf = const {},
}) {
if (addTypename) {
document = transform(
Expand Down Expand Up @@ -86,6 +85,7 @@ Map<String, dynamic>? denormalizeFragment({
dataIdFromObject: dataIdFromObject,
addTypename: addTypename,
allowPartialData: returnPartialData,
possibleTypeOf: possibleTypeOf,
);

try {
Expand Down
1 change: 1 addition & 0 deletions normalize/lib/src/denormalize_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Object? denormalizeNode({
typename: typename,
selectionSet: selectionSet,
fragmentMap: config.fragmentMap,
possibleTypeOf: config.possibleTypeOf,
);

final result = subNodes.fold<Map<String, dynamic>>(
Expand Down
4 changes: 2 additions & 2 deletions normalize/lib/src/denormalize_operation.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:gql/ast.dart';
import 'package:normalize/normalize.dart';

import 'package:normalize/src/policies/type_policy.dart';
import 'package:normalize/src/utils/resolve_root_typename.dart';
import 'package:normalize/src/utils/add_typename_visitor.dart';
import 'package:normalize/src/utils/exceptions.dart';
import 'package:normalize/src/utils/get_operation_definition.dart';
import 'package:normalize/src/denormalize_node.dart';
import 'package:normalize/src/config/normalization_config.dart';
Expand All @@ -31,6 +29,7 @@ Map<String, dynamic>? denormalizeOperation({
bool returnPartialData = false,
bool handleException = true,
String referenceKey = '\$ref',
Map<String, Set<String>> possibleTypeOf = const {},
}) {
if (addTypename) {
document = transform(
Expand All @@ -55,6 +54,7 @@ Map<String, dynamic>? denormalizeOperation({
dataIdFromObject: dataIdFromObject,
addTypename: addTypename,
allowPartialData: returnPartialData,
possibleTypeOf: possibleTypeOf,
);

try {
Expand Down
2 changes: 2 additions & 0 deletions normalize/lib/src/normalize_fragment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void normalizeFragment({
bool addTypename = false,
String referenceKey = '\$ref',
bool acceptPartialData = true,
Map<String, Set<String>> possibleTypeOf = const {},
}) {
// Always add typenames to ensure data is stored with typename
document = transform(
Expand Down Expand Up @@ -75,6 +76,7 @@ void normalizeFragment({
addTypename: addTypename,
dataIdFromObject: dataIdFromObject,
allowPartialData: acceptPartialData,
possibleTypeOf: possibleTypeOf,
);

final dataId = resolveDataId(
Expand Down
1 change: 1 addition & 0 deletions normalize/lib/src/normalize_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Object? normalizeNode({
typename: typename,
selectionSet: selectionSet,
fragmentMap: config.fragmentMap,
possibleTypeOf: config.possibleTypeOf,
);

final dataToMerge = <String, dynamic>{
Expand Down
2 changes: 2 additions & 0 deletions normalize/lib/src/normalize_operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void normalizeOperation({
bool addTypename = false,
bool acceptPartialData = true,
String referenceKey = '\$ref',
Map<String, Set<String>> possibleTypeOf = const {},
}) {
if (addTypename) {
document = transform(
Expand All @@ -59,6 +60,7 @@ void normalizeOperation({
addTypename: addTypename,
dataIdFromObject: dataIdFromObject,
allowPartialData: acceptPartialData,
possibleTypeOf: possibleTypeOf,
);

write(
Expand Down
3 changes: 2 additions & 1 deletion normalize/lib/src/policies/field_policy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FieldFunctionOptions {
FieldFunctionOptions({
required this.field,
required NormalizationConfig config,
}) : _config = config,
}) : _config = config,
variables = config.variables,
args = argsWithValues(config.variables, field.arguments);

Expand Down Expand Up @@ -50,6 +50,7 @@ class FieldFunctionOptions {
dataIdFromObject: _config.dataIdFromObject,
addTypename: _config.addTypename,
allowPartialData: true,
possibleTypeOf: _config.possibleTypeOf,
),
) as T?;
}
Expand Down
40 changes: 25 additions & 15 deletions normalize/lib/src/utils/expand_fragments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,49 @@ List<FieldNode> expandFragments({
required String? typename,
required SelectionSetNode selectionSet,
required Map<String, FragmentDefinitionNode> fragmentMap,
required Map<String, Set<String>> possibleTypeOf,
}) {
final fieldNodes = <FieldNode>[];

for (var selectionNode in selectionSet.selections) {
if (selectionNode is FieldNode) {
fieldNodes.add(selectionNode);
} else if (selectionNode is InlineFragmentNode) {
// Only include this fragment if the type name matches
if (selectionNode.typeCondition?.on.name.value == typename) {
fieldNodes.addAll(
expandFragments(
typename: typename,
selectionSet: selectionNode.selectionSet,
fragmentMap: fragmentMap,
),
);
}
continue;
}
if (typename == null) {
continue;
}
String fragmentOnName;
SelectionSetNode fragmentSelectionSet;
if (selectionNode is InlineFragmentNode) {
fragmentOnName = selectionNode.typeCondition?.on.name.value ?? typename;
fragmentSelectionSet = selectionNode.selectionSet;
} else if (selectionNode is FragmentSpreadNode) {
final fragment = fragmentMap[selectionNode.name.value];

if (fragment == null) {
throw Exception('Missing fragment ${selectionNode.name.value}');
}
fragmentOnName = fragment.typeCondition.on.name.value;
fragmentSelectionSet = fragment.selectionSet;
} else {
throw (FormatException('Unknown selection node type'));
}

// We'll add the fields if
// - We know the current type (typename != null)
// and
// - If the fragment and current type are the same (fragmentOnName == typename)
// - Or the current type is a type of the fragment target
if (fragmentOnName == typename ||
possibleTypeOf[typename]?.contains(fragmentOnName) == true) {
fieldNodes.addAll(
expandFragments(
typename: typename,
selectionSet: fragment.selectionSet,
selectionSet: fragmentSelectionSet,
fragmentMap: fragmentMap,
possibleTypeOf: possibleTypeOf,
),
);
} else {
throw (FormatException('Unknown selection node type'));
}
}
return List.from(_mergeSelections(fieldNodes));
Expand Down
2 changes: 2 additions & 0 deletions normalize/lib/src/utils/operation_field_names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ List<String> operationFieldNames<TData, TVars>(
String operationName,
Map<String, dynamic> vars,
Map<String, TypePolicy> typePolicies,
Map<String, Set<String>> possibleTypeOf,
) {
final operationDefinition = getOperationDefinition(
document,
Expand All @@ -27,6 +28,7 @@ List<String> operationFieldNames<TData, TVars>(
typename: rootTypename,
selectionSet: operationDefinition.selectionSet,
fragmentMap: fragmentMap,
possibleTypeOf: possibleTypeOf,
);
final typePolicy = typePolicies[rootTypename];
return fields.map((fieldNode) {
Expand Down
101 changes: 101 additions & 0 deletions normalize/test/possible_type_of.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import 'package:test/test.dart';
import 'package:gql/language.dart';

import 'package:normalize/normalize.dart';

void main() {
group('Normalizing and denormalizing with possible type of', () {
test('Mutiple fragments', () {
final possibleTypeOf = {
'Author': {'User'},
'Audience': {'User'},
};
final document = parseString('''
fragment FAudience on Audience {
__typename
id
numHands
isClapping
}
fragment FUser on User {
id
__typename
name
}
query {
users {
... on Author {
__typename
id
isGoodAuthor
}
...FAudience
...FUser
}
}
''');
final data = {
'users': [
{
'__typename': 'Author',
'id': '1',
'name': 'Knud',
'isGoodAuthor': true,
},
{
'__typename': 'Audience',
'id': 'a',
'name': 'Lars',
'numHands': 2,
'isClapping': false
},
],
};

final normalizedMap = {
'Author:1': {
'__typename': 'Author',
'id': '1',
'name': 'Knud',
'isGoodAuthor': true
},
'Audience:a': {
'__typename': 'Audience',
'id': 'a',
'numHands': 2,
'isClapping': false,
'name': 'Lars'
},
'Query': {
'users': [
{r'$ref': 'Author:1'},
{r'$ref': 'Audience:a'}
]
},
};
final normalizedResult = {};
normalizeOperation(
read: (dataId) => normalizedResult[dataId],
write: (dataId, value) => normalizedResult[dataId] = value,
document: document,
data: data,
acceptPartialData: false,
possibleTypeOf: possibleTypeOf,
);
expect(
normalizedResult,
equals(normalizedMap),
);

expect(
denormalizeOperation(
document: document,
handleException: false,
read: (dataId) => normalizedMap[dataId],
possibleTypeOf: possibleTypeOf,
),
equals(data),
);
});
});
}