Varies by the Spell Type, and your reach
First, let's take a look at the text of War Caster:
When a hostile creature’s movement provokes an opportunity attack from you, you can use your reaction to cast a spell at the creature, rather than making an opportunity attack. The spell must have a casting time of 1 action and must target only that creature. -PHB p170
Then note the text on casting spells with disadvantage.
Most spells that require attack rolls involve ranged attacks. Remember that you have disadvantage on a ranged attack roll if you are within 5 feet of a hostile creature that can see you and that isn’t incapacitated (see chapter 9).-PHB p204 (italics added)
Finally, let's consider the rules on opportunity attacks:
You can make an opportunity attack when a hostile creature that you can see moves out of your reach. -PHB p195 (italics added)
Note that it has been clarified by Jeremy Crawford that:
[T]he spell you cast in place of an opportunity attack has the same timing as an opportunity attack.
This tells us two things in order to cause disadvantage on your spell:
That means there are three ways to avoid casting a spell with disadvantage.
- Cast a spell with no attack roll, like Sacred Flame or Disintegrate.
- Cast a spell with a melee attack roll, like Shocking Grasp (as mentioned in THiebert's reply).
- Have a reach of more than 5 feet, such as by using a pike. Since the target will trigger an opportunity attack when they are further than 5 feet away from you, you could then cast any spell you wanted.
You could also avoid this situation altogether by taking the Crossbow Expert feat. Most of the features of that feat don't help casters very much, but it allows you to make ranged attacks without disadvantage if an enemy is within 5 feet of you, including spell attacks (according to http://dnd.wizards.com/articles/features/sageadvice_feats).