Module: ShopifyAPI::Utils::GraphqlProxy

Extended by:
T::Sig
Defined in:
lib/shopify_api/utils/graphql_proxy.rb

Class Method Summary collapse

Class Method Details

.proxy_query(session:, headers:, body:, cookies: nil, tries: 1) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shopify_api/utils/graphql_proxy.rb', line 19

def proxy_query(session:, headers:, body:, cookies: nil, tries: 1)
  raise Errors::PrivateAppError, "GraphQL proxing is unsupported for private apps." if Context.private?

  normalized_headers = HttpUtils.normalize_headers(headers)

  unless session.online?
    raise Errors::SessionNotFoundError,
      "Failed to load an online session from the provided parameters."
  end

  client = Clients::Graphql::Admin.new(session: session)

  case normalized_headers["content-type"]
  when "application/graphql"
    return client.query(query: body, tries: tries)
  when "application/json"
    parsed_body = JSON.parse(body)

    query = parsed_body["query"]
    raise Errors::InvalidGraphqlRequestError,
      "Request missing 'query' field in GraphQL proxy request." if query.nil?

    return client.query(query: query, variables: parsed_body["variables"], tries: tries)
  end

  raise Errors::InvalidGraphqlRequestError, "Unsupported Content-Type #{normalized_headers["content-type"]}."
end