Querying graphql in Clojure

I’ve started a project that involves querying a graphql api. So, given that I mostly just need to write graphql queries (using clj), what are people using these days for graphql? I don’t need Lacinia because I’m not building anything that needs to parse or accept graphql; I only need to produce queries, which seem a little different than raw json (for which I’d use Cheshire). I found GitHub - district0x/graphql-query: Clojure(Script) graphql query generation and see it hasn’t been updated in a few years, with a couple minor unaddressed PRs. Is this the way to go, or are there more recently active/complete solutions for writing graphql?

Personally, i would just write them as strings; I don’t have much use for manipulating queries as data, and I would rather be able to clearly see what query I am sending to my API rather than parse and validate some data DSL that gets converted to GraphQL.

2 Likes

I’ll second @lilactown. I had used venia and it was challenging to support all the graphql syntax. Maybe the new library covers all the gaps, but if you don’t need the query to be dynamic then strings will be more comprehensible and the queries easier to debug via graphiql.

1 Like

Make use of GraphQL variables so that your queries are static strings. You might even find that you don’t need to send the query itself to the server, but only the name of the query (plus the variables). We’ve implemented this in Lacinia but haven’t been able to share it (the code is trivial but tied to our database layers).

There’s even less reason to “string bash” GraphQL than there is with SQL.

2 Likes

String Bashing I had to do because the graphql-query library couldn’t generate this string:

(str "{\"query\": \"query {app(id: \\\""
                (-> env  :kuali :leave-form-id)
                "\\\"){id name documentConnection{totalCount edges{node{data}}}}}\"}")

It was a lesson in pain to figure out that it was a “you need more slashes” problem I was having… ugh. Not to mention the fact that graphql seems to be highly sensitive to white space.

We’ve been using GrapQL in Clojurescript for over a year for a very large project that has hundreds of queries and mutations and been very pleased with the ergonomics of GitHub - retro/graphql-builder: GraphQL client library for Clojure and ClojureScript in this context. On top of that, we have been using GitHub - oliyh/re-graph: A graphql client for clojurescript and clojure So, take a look at those as well!

1 Like

graphql-builder looks like gold! I usually Honeysql data-orientation to Hug “let their own language do it”, but this way I at least get my own mode!

Mockup:

(send-json-request
  {:query "query($id: String!) { app(id: $Id) { id name .... }}"
   :variables {:id (-> env :kuali :leave-form-id)})

Don’t string bash, use GraphQL variables and not ever worry about quoting again. Just like a SQL Prepared Statement.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.