shopify-graphql_proxy 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63eb52573ec696136c330d46e6366b08851a0fa0
4
- data.tar.gz: 0a072590efaee1179a3656e36edc9ad8941a5165
3
+ metadata.gz: 53a98e1b0047f818fedcfabe1015fce982167c76
4
+ data.tar.gz: ea8901b6ec6dab33dd1746fbc766bff80769ba4a
5
5
  SHA512:
6
- metadata.gz: 6db9250610bd658d30387181eed6b22be8f77ccefdc4ffffe7d9ab241849b03ff5d0ab4425496404266392a4e9918a54a5977fa011f093219cbbd7b06b375797
7
- data.tar.gz: ba7d4da8592744e36f05816077700c817947caa2921e69d2f7f5223cdc4af4b0319032435457a0cc1140f45a61db85f7aa3808bd7c989540b26748cc8032843e
6
+ metadata.gz: 7857c3847d01d36411599221120ec76190ac9ecf99b06d906a0eb0567dabc51c4a3ad8ffcbdc032580361efdab43664dcba591639ed3515b89c72cd6c5fe7490
7
+ data.tar.gz: 10db470df033c3bb6b6fcb8f12202993beef396bede4bb239dcf0a1dd5354c8698fe4e3f01a5de84697af738a137b9456c4fdd1babcbe075bc181a770188863b
@@ -0,0 +1,67 @@
1
+ # shopify-graphql_proxy
2
+ Gem to securely proxy graphql requests to Shopify from Rack based Apps
3
+
4
+ * Avoid CORS complications by proxying from same domain to Shopify
5
+ * Allows client side scripts to query a logged in merchant's shop without needing to know the users acces token
6
+
7
+ ## Installation
8
+ Add the following to your Gemfile
9
+ ```
10
+ gem 'shopify-graphql_proxy', '-> 0.1.0'
11
+ ```
12
+ Or install:
13
+ ```
14
+ gem install shopify-graphql_proxy
15
+ ```
16
+
17
+ ## Usage
18
+ It is recommended to use the [omniauth-shopify-oauth2](https://github.com/Shopify/omniauth-shopify-oauth2) to authenticate requests with Shopify
19
+
20
+ ```ruby
21
+ use Shopify::GraphQLProxy
22
+
23
+ ```
24
+
25
+ This middleware expects that the session data is stored in the shopify key
26
+
27
+
28
+ ```ruby
29
+ session[:shopify] = {
30
+ shop: shop_name,
31
+ token: token
32
+ }
33
+ ```
34
+
35
+ It will proxy any `POST` request to `/graphql` on your app to the current logged in shop found in session
36
+
37
+ #### Get GraphQL data from client side with logged in merchant's shop
38
+ ```javascript
39
+ fetch('/graphql', {
40
+ method: 'POST',
41
+ headers: { 'Content-Type': 'application/json' },
42
+ body: JSON.stringify({ query: '{ shop { name } }' }),
43
+ credentials: 'include'
44
+ })
45
+ .then(res => res.json())
46
+ .then(res => console.log(res.data));
47
+ ```
48
+
49
+ ## Custom path
50
+ You can use the Rack::Builder#map method to specify middleware to run under specific path
51
+ ```ruby
52
+ # /shopify/graphql
53
+
54
+ map('/shopify') do
55
+ use Shopify::GraphQLProxy
56
+ run Proc.new { |env| [200, {'Content-Type' => 'text/plain'}, ['get rack\'d']]}
57
+ end
58
+
59
+ map('/') do
60
+ run App
61
+ end
62
+ ```
63
+
64
+ ## Thanks
65
+
66
+ * [Rack Proxy](https://github.com/ncr/rack-proxy)
67
+ * [Koa Shopify GraphQL Proxy](https://github.com/Shopify/quilt/tree/master/packages/koa-shopify-graphql-proxy)
@@ -4,25 +4,33 @@ module Shopify
4
4
  class GraphQLProxy < Rack::Proxy
5
5
  PROXY_BASE_PATH = "/graphql"
6
6
  GRAPHQL_PATH = "/admin/api/graphql.json"
7
- VERSION = "0.1.0"
7
+ VERSION = "0.2.0"
8
+
9
+ def initialize(app = nil, opts= {})
10
+ super
11
+ @shop = opts[:shop] if opts[:shop]
12
+ @password = opts[:password] if opts[:password]
13
+ end
8
14
 
9
15
  def perform_request(env)
10
- request = Rack::Request.new(env)
16
+ @request = Rack::Request.new(env)
11
17
 
12
- if request.path_info =~ %r{^#{PROXY_BASE_PATH}}
13
- unless request.request_method == "POST"
14
- return @app.call(env)
15
- end
18
+ path_info = @request.path_info
19
+ request_method = @request.request_method
20
+
21
+ if path_info =~ %r{^#{PROXY_BASE_PATH}} && request_method == "POST"
22
+ shop = @shop ? @shop : value_from_shopify_session(:shop)
23
+ token = @password ? @password : value_from_shopify_session(:token)
16
24
 
17
- unless request.session.key?(:shopify)
25
+ unless shop && token
18
26
  return ["403", {"Content-Type" => "text/plain"}, ["Unauthorized"]]
19
27
  end
20
28
 
21
- backend = URI("https://#{request.session[:shopify][:shop]}#{GRAPHQL_PATH}")
29
+ backend = URI("https://#{shop}#{GRAPHQL_PATH}")
22
30
 
23
31
  env["HTTP_HOST"] = backend.host
24
32
  env["PATH_INFO"] = backend.path
25
- env["HTTP_X_SHOPIFY_ACCESS_TOKEN"] = request.session[:shopify][:token]
33
+ env["HTTP_X_SHOPIFY_ACCESS_TOKEN"] = token
26
34
  env["SCRIPT_NAME"] = ""
27
35
  env["HTTP_COOKIE"] = nil
28
36
 
@@ -31,5 +39,11 @@ module Shopify
31
39
  @app.call(env)
32
40
  end
33
41
  end
42
+
43
+ private
44
+
45
+ def value_from_shopify_session(key)
46
+ @request.session.key?(:shopify) ? @request.session[:shopify][key] : nil;
47
+ end
34
48
  end
35
49
  end
@@ -1,10 +1,11 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shopify/graphql_proxy'
4
5
 
5
6
  Gem::Specification.new do |spec|
6
7
  spec.name = "shopify-graphql_proxy"
7
- spec.version = "0.1.0"
8
+ spec.version = Shopify::GraphQLProxy::VERSION
8
9
  spec.authors = ["montalvomiguelo"]
9
10
  spec.email = ["me@montalvomiguelo.com"]
10
11
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify-graphql_proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - montalvomiguelo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-24 00:00:00.000000000 Z
11
+ date: 2018-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,6 +90,7 @@ files:
90
90
  - ".gitignore"
91
91
  - Gemfile
92
92
  - LICENSE.txt
93
+ - README.md
93
94
  - Rakefile
94
95
  - lib/shopify/graphql_proxy.rb
95
96
  - shopify-graphql_proxy.gemspec