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();

  final 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));
    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}.${artemisUnknown.name.namePrintable}';
    } else {
      jsonKeyAnnotation['unknownEnumValue'] =
          '${EnumName(name: dartTypeName.name).dartTypeSafe}.${artemisUnknown.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,
  ));
}