Preheat the oven to 200°C, fan 180°C, gas 6. Heat the oil in a large pan and cook the onion for 5 minutes until soft. Stir through the garlic and cook for a further minute. Stir through the tomatoes, season with black pepper and simmer for 5 minutes.
Meanwhile, cook the spaghetti according to pack instructions, then drain. Add the spaghetti to the tomato sauce, along with the stock, the basil and the mozzarella.
Tip into an ovenproof dish, sprinkle over the hard cheese and bake in the oven for 15 minutes until browned. Serve immediately.