| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 | # frozen_string_literal: truerequire '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      usage = <<~USAGE        Usage: rake shopify_api:graphql:dump [<args>]        Dumps a local JSON schema file of the Shopify Admin API. The schema is specific to an        API version and authentication is required (either OAuth or private app).        Dump the schema file for the 2020-01 API version using private app authentication:          $ rake shopify_api:graphql:dump SHOP_URL="https://API_KEY:PASSWORD@SHOP_NAME.myshopify.com" API_VERSION=2020-01        Dump the schema file for the unstable API version using an OAuth access token:          $ rake shopify_api:graphql:dump SHOP_DOMAIN=SHOP_NAME.myshopify.com ACCESS_TOKEN=abc API_VERSION=unstable        See https://github.com/Shopify/shopify_api#getting-started for more        details on getting started with authenticated API calls.        Arguments:          ACCESS_TOKEN  OAuth access token (shop specific)          API_VERSION   API version handle [example: 2020-01]          SHOP_DOMAIN   Shop domain (without path) [example: SHOP_NAME.myshopify.com]          SHOP_URL      Shop URL for private apps [example: https://API_KEY:PASSWORD@SHOP_NAME.myshopify.com]      USAGE      access_token = ENV['ACCESS_TOKEN'] || ENV['access_token']      api_version = ENV['API_VERSION'] || ENV['api_version']      shop_url = ENV['SHOP_URL'] || ENV['shop_url']      shop_domain = ENV['SHOP_DOMAIN'] || ENV['shop_domain']      unless access_token || api_version || shop_url || shop_domain        puts usage        exit(1)      end      unless shop_url || shop_domain        puts 'Error: either SHOP_DOMAIN or SHOP_URL is required for authentication'        puts usage        exit(1)      end      if shop_url && shop_domain        puts 'Error: SHOP_DOMAIN and SHOP_URL cannot be used together. Use one or the other for authentication.'        puts usage        exit(1)      end      if shop_domain && !access_token        puts 'Error: ACCESS_TOKEN required when SHOP_DOMAIN is used'        puts usage        exit(1)      end      unless api_version        puts 'Error: API_VERSION required. Example: 2020-01'        puts usage        exit(1)      end      ShopifyAPI::ApiVersion.fetch_known_versions      ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown      shopify_session = ShopifyAPI::Session.new(domain: shop_domain, token: access_token, api_version: api_version)      ShopifyAPI::Base.activate_session(shopify_session)      if shop_url        ShopifyAPI::Base.site = shop_url      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)    end  endend
 |