Can I execute an SQL query from the panel using JavaScript?

  • What Grafana version and what operating system are you using?
    grafana v10.2.3, windows 10

  • What are you trying to achieve?
    Can I execute an SQL query from the panel using JavaScript?

  • How are you trying to achieve it?
    I’m using js fetch() inside Html Graphics plugin like this:

const query = `SELECT * FROM test;`
  const refId = "A";
  const datasource = {
    "type": "mssql",
    "uid": "uid" // I'm using correct uid
  };
  const { from, to } = data.timeRange;

  const body = {
    queries: [
      {
        datasource,
        query,
        refId,
        intervalMs: 0,
        maxDataPoints: 1e9, 
      },
    ],
    from: String(from.valueOf()),
    to: String(to.valueOf()),
  };

  fetch("/api/ds/query", {
    headers: {
      "connection": "keep-alive",
      "content-type": "application/json",
    },
    body: JSON.stringify(body),
    method: "POST",
  })
    .then((res) => {
      console.log(res);
    })
    .catch(console.error);
  • What happened?
    the response is empty but status says OK, 200

  • What did you expect to happen?
    I expect to receive data from the server; the query is okay and returns data in SQL Server Management Studio

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
    Nope

  • Did you follow any online instructions? If so, what is the URL?
    Export to CSV (influx2) | HTMLGraphics