A grainy texture can result from overheated sauce or using pre-shredded cheese (which often contains anti-caking agents). To prevent this, melt the cheese in the sauce over medium-low heat. If using pre-shredded cheese, make sure to use a good-quality brand that doesn't have an excess of caking agents in it, or consider grating cheese from a block.