Variable updation in influx DB for templated dashboard

i created a variable for templated dashboard with one widget in it . that variable is being used in that widget .
wanted to know when widget is rendered at run time ,do grafana ui set the variable in the query or there is middle ware where these variables are set and then sent to datastore for respective query.

Grafana UI manages variable values.

@jangaraj thanks , can point me to that code wanted to look in details . where variable replacement is done.