Class: Dato::Gql

Inherits:
GQLi::Client
  • Object
show all
Defined in:
lib/dato/gql.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_token, validate_query, preview, live) ⇒ Gql

Returns a new instance of Gql.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/dato/gql.rb', line 3

def initialize(api_token, validate_query, preview, live)
  @api_token = api_token
  headers = {"Authorization" => @api_token}
  headers["X-Base-Editing-Url"] = Dato::Config.base_editing_url if Dato::Config.base_editing_url.present?

  super(
    "https://graphql#{"-listen" if live}.datocms.com/#{"preview" if preview}",
    headers: headers,
    validate_query: validate_query && !live
  )
end

Instance Method Details

#live!(query) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/dato/gql.rb', line 15

def live!(query)
  http_response = request.post(@url, params: @params, json: {query: query.to_gql})

  fail "Error: #{http_response.reason}\nBody: #{http_response.body}" if http_response.status >= 300

  parsed_response = JSON.parse(http_response.to_s)
  errors = parsed_response["errors"]
  GQLi::Response.new(parsed_response, errors, query)
end