From b41d875d9b91e6c66d4bb1236f239eb2054ba12f Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Thu, 23 Jan 2020 15:23:08 -0500 Subject: [PATCH] Improve rake task * Fixes Rails `environment` task prereq * Does a preflight request to ensure authentication works * Improved output --- lib/shopify_api/graphql/task.rake | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/shopify_api/graphql/task.rake b/lib/shopify_api/graphql/task.rake index 5c76639b7..21e511ae1 100644 --- a/lib/shopify_api/graphql/task.rake +++ b/lib/shopify_api/graphql/task.rake @@ -3,12 +3,8 @@ require 'fileutils' namespace :shopify_api do namespace :graphql do - prereqs = [] - # add the Rails environment task as a prerequisite if loaded from a Rails app - prereqs << :environment if Rake::Task.task_defined?('environment') - desc 'Dumps a local JSON schema file of the Shopify Admin API' - task dump: prereqs do + task :dump do usage = <<~USAGE Usage: rake shopify_api:graphql:dump [] @@ -66,6 +62,8 @@ namespace :shopify_api do exit(1) end + Rake::Task['environment'].invoke if Rake::Task.task_defined?('environment') + ShopifyAPI::ApiVersion.fetch_known_versions ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown @@ -76,11 +74,27 @@ namespace :shopify_api do ShopifyAPI::Base.site = shop_url end + puts "Fetching schema for #{ShopifyAPI::Base.api_version.handle} API version..." + + client = ShopifyAPI::GraphQL::HTTPClient.new(ShopifyAPI::Base.api_version) + document = GraphQL.parse('{ __schema { queryType { name } } }') + response = client.execute(document: document).to_h + + unless response['data'].present? + puts "Error: failed to query the API." + puts "Response: #{response}" + puts 'Ensure your SHOP_DOMAIN or SHOP_URL are valid and you have valid authentication credentials.' + puts usage + exit(1) + end + schema_location = ShopifyAPI::GraphQL.schema_location FileUtils.mkdir_p(schema_location) unless Dir.exist?(schema_location) - client = ShopifyAPI::GraphQL::HTTPClient.new(ShopifyAPI::Base.api_version) - GraphQL::Client.dump_schema(client, schema_location.join("#{api_version}.json").to_s) + schema_file = schema_location.join("#{api_version}.json") + GraphQL::Client.dump_schema(client, schema_file.to_s) + + puts "Wrote file #{schema_file}" end end end