Skip to content

avichalp/hql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HQL

CircleCI

Write graphql queries with Clojure data structures.

Usage

Get the latest version from Clojars

Latest version

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.

Examples

  1. A query to get your username from Github
[:query
  [:viewer
    [:login]]]

This will translate to:

query {
  viewer {
    login
  }
}

Represent a Feild as a vector.

  1. 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.

  1. 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.

  1. 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
    }
  }
}

License

Copyright © 2020 Avichal

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Releases

No releases published

Packages

No packages published