because the weasly twins weren't expelled, unlike hagrid
The Ministry of Magic 's law only says that minor wizzards can't do magic outside of Hogwarts. The restriction isn't about graduating.
They were over 17, legally adults, when they left anyway.
Hagrid did perform limited magic. He was expelled in his third year and hid his broken wand in his pink flowered umbrella. It worked in a limited way. He lit a fire when he came to get Harry. He gave Dudley a pig's tail. When the Death Eaters set fire to Hagrid's hut, Harry told him the spell "aguamenti." Then they both used it to pour water on the fire from their wands.