Module: ShopifyToolkit::Schema
Constant Summary
collapse
- SCHEMA_PATH =
"config/shopify/schema.rb"
- OWNER_TYPES =
%i[
api_permissions
articles
blogs
carttransforms
collections
companies
company_locations
customers
delivery_customizations
discounts
draftorders
fulfillment_constraint_rules
gift_card_transactions
locations
markets
orders
order_routing_location_rules
pages
payment_customizations
products
productvariants
selling_plans
shops
validations
].freeze
Constants included
from AdminClient
AdminClient::API_VERSION
Instance Method Summary
collapse
#announce, #say, #say_with_time, #write
define, #get_metafield_gid, log_time
#api_version, #handle_shopify_admin_client_errors, #shopify_admin_client
Instance Method Details
#define(&block) ⇒ Object
68
69
70
|
# File 'lib/shopify_toolkit/schema.rb', line 68
def define(&block)
instance_eval(&block)
end
|
#dump! ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'lib/shopify_toolkit/schema.rb', line 58
def dump!
schema_path = Rails.root.join(SCHEMA_PATH)
announce "Dumping metafield schema to #{schema_path}"
say_with_time "Generating schema" do
content = generate_schema_content
File.write(schema_path, content)
end
end
|
#fetch_definitions(owner_type:) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/shopify_toolkit/schema.rb', line 72
def fetch_definitions(owner_type:)
owner_type = owner_type.to_s.singularize.upcase
query = <<~GRAPHQL
query {
metafieldDefinitions(first: 250, ownerType: #{owner_type}) {
nodes {
id
name
key
type {
name
}
namespace
description
validations {
name
value
}
capabilities {
smartCollectionCondition {
enabled
}
adminFilterable {
enabled
}
}
access {
admin
customerAccount
storefront
}
ownerType
}
}
}
GRAPHQL
result =
shopify_admin_client
.query(query:)
.tap { handle_shopify_admin_client_errors(_1) }
.body
result.dig("data", "metafieldDefinitions", "nodes") || []
end
|
#generate_schema_content ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/shopify_toolkit/schema.rb', line 119
def generate_schema_content
definitions =
OWNER_TYPES.flat_map { |owner_type| fetch_definitions(owner_type:) }
content = StringIO.new
content << <<~RUBY
# This file is auto-generated from the current state of the Shopify metafields.
# Instead of editing this file, please use the metafields migration feature of ShopifyToolkit
# to incrementally modify your metafields, and then regenerate this schema definition.
#
# This file is the source used to define your metafields when running `bin/rails shopify:schema:load`.
#
# It's strongly recommended that you check this file into your version control system.
ShopifyToolkit::Schema.define do
RUBY
definitions
.sort_by { [_1["ownerType"], _1["namespace"], _1["key"]] }
.each do
owner_type = _1["ownerType"].downcase.pluralize.to_sym
key = _1["key"].to_sym
type = _1["type"]["name"].to_sym
name = _1["name"]
namespace = _1["namespace"]&.to_sym
description = _1["description"]
validations = _1["validations"]&.map { |v| v.transform_keys(&:to_sym) }
capabilities =
_1["capabilities"]
&.transform_keys(&:to_sym)
&.transform_values { |v| v.transform_keys(&:to_sym) }
args = [owner_type, key, type]
kwargs = { name: name }
kwargs[:namespace] = namespace if namespace && namespace != :custom
kwargs[:description] = description if description
kwargs[:validations] = validations if validations.present?
if capabilities.present?
has_non_default_capabilities =
capabilities.any? do |cap, value|
case cap
when :smartCollectionCondition, :adminFilterable
value[:enabled] == true
else
true
end
end
kwargs[:capabilities] = capabilities if has_non_default_capabilities
end
args_string = args.map(&:inspect).join(", ")
kwargs_string = kwargs.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")
content.puts " create_metafield #{args_string}, #{kwargs_string}"
end
content.puts "end"
content.string
end
|
#load! ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/shopify_toolkit/schema.rb', line 44
def load!
path = Rails.root.join(SCHEMA_PATH)
unless path.exist?
logger.warn "Schema file not found at #{path}."
return
end
announce "Loading metafield schema from #{path}"
say_with_time "Executing schema statements" do
load path
end
end
|