visitFragmentDefinitionNode method

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

Visit FragmentDefinitionNode.

Implementation

@override
void visitFragmentDefinitionNode(FragmentDefinitionNode node) {
  final partName = FragmentName(name: node.name.value);
  final nextContext = context.sameTypeWithNoPath(
    alias: partName,
    ofUnion: Nullable<TypeDefinitionNode?>(null),
  );

  logFn(context, nextContext.align, '-> Fragment');
  logFn(context, nextContext.align,
      '┌ ${nextContext.path}[${node.name.value}]');
  nextContext.fragments.add(node);

  final nextType = gql.getTypeByName(
      nextContext.typeDefinitionNodeVisitor, node.typeCondition.on);

  final visitorContext = Context(
    schema: context.schema,
    typeDefinitionNodeVisitor: context.typeDefinitionNodeVisitor,
    options: context.options,
    schemaMap: context.schemaMap,
    path: [nextContext.alias].whereType<Name>().toList(),
    currentType: nextType,
    currentFieldName: null,
    currentClassName: null,
    alias: nextContext.alias,
    generatedClasses: nextContext.generatedClasses,
    inputsClasses: [],
    fragments: [],
    usedEnums: nextContext.usedEnums,
    usedInputObjects: nextContext.usedInputObjects,
  );

  final visitor = GeneratorVisitor(context: visitorContext);

  node.selectionSet.visitChildren(visitor);

  final otherMixinsProps = nextContext.generatedClasses
      .whereType<FragmentClassDefinition>()
      .where((def) => visitor._mixins.contains(def.name))
      .map((def) => def.properties)
      .expand((a) => a)
      .mergeDuplicatesBy((a) => a.name, (a, b) => a);

  final fragmentName =
      FragmentName.fromPath(path: nextContext.fullPathName());
  logFn(context, nextContext.align,
      '└ ${nextContext.path}[${node.name.value}]');
  logFn(context, nextContext.align,
      '<- Generated fragment ${fragmentName.namePrintable}.');

  nextContext.generatedClasses.add(
    FragmentClassDefinition(
      name: fragmentName,
      properties:
          visitor._classProperties.followedBy(otherMixinsProps).toList(),
    ),
  );
}