Fork me on GitHub
#graphql
<
2018-10-05
>
jumar20:10:50

Hi, total GraphQL noob here. I'm thinking about replacing one of our GitHub REST API calls with GraphQL API call - basically fetching all branches for all user's repos (including all repos from all his organizations) The query could be something like this this (combined from https://platform.github.community/t/fetch-all-repos-that-are-accessible-to-a-user/1316 and https://platform.github.community/t/branches-on-repository/1766) *

{
  viewer {
    repositories(first: 100, after: "Y3Vyc29yOnYyOpHOABHBcQ==",affiliations: [OWNER, COLLABORATOR, ORGANIZATION_MEMBER]) {
      totalCount
      pageInfo {
        endCursor
        hasNextPage
      }
      edges {
        node {
          name
          owner {
            login
          }
          refs(first: 100, refPrefix: "refs/heads/") {
            totalCount
            pageInfo {
              endCursor
              hasNextPage
            }
            edges {
              node {
                name
              }
            }
          }
        }
      }
    }
  }
}
Is there any handy library I can use? Or just clj-http and POST request? Note that I need to fetch all pages - at least for repositories but ideally for branches too. Some lib with out-of-the-box support for paging would be useful.

jumar07:10:57

I actually realized that what I need is to fetch branches for all repositories in a given list. I'm currently using sth. like this:

(def repo-branches-query "
  repo_name%s: repository(owner: \"%s\", name: \"%s\") {
      owner {
        login
      }
      name
      defaultBranchRef {
        name
      }
      refs(first: 100, refPrefix: \"refs/heads/\", orderBy: {field: ALPHABETICAL, direction: ASC}) {
        edges {
          node {
            name
          }
        }
      }
    }")

(defn- repos-branches-query
  "Compose a GraphQL query for fetching all branches for all given repos at once.
  If any repository contains more than 100 branches then we need to use pagination
  or ignore remaining branches."
  [repos]
  (let [partial-queries
        (for [[{:keys [owner-login name]} idx]
              (map vector repos (range))]
          (format repo-branches-query
                  idx
                  owner-login name))]
    (str
     "{"
     (clojure.string/join "\n" partial-queries)
     "}")))