Cook the spaghetti in a large pan of boiling water, according to packet instructions.
While the spaghetti is cooking, heat the oil in a large frying pan and add the shallots. Fry for about 3 minutes until the shallots are starting to turn brown, then stir in the garlic and fry for another minute.
Drop in the butter and as it melts, stir in the mushrooms and thyme over a medium-high heat and fry for 2-3 minutes until the mushrooms are turning golden. Finally, stir in the ham for and cook for a further 1 minute, season with pepper, then set aside and keep warm.
Beat the eggs then stir in the cream and gruyère, then season with salt and pepper. Drain the spaghetti in a colander over a bowl, so you can reserve a little of the pasta water, then tip the spaghetti into the pan with the ham and mushrooms. Pour in the egg mixture and stir together to coat well, spooning in a few spoonfuls of the pasta water to give enough sauce. Make sure the spaghetti is piping hot to ensure the egg is cooked through.
Serve immediately, sprinkled with extra thyme leaves and gruyère.
See more Pasta recipes
For top tips on protecting you and your family when preparing raw meat and poultry, visit Food Safety in the Home.
If serving vulnerable groups, elderly people, babies, toddlers, pregnant women and people who are unwell, cook until the whites and yolks are solid.