Browse Source

Ability to use a custom graphql client

Richard Blair 5 years ago
parent
commit
2411c376cf
3 changed files with 43 additions and 3 deletions
  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,
 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.
 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
 ## Using a custom query execution adapter
 Github's GraphQL Client uses an adapter pattern so that you can define how you interact
 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`.
 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
   module GraphQL
     DEFAULT_SCHEMA_LOCATION_PATH = Pathname('shopify_graphql_schemas')
     DEFAULT_SCHEMA_LOCATION_PATH = Pathname('shopify_graphql_schemas')
     DEFAULT_EXECUTION_ADAPTER = HTTPClient
     DEFAULT_EXECUTION_ADAPTER = HTTPClient
+    DEFAULT_GRAPHQL_CLIENT = ::GraphQL::Client
 
 
     InvalidSchema = Class.new(StandardError)
     InvalidSchema = Class.new(StandardError)
     InvalidClient = Class.new(StandardError)
     InvalidClient = Class.new(StandardError)
@@ -57,8 +58,8 @@ module ShopifyAPI
             end
             end
           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
             c.allow_dynamic_queries = true
           end
           end
 
 
@@ -82,6 +83,14 @@ module ShopifyAPI
         @execution_adapter = executor
         @execution_adapter = executor
       end
       end
 
 
+      def graphql_client
+        @graphql_client || DEFAULT_GRAPHQL_CLIENT
+      end
+
+      def graphql_client=(client)
+        @graphql_client = client
+      end
+
       private
       private
 
 
       def initialize_client_cache
       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::Base.api_version = 'unstable'
 
 
       ShopifyAPI::GraphQL.initialize_clients
       ShopifyAPI::GraphQL.initialize_clients
-
       assert_instance_of SuperDuperExecutionAdapter, ShopifyAPI::GraphQL.client('unstable').execute
       assert_instance_of SuperDuperExecutionAdapter, ShopifyAPI::GraphQL.client('unstable').execute
     end
     end
 
 
     ShopifyAPI::GraphQL.execution_adapter = nil
     ShopifyAPI::GraphQL.execution_adapter = nil
   end
   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
   private
 
 
   def version_fixtures(*versions)
   def version_fixtures(*versions)