Sha256: de736382b16e261a05766cef079806e5814dfb56b6ebdf61e106fa851fd146e9

Contents?: true

Size: 1.21 KB

Versions: 13

Compression:

Stored size: 1.21 KB

Contents

require 'shopify_cli'

module ShopifyCli
  class AdminAPI
    class Schema < Hash
      class << self
        def get(ctx)
          unless ShopifyCli::DB.exists?(:shopify_admin_schema)
            shop = Project.current.env.shop || get_shop(ctx)
            schema = AdminAPI.query(ctx, 'admin_introspection', shop: shop)
            ShopifyCli::DB.set(shopify_admin_schema: JSON.dump(schema))
          end
          # This is ruby magic for making a new hash with another hash.
          # It wraps the JSON in our Schema Class to have the helper methods
          # available
          self[JSON.parse(ShopifyCli::DB.get(:shopify_admin_schema))]
        end

        private

        def get_shop(ctx)
          res = ShopifyCli::Tasks::SelectOrgAndShop.call(ctx)
          domain = res[:shop_domain]
          Project.current.env.update(ctx, :shop, domain)
          domain
        end
      end

      def type(name)
        data = self["data"]
        schema = data["__schema"]
        schema["types"].find do |object|
          object['name'] == name.to_s
        end
      end

      def get_names_from_type(name)
        type(name)["enumValues"].map do |object|
          object["name"]
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shopify-cli-1.6.0 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.5.0 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.4.1 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.4.0 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.3.1 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.3.0 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.2.0 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.1.2 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.1.1 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.1.0 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.0.5 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.0.4 lib/shopify-cli/admin_api/schema.rb
shopify-cli-1.0.3 lib/shopify-cli/admin_api/schema.rb