visitVariableDefinitionNode method

  1. @override
void visitVariableDefinitionNode(
  1. VariableDefinitionNode node
)

Visit VariableDefinitionNode.

Implementation

@override
void visitVariableDefinitionNode(VariableDefinitionNode node) {
  final leafType =
      gql.getTypeByName(context.typeDefinitionNodeVisitor, node.type);

  final nextClassName = context
      .nextTypeWithNoPath(
        nextType: leafType,
        nextClassName: ClassName(name: leafType.name.value),
        nextFieldName: ClassName(name: node.variable.name.value),
        ofUnion: Nullable<TypeDefinitionNode?>(null),
      )
      .fullPathName();

  var dartTypeName = gql.buildTypeName(
    node.type,
    context.options,
    dartType: true,
    replaceLeafWith: ClassName.fromPath(path: nextClassName),
    typeDefinitionNodeVisitor: context.typeDefinitionNodeVisitor,
  );

  final jsonKeyAnnotation = <String, String>{};

  if (leafType is EnumTypeDefinitionNode) {
    context.usedEnums.add(EnumName(name: leafType.name.value));

    // Check if convertEnumToString is enabled
    if (context.schemaMap.convertEnumToString) {
      // Convert enum to String
      if (node.type is ListTypeNode) {
        // For lists of enums, modify the type to be List<String>
        dartTypeName = ListOfTypeName(
          typeName: TypeName(name: 'String', isNonNull: false),
          isNonNull: dartTypeName.isNonNull,
        );
      } else {
        // For single enums, use String
        dartTypeName =
            TypeName(name: 'String', isNonNull: dartTypeName.isNonNull);
      }
    } else {
      // Original behavior when convertEnumToString is false
      final variableNodeType = node.type;
      if (variableNodeType is ListTypeNode) {
        final innerDartTypeName = gql.buildTypeName(
          variableNodeType.type,
          context.options,
          dartType: true,
          replaceLeafWith: ClassName.fromPath(path: nextClassName),
          typeDefinitionNodeVisitor: context.typeDefinitionNodeVisitor,
        );
        jsonKeyAnnotation['unknownEnumValue'] =
            '${EnumName(name: innerDartTypeName.name).dartTypeSafe}.${unknown.name.namePrintable}';
      } else {
        jsonKeyAnnotation['unknownEnumValue'] =
            '${EnumName(name: dartTypeName.name).dartTypeSafe}.${unknown.name.namePrintable}';
      }
    }
  } else if (leafType is InputObjectTypeDefinitionNode) {
    addUsedInputObjectsAndEnums(leafType);
  } else if (leafType is ScalarTypeDefinitionNode) {
    final scalar =
        gql.getSingleScalarMap(context.options, leafType.name.value);

    if (scalar?.customParserImport != null &&
        leafType.name.value == scalar?.graphQLType) {
      final graphqlTypeName = gql.buildTypeName(
        node.type,
        context.options,
        dartType: false,
        typeDefinitionNodeVisitor: context.typeDefinitionNodeVisitor,
      );

      jsonKeyAnnotation['fromJson'] =
          'fromGraphQL${graphqlTypeName.parserSafe}ToDart${dartTypeName.parserSafe}';
      jsonKeyAnnotation['toJson'] =
          'fromDart${dartTypeName.parserSafe}ToGraphQL${graphqlTypeName.parserSafe}';
    }
  }

  final inputName = QueryInputName(name: node.variable.name.value);

  if (inputName.namePrintable != inputName.name) {
    jsonKeyAnnotation['name'] = '\'${inputName.name}\'';
  }

  var annotations = <String>[];

  if (jsonKeyAnnotation.isNotEmpty) {
    final jsonKey = jsonKeyAnnotation.entries
        .map<String>((e) => '${e.key}: ${e.value}')
        .join(', ');
    annotations.add('JsonKey($jsonKey)');
  }

  context.inputsClasses.add(QueryInput(
    type: dartTypeName,
    name: inputName,
    annotations: annotations,
  ));
}