浏览代码

Ability to use a custom graphql client

Richard Blair 4 年之前
父节点
当前提交
2411c376cf
共有 3 个文件被更改,包括 43 次插入3 次删除
  1. 15 0
      docs/graphql.md
  2. 11 2
      lib/shopify_api/graphql.rb
  3. 17 1
      test/graphql_test.rb

+ 15 - 0
docs/graphql.md

@@ -147,6 +147,21 @@ during your boot process.
 The goal is to have all clients created at boot so there's no schema loading,
 parsing, or client instantiation done during runtime when your app serves a request.
 
+
+## Using a custom GraphQL Client
+By default `ShopifyAPI::GraphQL` wraps the Github GraphQL Client library. However, this client
+may not suitable for various reasons. If you wish to expand on the interface of the client or
+improve the required functions for your use case you can implement a client of your own.
+
+To use a custom GraphQL Client:
+```
+class CustomGraphQLClient < ::GraphQL::Client
+end
+
+ShopifyAPI::GraphQL.graphql_client = CustomGraphQLClient
+```
+
+
 ## Using a custom query execution adapter
 Github's GraphQL Client uses an adapter pattern so that you can define how you interact
 with GraphQL API's. Shopify provides a minimal implementation in `ShopifyAPI::GraphQL::HTTPClient`.

+ 11 - 2
lib/shopify_api/graphql.rb

@@ -6,6 +6,7 @@ module ShopifyAPI
   module GraphQL
     DEFAULT_SCHEMA_LOCATION_PATH = Pathname('shopify_graphql_schemas')
     DEFAULT_EXECUTION_ADAPTER = HTTPClient
+    DEFAULT_GRAPHQL_CLIENT = ::GraphQL::Client
 
     InvalidSchema = Class.new(StandardError)
     InvalidClient = Class.new(StandardError)
@@ -57,8 +58,8 @@ module ShopifyAPI
             end
           end
 
-          schema = ::GraphQL::Client.load_schema(schema_file.to_s)
-          client = ::GraphQL::Client.new(schema: schema, execute: execution_adapter.new(api_version)).tap do |c|
+          schema = graphql_client.load_schema(schema_file.to_s)
+          client = graphql_client.new(schema: schema, execute: execution_adapter.new(api_version)).tap do |c|
             c.allow_dynamic_queries = true
           end
 
@@ -82,6 +83,14 @@ module ShopifyAPI
         @execution_adapter = executor
       end
 
+      def graphql_client
+        @graphql_client || DEFAULT_GRAPHQL_CLIENT
+      end
+
+      def graphql_client=(client)
+        @graphql_client = client
+      end
+
       private
 
       def initialize_client_cache

+ 17 - 1
test/graphql_test.rb

@@ -152,13 +152,29 @@ class GraphQLTest < Test::Unit::TestCase
       ShopifyAPI::Base.api_version = 'unstable'
 
       ShopifyAPI::GraphQL.initialize_clients
-
       assert_instance_of SuperDuperExecutionAdapter, ShopifyAPI::GraphQL.client('unstable').execute
     end
 
     ShopifyAPI::GraphQL.execution_adapter = nil
   end
 
+  test '#client creates client based off configured class' do
+    class SuperDuperClient < ::GraphQL::Client
+    end
+
+    ShopifyAPI::GraphQL.graphql_client = SuperDuperClient
+    version_fixtures('unstable') do |dir|
+      ShopifyAPI::Base.api_version = 'unstable'
+
+      ShopifyAPI::GraphQL.initialize_clients
+
+      assert_instance_of SuperDuperClient, ShopifyAPI::GraphQL.client('unstable')
+    end
+
+    ShopifyAPI::GraphQL.clear_clients
+    ShopifyAPI::GraphQL.graphql_client = nil
+  end
+
   private
 
   def version_fixtures(*versions)