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

fix: Broken fragment spreads #300

Merged
merged 1 commit into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 16 additions & 22 deletions packages/normalize/lib/src/utils/expand_fragments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,35 @@ List<FieldNode> expandFragments({
if (typename == null) {
continue;
}
String fragmentOnName;
SelectionSetNode fragmentSelectionSet;
if (selectionNode is InlineFragmentNode) {
final fragmentOnName =
selectionNode.typeCondition?.on.name.value ?? typename;

// 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 ||
possibleTypes[fragmentOnName]?.contains(typename) == true) {
fieldNodes.addAll(
expandFragments(
typename: typename,
selectionSet: selectionNode.selectionSet,
fragmentMap: fragmentMap,
possibleTypes: possibleTypes,
),
);
}
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 ||
possibleTypes[fragmentOnName]?.contains(typename) == true) {
fieldNodes.addAll(
expandFragments(
typename: typename,
selectionSet: fragment.selectionSet,
selectionSet: fragmentSelectionSet,
fragmentMap: fragmentMap,
possibleTypes: possibleTypes,
),
);
} else {
throw (FormatException('Unknown selection node type'));
}
}
return List.from(_mergeSelections(fieldNodes));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import 'package:test/test.dart';
import 'package:gql/language.dart';

import 'package:normalize/normalize.dart';

