buildTypeName function

TypeName buildTypeName(
  1. Node node,
  2. GeneratorOptions options, {
  3. bool dartType = true,
  4. Name? replaceLeafWith,
  5. required TypeDefinitionNodeVisitor typeDefinitionNodeVisitor,
})

Build a string representing a Dart type, given a GraphQL type.

Implementation

TypeName buildTypeName(
  Node node,
  GeneratorOptions options, {
  bool dartType = true,
  Name? replaceLeafWith,
  required TypeDefinitionNodeVisitor typeDefinitionNodeVisitor,
}) {
  if (node is NamedTypeNode) {
    final type = typeDefinitionNodeVisitor.getByName(node.name.value);

    if (type != null) {
      if (type is ScalarTypeDefinitionNode) {
        final scalar = getSingleScalarMap(options, node.name.value);
        final dartTypeValue = scalar?.dartType;
        final graphQLTypeValue = scalar?.graphQLType;

        if (dartType && dartTypeValue != null && dartTypeValue.name != null) {
          return DartTypeName(
            name: dartTypeValue.name!,
            isNonNull: node.isNonNull,
          );
        } else if (graphQLTypeValue != null) {
          return TypeName(
            name: graphQLTypeValue,
            isNonNull: node.isNonNull,
          );
        }
      }

      if (type is EnumTypeDefinitionNode ||
          type is InputObjectTypeDefinitionNode) {
        return TypeName(name: type.name.value, isNonNull: node.isNonNull);
      }

      if (replaceLeafWith != null) {
        return TypeName(name: replaceLeafWith.name, isNonNull: node.isNonNull);
      } else {
        return TypeName(name: type.name.value, isNonNull: node.isNonNull);
      }
    }

    return TypeName(name: node.name.value, isNonNull: node.isNonNull);
  }

  if (node is ListTypeNode) {
    final typeName = buildTypeName(
      node.type,
      options,
      dartType: dartType,
      replaceLeafWith: replaceLeafWith,
      typeDefinitionNodeVisitor: typeDefinitionNodeVisitor,
    );
    return ListOfTypeName(
      typeName: typeName,
      isNonNull: node.isNonNull,
    );
  }

  throw Exception('Unable to build type name');
}