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.
(require '[graphql-inquiry.main :as graphql])
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]])
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}}"
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}
}
}
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
}
}
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 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
}
}
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
}
}
<version>
in the pom.xml.bin/dclojure -Spom
to update the dependencies.git commit -p
git tag v`grep -oP '(?<=^ <version>).*?(?=</version>)' pom.xml` && git push --tags && git push
bin/dclojure -A:pack mach.pack.alpha.skinny --no-libs --project-path graphql-inquiry.jar
docker-compose run -e CLOJARS_USERNAME=billfront -e CLOJARS_PASSWORD=<token> code clojure -A:deploy
. Get a token from Clojars dashboard.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.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close