We present an electrically switchable graphene terahertz (THz) modulator with a tunable-by-design optical bandwidth and we exploit it to compensate the cavity dispersion of a quantum cascade laser (QCL). Electrostatic gating is achieved by a metal-grating used as a gate electrode, with an HfO2/AlOx gate dielectric on top. This is patterned on a polyimide layer, which acts as a quarter wave resonance cavity, coupled with an Au reflector underneath. We get 90% modulation depth of the intensity, combined with a 20 kHz electrical bandwidth in the 1.9 _ 2.7 THz range. We then integrate our modulator with a multimode THz QCL. By adjusting the modulator operational bandwidth, we demonstrate that the graphene modulator can partially compensates the QCL cavity dispersion, resulting in an integrated laser behaving as a stable frequency comb over 35% of the laser operational range, with 98 equidistant optical modes and with a spectral coverage of ~ 1.2 THz. This has significant potential for frontier applications in the terahertz, as tunable transformation-optics devices, active photonic components, adaptive and quantum optics, and as a metrological tool for spectroscopy at THz frequencies.