Skip to content

vane-tech/graphql-inquiry

Repository files navigation

GraphQL-Inquiry

Clojars Project

A tiny library to generate GraphQL queries from Clojure data structures.

Heavily inspired by Poor man’s GraphQL client for ClojureScript apps by Kirill Ishanov. Thank you very much! We have used this for more than a year.

Philosophy

  • Tiny, quick, and consise!
  • Just generate the query string, leave the https request to the user.
  • Support the 80% of GraphQL that we need in our daily work: queries and mutations, fields including aliases, arguments and variables. If you need more, have a look at graphql-query or graphql-builder. Or just write a simple string for that one case?
  • Neither try to hide GraphQL nor prevent generating invalid queries. This allows users who understand GraphQL to write really consise queries. The server will validate the query.
  • Works with Clojure and ClojureScript

Usage

(require '[graphql-inquiry.main :as graphql])

Queries

The easiest way to start with graphql-inquiry, is simple's query generation.

(graphql/query [:employee {:id 1
                           :active true} [:name :address :friends [:name :email]]])
=> "{employee (id:1,active:true) {name address friends {name email}}}"

Obviously, If we would like to fetch employees and projects within the same simple query, we would do it this way:

(graphql/query [:employee {:id 1
                           :active true} [:name :address :friends [:name :email]]
                :projects {:active true} [:customer :price]])

Field arguments

In the example above, :employee and :projects fields have arguments {:id 1 :active true} and {:id 1 :active true} respectively.

We can add arguments to other fields easily by wrapping field name and its arguments to vector [:customer {:id 2}]:

(graphql/query [:projects {:active true} [:customer {:id 2} :price]])

=> "{projects (active:true) {customer (id:2) price}}"

Query with alias

If you know how aliases work in GraphQL, you know how to do them in graphql-inquiry:

(graphql/query [:workhorse:employee {:id 1, :active true} [:name :address]
                :boss:employee {:id 2, :active true} [:name :address]])

Prettified query:

{
  workhorse:employee (id: 1, active: true) {
    name
    address
  }
  boss:employee (id: 2, active: true) {
    name
    address
  }
}

This of course works just fine at any level!

(graphql/query [:employee {:id 1, :active true} [:name
                                                 :address
                                                 :mates:friends [:name :email]]])

Prettified query:

{
  employee (id: 1, active: true) {
    name
    address
    mates:friends {name email}
  }
}

Query with variables

If you want to define variables to the query, you need to pass a map to the query function. When using them as parameters, just pass them as a keyword. For example, :id will become $id.

(graphql/query {:query [:workhorse:employee {:id :id
                                             :active true
                                             :name :name} [:name :address]
                        :boss:employee {:id :id, :active true} [:name :address]]
                :variable-defs {:id :ID!
                                :name :String}})

Prettified query:

query ($id: ID!, $name: String) {
  workhorse:employee (id: $id, active: true, name: $name) {
    name
    address
  }
  boss:employee (id: $id, active: true) {
    name
    address
  }
}

Operation names

GraphQL allows optional an operation name. While it's optional for single operation documents, it can be useful for example to identify queries when collecting metrics about them. Just pass the :operation-name in the query or mutation:

(query {:query [:say [:hello :world]]
        :operation-name "OperationVittles"})
query OperationVittles {say {hello world}}

Mutations

Mutations work just like queries with variables, but instead of query, you call mutation:

(graphql/mutation {:variable-defs {:id :ID!
                                   :project :ProjectInput!}
                   :query [:addProjectToEmployee {:employeeId :id, :project :project} [:allocation :name]]})

Prettified query:

mutation ($id: ID!, $project: ProjectInput!) {
  addProjectToEmployee (employeeId: $id, project: $project) {
    allocation
    name
  }
}

Meta fields

As you probably expect by now, there is no special syntax for meta fields. Just query them the same as any other field:

(query [:say [:hello :world :__typename]])

Prettified query:

{
  say {
    hello
    world
    __typename
  }
}

Release a new version

  1. First, adjust the <version> in the pom.xml.
  2. Now, run bin/dclojure -Spom to update the dependencies.
  3. Commit these things: git commit -p
  4. Tag the release and push it and the rest: git tag v`grep -oP '(?<=^ <version>).*?(?=</version>)' pom.xml` && git push --tags && git push
  5. Next, generate the jar with bin/dclojure -A:pack mach.pack.alpha.skinny --no-libs --project-path graphql-inquiry.jar
  6. Publish the jar on Clojars with docker-compose run -e CLOJARS_USERNAME=billfront -e CLOJARS_PASSWORD=<token> code clojure -A:deploy. Get a token from Clojars dashboard.

MIT License

Copyright 2020 BillFront GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.