Created
May 13, 2020 14:04
-
-
Save smkhalsa/a7c2270790b36a76729c03ec52888d06 to your computer and use it in GitHub Desktop.
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
import 'dart:async'; | |
import 'package:ferry/ferry.dart'; | |
import 'package:gql_link/gql_link.dart'; | |
import 'package:gql_exec/gql_exec.dart'; | |
import 'package:gql/ast.dart'; | |
Map<String, List<SelectionNode>> _fragmentMap(DocumentNode document) => { | |
for (var def in document.definitions.whereType<FragmentDefinitionNode>()) | |
def.name.value: def.selectionSet.selections, | |
}; | |
/// Deeply merges field nodes | |
List<SelectionNode> _mergeSelections( | |
List<SelectionNode> selections, | |
Map<String, List<SelectionNode>> fragmentMap, | |
) => | |
_expandFragmentSpreads(selections, fragmentMap) | |
.fold<Map<String, SelectionNode>>( | |
{}, | |
(selectionMap, selection) { | |
if (selection is FieldNode) { | |
final key = selection.alias?.value ?? selection.name.value; | |
if (selection.selectionSet == null) { | |
selectionMap[key] = selection; | |
} else { | |
final existingNode = selectionMap[key]; | |
final existingSelections = existingNode is FieldNode && | |
existingNode.selectionSet != null | |
? existingNode.selectionSet.selections | |
: <SelectionNode>[]; | |
selectionMap[key] = FieldNode( | |
arguments: selection.arguments, | |
name: selection.name, | |
alias: selection.alias, | |
selectionSet: SelectionSetNode( | |
selections: _mergeSelections( | |
[ | |
...existingSelections, | |
...selection.selectionSet.selections | |
], | |
fragmentMap, | |
))); | |
} | |
} else { | |
selectionMap[selection.hashCode.toString()] = selection; | |
} | |
return selectionMap; | |
}, | |
) | |
.values | |
.toList(); | |
List<SelectionNode> _expandFragmentSpreads( | |
List<SelectionNode> selections, | |
Map<String, List<SelectionNode>> fragmentMap, | |
) => | |
selections.expand( | |
(selection) { | |
if (selection is FragmentSpreadNode) { | |
if (!fragmentMap.containsKey(selection.name.value)) { | |
throw Exception( | |
"Couldn't find fragment definition for fragment spread '${selection.name.value}'"); | |
} | |
return [ | |
..._expandFragmentSpreads( | |
fragmentMap[selection.name.value], | |
fragmentMap, | |
) | |
]; | |
} else if (selection is InlineFragmentNode) { | |
return [ | |
InlineFragmentNode( | |
typeCondition: selection.typeCondition, | |
directives: selection.directives, | |
selectionSet: SelectionSetNode( | |
selections: _expandFragmentSpreads( | |
selection.selectionSet.selections, fragmentMap))) | |
]; | |
} else { | |
return [selection]; | |
} | |
}, | |
).toList(); | |
class FlattenFragmentLink extends Link { | |
@override | |
Stream<Response> request( | |
Request request, [ | |
NextLink forward, | |
]) { | |
final fragmentMap = _fragmentMap(request.operation.document); | |
final document = DocumentNode( | |
definitions: request.operation.document.definitions | |
.whereType<OperationDefinitionNode>() | |
.map( | |
(op) => OperationDefinitionNode( | |
type: op.type, | |
name: op.name, | |
variableDefinitions: op.variableDefinitions, | |
directives: op.directives, | |
selectionSet: SelectionSetNode( | |
selections: _mergeSelections( | |
op.selectionSet.selections, | |
fragmentMap, | |
), | |
), | |
), | |
) | |
.toList(), | |
); | |
final operation = Operation( | |
document: document, | |
operationName: request.operation.operationName, | |
); | |
if (request is QueryRequest) { | |
return forward(request.copyWith(operation: operation)); | |
} | |
return forward(Request( | |
operation: operation, | |
variables: request.variables, | |
context: request.context, | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment