hairyhenderson/gomplate

New feature: GraphQL datasource

miko opened this issue · 3 comments

miko commented

Hello,
as GraphQL gets more popular, it would be nice to integrate it as a datasource - using [machinebox](https://github.com/machinebox/graphql or similar implementation.

Hi @miko, thanks for filing this.

Do you have a specific use for accessing GraphQL datasources with gomplate?

One particular problem with GraphQL (as opposed to more traditional RESTful APIs) is the somewhat complex request body necessary to make almost any query. With a RESTful API, you can treat the remote endpoint like a filesystem, where the GET method is conceptually similar to a regular read operation. But with GraphQL, all queries (as far as I know?) are POSTs, and require bodies. I'm not sure how it would be possible to model that as a datasource in URL form, which is a strict necessity for gomplate.

Does this make sense, or am I missing some obvious simpler usage here?

miko commented

GraphQL queries can be transmitted by HTTP either as POST or GET request, as specified here:
https://graphql.org/learn/serving-over-http/

There are (read-only) queries and (possibly read-write) mutations (add, update, delete), so I mean queries as datasources (both by GET or POST).

Example graphql query using GET:

curl -v 'https://countries.trevorblades.com/graphql?query=\{countries\{name\}\}'

(but some graphql servers may block the GET method, even if required by specifiaction)

Example graphql query using POST:

curl --request POST \
    --header 'content-type: application/json' \
    --url 'https://spacex-production.up.railway.app/' \
    --data '{"query":"query ExampleQuery($limit: Int) {\nships (limit:$limit){\n  name\n}\n}","variables":{"limit":2}}'

For simple cases (like the first query above) you could use a http datasource, but the real help would be if for the second case I could use something similar to:

gomplate -d foo=graphql+POST://spacex-production.up.railway.app/' --query '{ships (limit:$limit){name}}' --variables '{"limit":2}'

gomplate -d foo=graphql+POST://spacex-production.up.railway.app/' --queryfile  ./ships.graphql --variables '{"limit":2}'

Thanks for the response @miko. Like I said above, this would be a significant departure from how gomplate works with datasources today. Datasources are modelled internally as filesystems accessed purely through URLs, and so passing in a large query body and set of variables seems like it just wouldn't work conceptually.

Being a query language, GraphQL is much more akin to SQL (which gomplate also can't support) than a filesystem layer on top of a key-value storage back-end.

As such, I think the answer this feature is going to have to be "no" (at least, not yet).

I'm going to close the issue, but feel free to comment further, or start up a discussion if you can see a reasonable approach that could fit elegantly within gomplate's operational model.