convertType function

GraphQLType<Object?, Object?> convertType(
  1. TypeNode node,
  2. Map<String, GraphQLType> customTypes
)

Returns a GraphQLType from a TypeNode and a map of names to types customTypes. throws GraphQLError there isn't a match.

Implementation

GraphQLType<Object?, Object?> convertType(
  TypeNode node,
  Map<String, GraphQLType> customTypes,
) {
  GraphQLType _type() {
    if (node is ListTypeNode) {
      return convertType(node.type, customTypes).list();
    } else if (node is NamedTypeNode) {
      switch (node.name.value) {
        case 'Int':
          return graphQLInt;
        case 'Float':
          return graphQLFloat;
        case 'String':
          return graphQLString;
        case 'Boolean':
          return graphQLBoolean;
        case 'ID':
          return graphQLId;
        default:
          final type = customTypes[node.name.value];
          if (type == null) {
            throw GraphQLError(
              'Unknown GraphQL type: "${node.name.value}".',
              locations: GraphQLErrorLocation.listFromSource(
                node.span?.start ?? node.name.span?.start,
              ),
            );
          }
          return type;
      }
    } else {
      throw ArgumentError('Invalid GraphQL type: "${node.span!.text}"');
    }
  }

  if (node.isNonNull) {
    return _type().nonNull();
  }
  return _type();
}