Can the Shield spell be used against Shocking Grasp?

A few attacks, such as the Shocking Grasp cantrip, can prevent reactions on a target when they hit it.

A few “defensive reactions”, such as the Shield spell (+5 to AC including against attack roll), can prevent an attack from succeeding.

I have recently faced the following scenario : I had an AC of 18, and an opponent attacked me with an equivalent of Shocking Grasp, touching with, I believe, a 21. At that moment, I was not sure if I could use my reaction to cast a Shield to jump my AC to 23 (therefore blocking the attack), or if the attack already prevented me from using Shield.

As far as I know, an attack has three phases (make the roll, determine if it hits, then apply the damage and effects). I would believe that the Shield reaction occurs between phase 2 and phase 3, therefore potentially protecting from an attack like Shocking Grasp, but I could be wrong.