Effectively - your hypothetical proposed ‘apple law’ would, at some point in the Irish law passing process, be found to be incompatible with their commitments to being in the EU, and I assume it would be then an unconstitutional law. The price of admittance to the EU is basically having this process and constitution.
Now - they could go ahead and do it anyway in which case the enforcement from the EU could range from anything to an angry letter to some large monetary penalty - as is the case with Hungary currently being withheld some funds.
Again, not disputing that this is legally accurate to how things work, but that definitely strikes me as an environment that a lot of businesses would find hard to work with. Other smaller startups I've worked with had Irish branches because it was a good way to hire devs and governments gave us some incentives. Finding out, potentially decades later, that the Irish government had screwed us over would be a lot more catastrophic than this fine will be to Apple.
Does it make the EU/Ireland a little less attractive to foreign investment? Maybe. Was it worth the gamble in the end for Apple and Ireland - probably.
They're not really getting a reward, because this makes them much less attractive for investment. Meaning less tax revenue in the wider and longer perspective.
The Union competency in question had been established by treaty - the establishment and protection of the Single Market, and as I understand it, specifically the provisions restricting state aid - where by being members of the Union, countries have delegated regulatory and judicial primacy to organs of the Union.