Module: Tibber::Request::GraphQL

Included in:
API
Defined in:
lib/tibber/request.rb

Overview

JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this specification defines several data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within the same process, over sockets, over http, or in many various message passing environments. It uses JSON (RFC 4627) as data format.

www.jsonrpc.org/specification

Instance Method Summary collapse

Instance Method Details

#graphql_call(query, params = nil) ⇒ WrAPI::Request::Entity

Executes a GraphQL query with optional parameters.

Examples:

Basic GraphQL call

query = "{ user(id: %d) { name email } }"
graphql_call(query, { id: 123 })

Parameters:

  • query (String)

    The GraphQL query string.

  • params (Hash, nil) (defaults to: nil)

    (optional) Parameters to be interpolated into the query.

Returns:

  • (WrAPI::Request::Entity)

    The parsed response data.

Raises:

  • (GraphQLError)

    If the response contains GraphQL errors.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tibber/request.rb', line 27

def graphql_call(query, params = nil)
  query = (query % params) if params&.any?
  options = {
    "query": query
  }
  result = post('', options)
  raise GraphQLError.new(result.body['errors']) if result.body['errors']

  data = result.body['data']
  WrAPI::Request::Entity.create(data['viewer'] || data)
rescue Faraday::BadRequestError => e
  body = e.response[:body]
  error = body&.dig('errors') || e.to_s
  raise GraphQLError.new(error)
end