void main() {
group('Union Type Inline Fragments With Possible Types', () {
final inlineFragmentQuery = parseString('''
query TestQuery {
booksAndAuthors {
id
__typename
... on Book {
title
}
... on Author {
name
}
}
}
''');
final namedFragmentQuery = parseString('''
query TestQuery {
booksAndAuthors {
id
__typename
...BookFragment
...AuthorFragment
}
}
fragment BookFragment on Book {
title
}
fragment AuthorFragment on Author {
name
}
''');

final data = {
'booksAndAuthors': [
{'id': '123', '__typename': 'Book', 'title': 'My awesome blog post'},
{'id': '324', '__typename': 'Author', 'name': 'Nicole'}
]
};
final dataDeserializedWithoutPossibleTypes = {
'booksAndAuthors': [
{
'id': '123',
'__typename': 'Book',
'title': 'My awesome blog post',
},
{'id': '324', '__typename': 'Author', 'name': 'Nicole'}
]
};

final normalizedMapWithPossibleTypes = {
'Query': {
'booksAndAuthors': [
{'\$ref': 'Book:123'},
{'\$ref': 'Author:324'}
]
},
'Book:123': {
'id': '123',
'__typename': 'Book',
'title': 'My awesome blog post'
},
'Author:324': {'id': '324', '__typename': 'Author', 'name': 'Nicole'}
};
final normalizedMapWithoutPossibleTypes = {
'Query': {
'booksAndAuthors': [
{'\$ref': 'Book:123'},
{'\$ref': 'Author:324'}
]
},
'Book:123': {
'id': '123',
'__typename': 'Book',
'title': 'My awesome blog post',
},
'Author:324': {
'id': '324',
'__typename': 'Author',
'name': 'Nicole',
}
};
final possibleTypes = {
'BookAndAuthor': {'Book', 'Author'}
};
test('Produces same normalized object with possible types', () {
final inlineNormalizedResult = {};
normalizeOperation(
read: (dataId) => inlineNormalizedResult[dataId],
write: (dataId, value) => inlineNormalizedResult[dataId] = value,
document: inlineFragmentQuery,
data: data,
possibleTypes: possibleTypes,
);
final namedFragmentNormalizedResult = {};
normalizeOperation(
read: (dataId) => namedFragmentNormalizedResult[dataId],
write: (dataId, value) => namedFragmentNormalizedResult[dataId] = value,
document: inlineFragmentQuery,
data: data,
possibleTypes: possibleTypes,
);

expect(
inlineNormalizedResult,
equals(namedFragmentNormalizedResult),
);
expect(
inlineNormalizedResult,
equals(normalizedMapWithPossibleTypes),
);
});
test('Produces same normalized object without possible types', () {
final inlineNormalizedResult = {};
normalizeOperation(
read: (dataId) => inlineNormalizedResult[dataId],
write: (dataId, value) => inlineNormalizedResult[dataId] = value,
document: inlineFragmentQuery,
data: data,
);
final namedFragmentNormalizedResult = {};
normalizeOperation(
read: (dataId) => namedFragmentNormalizedResult[dataId],
write: (dataId, value) => namedFragmentNormalizedResult[dataId] = value,
document: inlineFragmentQuery,
data: data,
);

expect(
inlineNormalizedResult,
equals(namedFragmentNormalizedResult),
);
expect(
inlineNormalizedResult,
equals(normalizedMapWithoutPossibleTypes),
);
});

test('Produces correct nested data object with possible types', () {
expect(
denormalizeOperation(
document: inlineFragmentQuery,
read: (dataId) => normalizedMapWithPossibleTypes[dataId],
possibleTypes: possibleTypes,
),
equals(
denormalizeOperation(
document: namedFragmentQuery,
read: (dataId) => normalizedMapWithPossibleTypes[dataId],
possibleTypes: possibleTypes,
),
),
);
expect(
denormalizeOperation(
document: inlineFragmentQuery,
read: (dataId) => normalizedMapWithPossibleTypes[dataId],
possibleTypes: possibleTypes,
),
equals(data),
);
});

test('Produces correct nested data object without possible types', () {
expect(
denormalizeOperation(
document: inlineFragmentQuery,
read: (dataId) => normalizedMapWithoutPossibleTypes[dataId],
),
equals(
denormalizeOperation(
document: namedFragmentQuery,
read: (dataId) => normalizedMapWithoutPossibleTypes[dataId],
),
),
);
expect(
denormalizeOperation(
document: inlineFragmentQuery,
read: (dataId) => normalizedMapWithoutPossibleTypes[dataId],
),
equals(dataDeserializedWithoutPossibleTypes),
);
});
});
}
20 changes: 12 additions & 8 deletions packages/normalize/test/query_fragments/named_fragments_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ void main() {
test('Produces correct normalized object', () {
final normalizedResult = {};
normalizeOperation(
read: (dataId) => normalizedResult[dataId],
write: (dataId, value) => normalizedResult[dataId] = value,
document: query,
data: sharedResponse,
);
read: (dataId) => normalizedResult[dataId],
write: (dataId, value) => normalizedResult[dataId] = value,
document: query,
data: sharedResponse,
possibleTypes: {
'Person': {'Author'}
});

expect(
normalizedResult,
Expand All @@ -57,9 +59,11 @@ void main() {
test('Produces correct nested data object', () {
expect(
denormalizeOperation(
document: query,
read: (dataId) => sharedNormalizedMap[dataId],
),
document: query,
read: (dataId) => sharedNormalizedMap[dataId],
possibleTypes: {
'Person': {'Author'}
}),
equals(sharedResponse),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,30 @@ void main() {

final data = {
'booksAndAuthors': [
{'id': '123', '__typename': 'Book', 'title': 'My awesome blog post'},
{'id': '324', '__typename': 'Author', 'name': 'Nicole'}
{
'id': '123',
'__typename': 'Book',
'title': 'My awesome blog post',
},
{
'id': '324',
'__typename': 'Author',
'name': 'Nicole',
}
]
};
final denormalizedData = {
'booksAndAuthors': [
{
'id': '123',
'__typename': 'Book',
'title': 'My awesome blog post',
},
{
'id': '324',
'__typename': 'Author',
'name': 'Nicole',
}
]
};

Expand All @@ -37,9 +59,13 @@ void main() {
'Book:123': {
'id': '123',
'__typename': 'Book',
'title': 'My awesome blog post'
'title': 'My awesome blog post',
},
'Author:324': {'id': '324', '__typename': 'Author', 'name': 'Nicole'}
'Author:324': {
'id': '324',
'__typename': 'Author',
'name': 'Nicole',
}
};

test('Produces correct normalized object', () {
Expand All @@ -63,7 +89,7 @@ void main() {
document: query,
read: (dataId) => normalizedMap[dataId],
),
equals(data),
equals(denormalizedData),
);
});
});
Expand Down
Loading