Class: Okay::GraphQL::Query

Inherits:
Object show all
Defined in:
lib/okay/graphql.rb

Overview

A class for submitting GraphQL queries.

Instance Method Summary collapse

Constructor Details

#initialize(raw_query = nil, &query) ⇒ Query

Returns a new instance of Query.



75
76
77
# File 'lib/okay/graphql.rb', line 75

def initialize(raw_query = nil, &query)
  @query = raw_query || QueryDSL.new(&query)
end

Instance Method Details

#submit!(url, headers = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/okay/graphql.rb', line 85

def submit!(url, headers = {})
  if url == :github
    url = "https://api.github.com/graphql"

    bearer_token = headers[:bearer_token]

    if bearer_token.nil?
      raise ArgumentError, "missing keyword: bearer_token"
    end

    headers = default_github_headers(bearer_token).merge(headers)
  end

  data = {
    "query" => to_s,
  }.to_json
  Okay::HTTP.post(url, headers: headers, data: data)
end

#to_sObject



79
80
81
82
83
# File 'lib/okay/graphql.rb', line 79

def to_s
  "query {\n" +
    @query.to_s.gsub(/^/, "  ") +
  "}"
end