Skip to content

Instantly share code, notes, and snippets.

@daniel-sim
Created February 1, 2021 15:49
Show Gist options
  • Save daniel-sim/e8eee982b0e90dd3b74fc66eb568872c to your computer and use it in GitHub Desktop.
Save daniel-sim/e8eee982b0e90dd3b74fc66eb568872c to your computer and use it in GitHub Desktop.
require "graphql/client"
require "http_client"
module ShopifyPartnerAPI
class << self
delegate :parse, :query, to: :client
def client
initialize_client_cache
cached_client = @_client_cache
if cached_client != nil
cached_client
else
initialize_client
@_client_cache
end
end
def initialize_client
initialize_client_cache
http = ShopifyPartnerAPI::HTTPClient.new
# So the schema is not requested every time the client is initialized we store it on disk.
# If the schema changes, run GraphQL::Client.dump_schema(http, "config/partner-api-schema.json")
GraphQL::Client.dump_schema(http, "config/partner-api-schema.json") unless File.exists?("config/partner-api-schema.json")
schema = GraphQL::Client.load_schema("config/partner-api-schema.json")
client = GraphQL::Client.new(schema: schema, execute: http)
@_client_cache = client
end
private
def initialize_client_cache
@_client_cache ||= nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment