Module: ActiveProject::Connections::GraphQl

Includes:
Base, Pagination
Included in:
Adapters::GithubProject::Connection
Defined in:
lib/active_project/connections/graph_ql.rb

Overview

Supposedly “reusable” GraphQL connection logic. Because clearly REST wasn’t performative enough, and now we need a whole query language to fetch a user’s email address.

Constant Summary

Constants included from HttpClient

HttpClient::DEFAULT_HEADERS, HttpClient::DEFAULT_RETRY_OPTS

Instance Attribute Summary

Attributes included from HttpClient

#connection, #last_response

Instance Method Summary collapse

Methods included from Pagination

#each_edge, #each_page

Methods included from HttpClient

#build_connection, #request

Methods included from Base

#parse_link_header

Instance Method Details

#init_graphql(endpoint:, token:, auth_header: "Authorization", extra_headers: {}) ⇒ Object

Initializes the GraphQL connection. Requires an endpoint, a token, an optional auth header, and—if it still doesn’t work—maybe a goat sacrifice. Bonus points if you time it around Eid al-Adha or Yom Kippur. Nothing says “API design” like invoking Abrahamic tension.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_project/connections/graph_ql.rb', line 16

def init_graphql(endpoint:, token:, auth_header: "Authorization", extra_headers: {})
  default_headers = {
    "Content-Type" => "application/json"
  }

  build_connection(
    base_url: endpoint,
    auth_middleware: ->(c) { c.headers[auth_header] = "Bearer #{token}" },
    extra_headers: default_headers.merge(extra_headers)
  )
end

#request_gql(query:, variables: {}) ⇒ Hash

Executes a GraphQL POST request. Because normal HTTP verbs had too much dignity.

Returns:

  • (Hash)

    The “data” part, which is always buried under a mountain of abstract misery.



31
32
33
34
35
36
# File 'lib/active_project/connections/graph_ql.rb', line 31

def request_gql(query:, variables: {})
  payload = { query: query, variables: variables }.to_json
  res = request(:post, "", body: payload)
  raise_graphql_errors!(res) # Make sure to decode the latest prophecy from the Error Oracle.
  res["data"]
end