dnd 5e – Can you grapple a creature as an opportunity attack?

As Miniman’s answer points out, you cannot grapple as an opportunity attack because an opportunity attack does not give you an Attack action. However, your situation doesn’t actually call for an opportunity attack. Instead, it sounds like you had readied an action. From Basic Rules page 72:

Sometimes you want to get the jump on a foe or wait for a particular circumstance before you act. To do so, you can take the Ready action on your turn so that you can act later in the round using your reaction.

First, you decide what perceivable circumstance will trigger your reaction. Then, you choose the action you will take in response to that trigger, or you choose to move up to your speed in response to it…

When the trigger occurs, you can either take your reaction right after the trigger finishes or ignore the trigger. Remember that you can take only one reaction per round.

When you ready an action, you declare that you intend to do something out of your turn in response to something specific happening. For example, you can ready an action by saying “If an enemy comes through that door, I will fire an arrow at them.” If an enemy does come through the door, you can then immediately use the readied action and shoot them.

Therefore, if you state on your turn, “I want to grapple the gnome if he tries to get past me,” and you have not taken another action on your turn, you would be able to grapple the gnome as he moves past you. If the gnome doesn’t try to get by you, the action is wasted. You can also choose to ignore the trigger and take an attack of opportunity as your reaction instead.

Keep in mind that you must declare a Ready action on your turn. Ready actions are actions, like attacks or casting spells. If your turn had already passed when you declared you’d grapple the gnome, your DM would be right in ruling that you can only make an attack. However, if you declared it on your turn, and hadn’t already attacked or acted, your DM should have allowed you to grab the gnome.