Loyalty oaths have been struck down a number of times (justifiably so, given how they were used) — but even then, the cost of refusing to swear to them was the inability to join some organization or hold a job with a specific organization. In this case, the alternative to Apple having it’s employees create this form of expression is fines and potential imprisonment.
I’m a programmer. I’d turned down jobs to create things I found morally represensible. If I was asked to create code to take down websites, I’d refuse. Because yes, code is an expression of a mindset and of approval for certain behavior. Making Apple write code destroying security would be akin to making Peter, Paul and Mary write a song lauding Donald Trump’s campaign. (If you don’t know who they are, ask your parents.) It also would be highly damaging to an asset of theirs, their reputation.