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
# File 'lib/dato/gql.rb', line 3

def initialize(api_token, validate_query, preview, live)
  @api_token = api_token
  super(
    "https://graphql#{"-listen" if live}.datocms.com/#{"preview" if preview}",
    headers: {
      "Authorization" => @api_token
    },
    validate_query: validate_query && !live
  )
end

Instance Method Details

#live!(query) ⇒ Object



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

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