Write graphql queries with Clojure data structures.
Get the latest version from Clojars
Converting an EDN based query to a GraphQL query
(require '[hql.core :as hql])
(def q [:user
[:id]
[:name]
[:profilePic {:size 300}]])
(hql/graphql q)
;; ==> "user{id name profilePic(size: 300)}"
Lets look at some example queries.
- A query to get your username from Github
[:query
[:viewer
[:login]]]
This will translate to:
query {
viewer {
login
}
}
Represent a Feild
as a vector.
- Get your last N repos using a query with variables.
[:query
{:$number_of_repos 'Int!}
[:viewer
[:name]
[:repositories
{:last '$number_of_repos}
[:nodes
[:name]]]]]
query($number_of_repos: Int!) {
viewer {
name
repositories(last: $number_of_repos) {
nodes {
name
}
}
}
}
Variables in a query are represented as a map
. And arguments to the field repositories
, in the above query, is represend as a map
too.
- Get closed issues from a repo:
[:query
"issuesQuery"
{:$owner 'String! :$repo 'String!}
[:repository
"repo"
{:owner '$owner, :name '$repo}
[:issues
"issues"
{:last 20, :states 'CLOSED}
[:edges
[:node
[:labels
{:first 5}
[:edges
[:node
[:name]]]]]]]]]
query issuesQuery($owner: String!, $repo: String!) {
repo: repository(owner: $owner, name: $repo) {
issues: issues(last: 20, states: CLOSED) {
edges {
node {
labels(first: 5) {
edges {
node {
name
}
}
}
}
}
}
}
}
Repo and Issues feilds have aliases "repo" and "issues" in the above query. To specify an alias add it as the second element in the field vector.
- Use mutations to add reaction to an issue
[:mutation
"AddReactionToIssue"
[:addReaction
{:input
{:subjectId "MDU6SXNzdWUyMzEzOTE1NTE="
:content 'HOORAY}}
[:reaction
[:content]]
[:subject
[:id]]]]]
mutation AddReactionToIssue {
addReaction(
input: { subjectId: "MDU6SXNzdWU1MjM5NzY0MDE=", content: HOORAY }
) {
reaction {
content
}
subject {
id
}
}
}
Copyright © 2020 Avichal
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.