Class: Shopify::GraphQLProxy

Inherits:
Rack::Proxy
  • Object
show all
Defined in:
lib/shopify/graphql_proxy.rb

Constant Summary collapse

PROXY_BASE_PATH =
"/graphql"
GRAPHQL_PATH =
"/admin/api/graphql.json"
VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, opts = {}) ⇒ GraphQLProxy

Returns a new instance of GraphQLProxy.



9
10
11
12
13
# File 'lib/shopify/graphql_proxy.rb', line 9

def initialize(app = nil, opts= {})
  super
  @shop = opts[:shop] if opts[:shop]
  @password = opts[:password] if opts[:password]
end

Instance Method Details

#perform_request(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shopify/graphql_proxy.rb', line 15

def perform_request(env)
  @request = Rack::Request.new(env)

  path_info = @request.path_info
  request_method = @request.request_method

  if path_info =~ %r{^#{PROXY_BASE_PATH}} && request_method == "POST"
    shop = @shop ? @shop : value_from_shopify_session(:shop)
    token = @password ? @password : value_from_shopify_session(:token)

    unless shop && token
      return ["403", {"Content-Type" => "text/plain"}, ["Unauthorized"]]
    end

    backend = URI("https://#{shop}#{GRAPHQL_PATH}")

    env["HTTP_HOST"] = backend.host
    env["PATH_INFO"] = backend.path
    env["HTTP_X_SHOPIFY_ACCESS_TOKEN"] = token
    env["SCRIPT_NAME"] = ""
    env["HTTP_COOKIE"] = nil

    super(env)
  else
    @app.call(env)
  end
end