graphql-datasource icon indicating copy to clipboard operation
graphql-datasource copied to clipboard

Support GraphQL query variables

Open retzkek opened this issue 4 years ago • 5 comments

Since GraphQL has first-class variables we should use those rather than directly interpolating in the query string.

retzkek avatar Apr 25 '20 17:04 retzkek

An easy way to implement this is to use two distinct fields in the query configuration, one with the query, another with the variables as a JSON string. The variable string can easily be appended in the POST request to the graphql server. The expressions (e.g. Grafana global variables) could then be replaced in the variable string instead of the query string (or in both if relevant).

Totalus avatar May 14 '20 16:05 Totalus

That's a good idea @Totalus, and is how graphiql does it, making it easy for one to copy-paste queries. We'll need to be careful about quoting, can we do any better than raw substitution and leaving quoting up to the user?

The expressions (e.g. Grafana global variables) could then be replaced in the variable string instead of the query string (or in both if relevant).

This is also tricky - do we make a breaking change and only do interpolation in the variable document, or do we risk doing unintended substitutions in the query body (since both Grafana and GraphQL use $var syntax)?

retzkek avatar May 14 '20 17:05 retzkek

I didn't mean to "quote" the expressions to replace, sorry for the confusion. As you say it should be raw substitution and quoting of the variables left to the user.

As for the breaking change, if Grafana 7.0 implies some breaking changes in this plugin (not sure about that, you would know more than I do), we could possibly make the change at the same time.

Totalus avatar May 14 '20 18:05 Totalus

I didn't mean to "quote" the expressions to replace, sorry for the confusion.

Sorry, I wasn't referring specifically to anything you said, just thinking out loud.

As for the breaking change, if Grafana 7.0 implies some breaking changes in this plugin (not sure about that, you would know more than I do), we could possibly make the change at the same time.

I've started working on upgrading to Grafana v7 libraries. Some components have moved around (as usual...) but I don't think there's anything that will require a user-facing change.

retzkek avatar May 14 '20 18:05 retzkek