GhEx.GraphQL (gh_ex v0.1.0)

Copy Markdown View Source

Generic GraphQL access. This is the whole GraphQL API: any query or mutation, including the corners REST cannot reach, such as Projects v2 and Discussions.

query/3 runs a single operation. stream/4 walks a cursor-paginated connection into a lazy Stream, mirroring GhEx.REST.stream/3 so both transports paginate through one mental model.

Result shape

query/3 returns {:ok, data, meta} on success, where data is the contents of the GraphQL data field and meta is a GhEx.GraphQL.Meta. GraphQL answers with HTTP 200 even on failure, so a response carrying a non-empty errors array becomes {:error, %GhEx.Error{}}; any partial data is preserved on the error's :body. Transport failures return the Req exception.

Variables

Pass variables as a keyword list or map. Keys may be atoms or strings; both serialize to the matching $name in the query.

GhEx.GraphQL.query(client, ~s|query($login: String!) {
  user(login: $login) { name }
}|, login: "joshrotenberg")

Pagination

stream/4 drives a connection's pageInfo. The query must accept a cursor variable (named cursor by default) wired into the connection's after: argument, and must select pageInfo { hasNextPage endCursor } alongside the nodes. Tell stream/4 where the connection lives with :path:

query = ~s|query($org: String!, $cursor: String) {
  organization(login: $org) {
    projectsV2(first: 100, after: $cursor) {
      nodes { id title }
      pageInfo { hasNextPage endCursor }
    }
  }
}|

client
|> GhEx.GraphQL.stream(query, [org: "joshrotenberg"], path: ["organization", "projectsV2"])
|> Stream.map(& &1["title"])
|> Enum.to_list()

A failed page raises GhEx.Error.

Summary

Functions

Runs a single GraphQL query (or mutation) with optional variables.

Streams the nodes of a cursor-paginated connection.

Types

meta()

@type meta() :: GhEx.GraphQL.Meta.t()

result()

@type result() :: {:ok, term(), meta()} | {:error, GhEx.Error.t() | Exception.t()}

Functions

query(client, query, variables \\ [])

@spec query(GhEx.Client.t(), String.t(), keyword() | map()) :: result()

Runs a single GraphQL query (or mutation) with optional variables.

stream(client, query, variables \\ [], opts)

@spec stream(GhEx.Client.t(), String.t(), keyword() | map(), keyword()) ::
  Enumerable.t()

Streams the nodes of a cursor-paginated connection.

Options

  • :path - required. The list of keys from data down to the connection, e.g. ["organization", "projectsV2"].
  • :cursor_var - the variable name wired into after:. Defaults to "cursor".
  • :nodes_key - the field holding the page's items. Defaults to "nodes".