Module: ShopifyToolkit::MetafieldStatements

Extended by:
ActiveSupport::Concern
Includes:
AdminClient, ShopifyToolkit::Migration::Logging
Included in:
Migration, Migrator, Schema
Defined in:
lib/shopify_toolkit/metafield_statements.rb

Constant Summary

Constants included from AdminClient

AdminClient::API_VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AdminClient

#api_version, #handle_shopify_admin_client_errors, #shopify_admin_client

Methods included from ShopifyToolkit::Migration::Logging

#announce, #say, #say_with_time, #write

Class Method Details

.define(&block) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/shopify_toolkit/metafield_statements.rb', line 150

def self.define(&block)
  context = Object.new
  context.extend(self)

  context.instance_eval(&block) if block_given?(&block)
  context
end

.log_time(method_name) ⇒ Object



10
11
12
13
14
15
# File 'lib/shopify_toolkit/metafield_statements.rb', line 10

def self.log_time(method_name)
  current_method = instance_method(method_name)
  define_method(method_name) do |*args, **kwargs, &block|
    say_with_time("#{method_name}(#{args.map(&:inspect).join(', ')})") { current_method.bind(self).call(*args, **kwargs, &block) }
  end
end

Instance Method Details

#get_metafield_gid(owner_type, key, namespace: :custom) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shopify_toolkit/metafield_statements.rb', line 53

def get_metafield_gid(owner_type, key, namespace: :custom)
  ownerType = owner_type.to_s.singularize.upcase # Eg. "PRODUCT"

  result =
    shopify_admin_client
      .query(
        query:
          "# GraphQL
            query FindMetafieldDefinition($ownerType: MetafieldOwnerType!, $key: String!) {
              metafieldDefinitions(first: 1, ownerType: $ownerType, key: $key) {
                nodes { id }
              }
            }",
        variables: {
          ownerType:,
          key:,
          namespace:,
        },
      )
      .tap { handle_shopify_admin_client_errors(_1) }
      .body

  result.dig("data", "metafieldDefinitions", "nodes", 0, "id")
end