Browse Source

Move graphql urls to ApiVersion.

Alex Aitken 6 years ago
parent
commit
2dd77b5ba8
3 changed files with 31 additions and 5 deletions
  1. 16 4
      lib/shopify_api/api_version.rb
  2. 1 1
      lib/shopify_api/resources/graphql.rb
  3. 14 0
      test/api_version_test.rb

+ 16 - 4
lib/shopify_api/api_version.rb

@@ -1,7 +1,8 @@
 module ShopifyAPI
   class ApiVersion
     class NoVersion < ApiVersion
-      API_PREFIX = '/admin/'.freeze
+      API_PREFIX = '/admin/'
+      GRAPHQL_PATH = '/admin/api/graphql.json'
 
       def initialize
         @version_name = "no version"
@@ -10,18 +11,25 @@ module ShopifyAPI
       def construct_api_path(path)
         "#{API_PREFIX}#{path}"
       end
+
+      def construct_graphql_path
+        GRAPHQL_PATH
+      end
     end
 
     class Unstable < ApiVersion
-      API_PREFIX = '/admin/api/'.freeze
+      API_PREFIX = '/admin/api/unstable/'
 
       def initialize
         @version_name = "unstable"
-        @url = "#{API_PREFIX}unstable/"
       end
 
       def construct_api_path(path)
-        "#{@url}#{path}"
+        "#{API_PREFIX}#{path}"
+      end
+
+      def construct_graphql_path
+        construct_api_path('graphql.json')
       end
     end
 
@@ -52,5 +60,9 @@ module ShopifyAPI
     def construct_api_path(_path)
       raise NotImplementedError
     end
+
+    def construct_graphql_path
+      raise NotImplementedError
+    end
   end
 end

+ 1 - 1
lib/shopify_api/resources/graphql.rb

@@ -7,7 +7,7 @@ module ShopifyAPI
   class GraphQL
     def initialize
       uri = Base.site.dup
-      uri.path = '/admin/api/graphql.json'
+      uri.path = Base.api_version.construct_graphql_path
       @http = ::GraphQL::Client::HTTP.new(uri.to_s) do
         define_method(:headers) do |_context|
           Base.headers

+ 14 - 0
test/api_version_test.rb

@@ -9,10 +9,24 @@ class ApiVersionTest < Test::Unit::TestCase
     )
   end
 
+  test "no version creates graphql url that start with /admin/api" do
+    assert_equal(
+      "/admin/api/graphql.json",
+      ShopifyAPI::ApiVersion.no_version.construct_graphql_path
+    )
+  end
+
   test "unstable version creates url that start with /admin/api/unstable/" do
     assert_equal(
       "/admin/api/unstable/resource_path/id.json",
       ShopifyAPI::ApiVersion.unstable.construct_api_path("resource_path/id.json")
     )
   end
+
+  test "unstable version creates graphql url that start with /admin/api/unstable/" do
+    assert_equal(
+      "/admin/api/unstable/graphql.json",
+      ShopifyAPI::ApiVersion.unstable.construct_graphql_path
+    )
+  end
 end