task.rake 3.1 KB

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