visitFragmentDefinitionNode method

  1. @override
FragmentDefinitionNode visitFragmentDefinitionNode(
  1. FragmentDefinitionNode node
)

Visit FragmentDefinitionNode.

Implementation

@override
FragmentDefinitionNode visitFragmentDefinitionNode(
  FragmentDefinitionNode node,
) {
  final hasTypename = node.selectionSet.selections
      .whereType<FieldNode>()
      .any((node) => node.name.value == '__typename');

  if (hasTypename) return node;

  return FragmentDefinitionNode(
    name: node.name,
    typeCondition: node.typeCondition,
    directives: node.directives,
    selectionSet: SelectionSetNode(
      selections: <SelectionNode>[
        FieldNode(
          name: NameNode(value: '__typename'),
        ),
        ...node.selectionSet.selections,
      ],
    ),
  );
}