task.rake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # frozen_string_literal: true
  2. require 'fileutils'
  3. namespace :shopify_api do
  4. namespace :graphql do
  5. desc 'Dumps a local JSON schema file of the Shopify Admin API'
  6. task :dump do
  7. usage = <<~USAGE
  8. Usage: rake shopify_api:graphql:dump [<args>]
  9. Dumps a local JSON schema file of the Shopify Admin API. The schema is specific to an
  10. API version and authentication is required (either OAuth or private app).
  11. Dump the schema file for the 2020-01 API version using private app authentication:
  12. $ rake shopify_api:graphql:dump SHOP_URL="https://API_KEY:PASSWORD@SHOP_NAME.myshopify.com" API_VERSION=2020-01
  13. Dump the schema file for the unstable API version using an OAuth access token:
  14. $ rake shopify_api:graphql:dump SHOP_DOMAIN=SHOP_NAME.myshopify.com ACCESS_TOKEN=abc API_VERSION=unstable
  15. See https://github.com/Shopify/shopify_api#getting-started for more
  16. details on getting started with authenticated API calls.
  17. Arguments:
  18. ACCESS_TOKEN OAuth access token (shop specific)
  19. API_VERSION API version handle [example: 2020-01]
  20. SHOP_DOMAIN Shop domain (without path) [example: SHOP_NAME.myshopify.com]
  21. SHOP_URL Shop URL for private apps [example: https://API_KEY:PASSWORD@SHOP_NAME.myshopify.com]
  22. USAGE
  23. access_token = ENV['ACCESS_TOKEN'] || ENV['access_token']
  24. api_version = ENV['API_VERSION'] || ENV['api_version']
  25. shop_url = ENV['SHOP_URL'] || ENV['shop_url']
  26. shop_domain = ENV['SHOP_DOMAIN'] || ENV['shop_domain']
  27. unless access_token || api_version || shop_url || shop_domain
  28. puts usage
  29. exit(1)
  30. end
  31. unless shop_url || shop_domain
  32. puts 'Error: either SHOP_DOMAIN or SHOP_URL is required for authentication'
  33. puts usage
  34. exit(1)
  35. end
  36. if shop_url && shop_domain
  37. puts 'Error: SHOP_DOMAIN and SHOP_URL cannot be used together. Use one or the other for authentication.'
  38. puts usage
  39. exit(1)
  40. end
  41. if shop_domain && !access_token
  42. puts 'Error: ACCESS_TOKEN required when SHOP_DOMAIN is used'
  43. puts usage
  44. exit(1)
  45. end
  46. unless api_version
  47. puts 'Error: API_VERSION required. Example: 2020-01'
  48. puts usage
  49. exit(1)
  50. end
  51. Rake::Task['environment'].invoke if Rake::Task.task_defined?('environment')
  52. ShopifyAPI::ApiVersion.fetch_known_versions
  53. ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown
  54. shopify_session = ShopifyAPI::Session.new(domain: shop_domain, token: access_token, api_version: api_version)
  55. ShopifyAPI::Base.activate_session(shopify_session)
  56. if shop_url
  57. ShopifyAPI::Base.site = shop_url
  58. end
  59. puts "Fetching schema for #{ShopifyAPI::Base.api_version.handle} API version..."
  60. client = ShopifyAPI::GraphQL::HTTPClient.new(ShopifyAPI::Base.api_version)
  61. document = GraphQL.parse('{ __schema { queryType { name } } }')
  62. response = client.execute(document: document).to_h
  63. unless response['data'].present?
  64. puts "Error: failed to query the API."
  65. puts "Response: #{response}"
  66. puts 'Ensure your SHOP_DOMAIN or SHOP_URL are valid and you have valid authentication credentials.'
  67. puts usage
  68. exit(1)
  69. end
  70. schema_location = ShopifyAPI::GraphQL.schema_location
  71. FileUtils.mkdir_p(schema_location) unless Dir.exist?(schema_location)
  72. schema_file = schema_location.join("#{api_version}.json")
  73. GraphQL::Client.dump_schema(client, schema_file.to_s)
  74. puts "Wrote file #{schema_file}"
  75. end
  76. end
  77. end