Skip to content

Commit

Permalink
Improve rake task
Browse files Browse the repository at this point in the history
* Fixes Rails `environment` task prereq
* Does a preflight request to ensure authentication works
* Improved output
  • Loading branch information
swalkinshaw committed Jan 23, 2020
1 parent d6295ff commit b41d875
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/shopify_api/graphql/task.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<args>]
Expand Down Expand Up @@ -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

Expand All @@ -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

0 comments on commit b41d875

Please sign in to comment.