How to display newest to oldest customer orders in Graphql response Magento 2?

While you working with the Magento Customer Order GraphQl, You will fetch the order by GraphQl query customerOrders.

type Query {
    customerOrders: CustomerOrders @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\Orders")
}

By Default, The Result of the customer orders will be displayed from the Older order to the newest order.

We can add sort order logic to the created_at field with Desc order so always result will be the latest order first and the older order at the end of the collection in the overridden class file.

//HERE YOU CAN SET THE ORDER TO DESCENDING ORDER (NEWESET FIRST) 
$sortOrder = $this->sortOrderFactory->create(['data' =>[ SortOrder::FIELD => 'created_at', SortOrder::DIRECTION => 'ASC' ]]); 
$this->searchCriteriaBuilder->setSortOrders([$sortOrder]);

Here We have used the setSortOrder([$sortOrder]) method to apply sort order logic in the searchCriterialBuilder.

If you want to display the latest new order first and the older order at the end of the collection, you need to override the Magento class.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\SalesGraphQl\Model\Resolver\CustomerOrders"
        type="Customer\OrderList\Model\Resolver\CustomerOrderDesc" />
</config>

Now you need to create a custom class to apply sort order for the collection.

<?php
namespace Customer\OrderList\Model\Resolver;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SortOrderFactory;
use Magento\Framework\Exception\InputException;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderFilter;

class CustomerOrderDesc implements ResolverInterface
{
    private OrderRepositoryInterface $orderRepository;

    private OrderFilter $orderFilter;

    private SortOrderFactory $sortOrderFactory;
    
    private SearchCriteriaBuilder $searchCriteriaBuilder;
    
    public function __construct(
        OrderRepositoryInterface $orderRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        OrderFilter $orderFilter,
        OrderFormatter $orderFormatter,
        SortOrderFactory $sortOrderFactory
    ) {
        $this->orderRepository = $orderRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->orderFilter = $orderFilter;
        $this->orderFormatter = $orderFormatter;
        $this->sortOrderFactory = $sortOrderFactory;
    }

    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        if (false === $context->getExtensionAttributes()->getIsCustomer()) {
            throw new GraphQlAuthorizationException(__('The current customer is not authorized.'));
        }
        
        if (isset($args['currentPage']) && $args['currentPage'] < 1) {
            throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
        }
        
        if (isset($args['pageSize']) && $args['pageSize'] < 1) {
            throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
        }
        
        
        $customerId = $context->getUserId(); // current customer id
        $store = $context->getExtensionAttributes()->getStore();
        try {
            $searchResult = $this->getSearchResult($args, (int)$userId, (int)$store->getId());
            $maxPages = (int)ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
        } catch (InputException $e) {
            throw new GraphQlInputException(__($e->getMessage()));
        }

        $ordersArray = [];
        foreach ($searchResult->getItems() as $orderModel) {
            $ordersArray[] = $this->orderFormatter->format($orderModel);
        }

        return [
            'total_count' => $searchResult->getTotalCount(),
            'items' => $ordersArray,
            'page_info' => [
                'page_size' => $searchResult->getPageSize(),
                'current_page' => $searchResult->getCurPage(),
                'total_pages' => $maxPages,
            ]
        ];
    }

    private function getSearchResult(array $args, int $customerId, int $storeId): OrderSearchResultInterface
    {
        if (isset($args['currentPage'])) {
            $this->searchCriteriaBuilder->setCurrentPage($args['currentPage']);
        }
        if (isset($args['pageSize'])) {
            $this->searchCriteriaBuilder->setPageSize($args['pageSize']);
        }
        
        $filterGroups = $this->orderFilter->createFilterGroups($args, $customerId, (int)$storeId);
        $this->searchCriteriaBuilder->setFilterGroups($filterGroups);
        
        //HERE YOU CAN SET THE ORDER TO DESCENDING ORDER (NEWESET FIRST)
        $sortOrder = $this->sortOrderFactory->create(['data' =>[
                SortOrder::FIELD        => 'created_at',
                SortOrder::DIRECTION    => 'ASC'
        ]]);
        $this->searchCriteriaBuilder->setSortOrders([$sortOrder]);
        
        return $this->orderRepository->getList($this->searchCriteriaBuilder->create());
    }
}

Now Just run the setup:upgarde command for your custom module and refresh the cache to see the changes.