Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #511 from Shopify/relocate_schema
Browse files Browse the repository at this point in the history
Moved Admin Graphql Schema functionality into the AdminAPI namespace
  • Loading branch information
Tim Anema authored Apr 13, 2020
2 parents 3e1aa62 + 34bc1da commit 7d78d8a
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 121 deletions.
9 changes: 3 additions & 6 deletions lib/project_types/node/commands/generate/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ class Generate
class Webhook < ShopifyCli::SubCommand
def call(args, _name)
selected_type = args.first
schema = ShopifyCli::Helpers::SchemaParser.new(
schema: ShopifyCli::Tasks::Schema.call(@ctx)
)
enum = schema['WebhookSubscriptionTopic']
webhooks = schema.get_names_from_enum(enum)
schema = ShopifyCli::AdminAPI::Schema.get(@ctx)
webhooks = schema.get_names_from_type('WebhookSubscriptionTopic')
unless selected_type && webhooks.include?(selected_type)
selected_type = CLI::UI::Prompt.ask('What type of webhook would you like to create?') do |handler|
webhooks.each do |type|
Expand All @@ -36,4 +33,4 @@ def self.help
end
end
end
end
end
2 changes: 0 additions & 2 deletions lib/project_types/node/commands/populate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
module Node
module Commands
class Populate < ShopifyCli::Command
prerequisite_task :schema

subcommand :Product, 'products', Project.project_filepath('commands/populate/product')
subcommand :Customer, 'customers', Project.project_filepath('commands/populate/customer')
subcommand :DraftOrder, 'draftorders', Project.project_filepath('commands/populate/draft_order')
Expand Down
9 changes: 3 additions & 6 deletions lib/project_types/rails/commands/generate/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ class Generate
class Webhook < ShopifyCli::SubCommand
def call(args, _name)
selected_type = args.first
schema = ShopifyCli::Helpers::SchemaParser.new(
schema: ShopifyCli::Tasks::Schema.call(@ctx)
)
enum = schema['WebhookSubscriptionTopic']
webhooks = schema.get_names_from_enum(enum)
schema = ShopifyCli::AdminAPI::Schema.get(@ctx)
webhooks = schema.get_names_from_type('WebhookSubscriptionTopic')
unless selected_type && webhooks.include?(selected_type)
selected_type = CLI::UI::Prompt.ask('What type of webhook would you like to create?') do |handler|
webhooks.each do |type|
Expand Down Expand Up @@ -42,4 +39,4 @@ def generate_command(selected_type)
end
end
end
end
end
2 changes: 0 additions & 2 deletions lib/project_types/rails/commands/populate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
module Rails
module Commands
class Populate < ShopifyCli::Command
prerequisite_task :schema

subcommand :Product, 'products', Project.project_filepath('commands/populate/product')
subcommand :Customer, 'customers', Project.project_filepath('commands/populate/customer')
subcommand :DraftOrder, 'draftorders', Project.project_filepath('commands/populate/draft_order')
Expand Down
1 change: 1 addition & 0 deletions lib/shopify-cli/admin_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module ShopifyCli
class AdminAPI < API
autoload :PopulateResourceCommand, 'shopify-cli/admin_api/populate_resource_command'
autoload :Schema, 'shopify-cli/admin_api/schema'

class << self
def query(ctx, body, api_version: nil, shop: nil, **variables)
Expand Down
4 changes: 2 additions & 2 deletions lib/shopify-cli/admin_api/populate_resource_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def populate
end

def input_options
schema[self.class.input_type]['inputFields'].each do |field|
schema.type(self.class.input_type)['inputFields'].each do |field|
resource_options.on(
"--#{field['name']}=#{field['defaultValue']}",
field['description']
Expand All @@ -105,7 +105,7 @@ def input_options
end

def schema
@schema ||= ShopifyCli::Helpers::SchemaParser.new(schema: @ctx.app_metadata[:schema])
@schema ||= AdminAPI::Schema.get(@ctx)
end

def run_mutation(data)
Expand Down
32 changes: 32 additions & 0 deletions lib/shopify-cli/admin_api/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'shopify_cli'

module ShopifyCli
class AdminAPI
class Schema < Hash
def self.get(ctx)
unless Helpers::Store.exists?(:shopify_admin_schema)
schema = AdminAPI.query(ctx, 'admin_introspection')
Helpers::Store.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(Helpers::Store.get(:shopify_admin_schema))]
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
2 changes: 0 additions & 2 deletions lib/shopify-cli/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ module Helpers
autoload :Organizations, 'shopify-cli/helpers/organizations'
autoload :PartnersAPI, 'shopify-cli/helpers/partners_api'
autoload :PkceToken, 'shopify-cli/helpers/pkce_token'
autoload :SchemaParser, 'shopify-cli/helpers/schema_parser'
autoload :ShopifySchema, 'shopify-cli/helpers/shopify_schema'
autoload :Store, 'shopify-cli/helpers/store'
autoload :String, 'shopify-cli/helpers/string'
end
Expand Down
28 changes: 0 additions & 28 deletions lib/shopify-cli/helpers/schema_parser.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/shopify-cli/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def self.register(task, name, path)
register :EnsureEnv, :ensure_env, 'shopify-cli/tasks/ensure_env'
register :EnsureLoopbackURL, :ensure_loopback_url, 'shopify-cli/tasks/ensure_loopback_url'
register :EnsureTestShop, :ensure_test_shop, 'shopify-cli/tasks/ensure_test_shop'
register :Schema, :schema, 'shopify-cli/tasks/schema'
register :Tunnel, :tunnel, 'shopify-cli/tasks/tunnel'
register :UpdateDashboardURLS, :update_dashboard_urls, 'shopify-cli/tasks/update_dashboard_urls'
end
Expand Down
17 changes: 0 additions & 17 deletions lib/shopify-cli/tasks/schema.rb

This file was deleted.

10 changes: 2 additions & 8 deletions test/project_types/node/commands/generate/webhook_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module Commands
module GenerateTests
class WebhookTest < MiniTest::Test
include TestHelpers::FakeUI
include TestHelpers::Schema

def setup
super
ShopifyCli::ProjectType.load_type(:node)
ShopifyCli::Tasks::Schema.expects(:call).returns(schema_json)
end

def test_with_existing_param
Expand All @@ -31,13 +31,7 @@ def test_with_selection
.returns(mock(success?: true))
run_cmd('generate webhook')
end

private

def schema_json
@schema_json ||= JSON.parse(File.read(File.join(ShopifyCli::ROOT, "test/fixtures/shopify_schema.json")))
end
end
end
end
end
end
1 change: 0 additions & 1 deletion test/project_types/node/commands/populate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class PopulateTest < MiniTest::Test
def setup
super
ShopifyCli::Helpers::AccessToken.stubs(:read).returns('myaccesstoken')
ShopifyCli::Tasks::Schema.stubs(:call)
ShopifyCli::Tasks::EnsureEnv.stubs(:call)
project_context('app_types', 'node')
ShopifyCli::ProjectType.load_type(:node)
Expand Down
12 changes: 2 additions & 10 deletions test/project_types/rails/commands/generate.rb/webhook_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,27 @@ module Commands
module GenerateTests
class WebhookTest < MiniTest::Test
include TestHelpers::FakeUI
include TestHelpers::Schema

def setup
super
ShopifyCli::ProjectType.load_type(:rails)
end

def test_with_existing_param
ShopifyCli::Tasks::Schema.expects(:call).returns(
JSON.parse(File.read(File.join(ShopifyCli::ROOT, "test/fixtures/shopify_schema.json")))
)
@context.expects(:system).with('rails g shopify_app:add_webhook -t app/uninstalled -a https://example.com/webhooks/app/uninstalled')
.returns(mock(success?: true))
run_cmd('generate webhook APP_UNINSTALLED')
end

def test_with_incorrect_param_expects_ask
ShopifyCli::Tasks::Schema.expects(:call).returns(
JSON.parse(File.read(File.join(ShopifyCli::ROOT, "test/fixtures/shopify_schema.json")))
)
CLI::UI::Prompt.expects(:ask).returns('APP_UNINSTALLED')
@context.expects(:system).with('rails g shopify_app:add_webhook -t app/uninstalled -a https://example.com/webhooks/app/uninstalled')
.returns(mock(success?: true))
run_cmd('generate webhook create_webhook_fake')
end

def test_with_selection
ShopifyCli::Tasks::Schema.expects(:call).returns(
JSON.parse(File.read(File.join(ShopifyCli::ROOT, "test/fixtures/shopify_schema.json"))),
)
CLI::UI::Prompt.expects(:ask).returns('PRODUCT_CREATE')
@context.expects(:system).with('rails g shopify_app:add_webhook -t product/create -a https://example.com/webhooks/product/create')
.returns(mock(success?: true))
Expand All @@ -42,4 +34,4 @@ def test_with_selection
end
end
end
end
end
1 change: 0 additions & 1 deletion test/project_types/rails/commands/populate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class PopulateTest < MiniTest::Test
def setup
super
ShopifyCli::Helpers::AccessToken.stubs(:read).returns('myaccesstoken')
ShopifyCli::Tasks::Schema.stubs(:call)
ShopifyCli::Tasks::EnsureEnv.stubs(:call)
project_context('app_types', 'rails')
ShopifyCli::ProjectType.load_type(:rails)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
require 'test_helper'

module ShopifyCli
module Tasks
class AdminAPI
class SchemaTest < MiniTest::Test
include TestHelpers::Project
include TestHelpers::Constants

def setup
json_data = File.read(File.join(ShopifyCli::ROOT, "test/fixtures/shopify_schema.json"))
@test_obj = AdminAPI::Schema[JSON.parse(json_data)]
@enum = {
"kind" => "ENUM",
"name" => "WebhookSubscriptionTopic",
"enumValues" => [{ "name" => "APP_UNINSTALLED" }],
}
end

def test_gets_schema
Helpers::Store.expects(:exists?).returns(false)
Helpers::Store.expects(:set).with(shopify_admin_schema: "{\"foo\":\"baz\"}")
Helpers::Store.expects(:get).with(:shopify_admin_schema).returns("{\"foo\":\"baz\"}")
ShopifyCli::AdminAPI.expects(:query)
.with(@context, 'admin_introspection')
.returns(foo: "baz")
assert_equal({ "foo" => "baz" }, ShopifyCli::Tasks::Schema.call(@context))
assert_equal({ "foo" => "baz" }, AdminAPI::Schema.get(@context))
end

def test_gets_schema_if_already_downloaded
Helpers::Store.expects(:exists?).returns(true)
Helpers::Store.expects(:get).with(:shopify_admin_schema).returns("{\"foo\":\"baz\"}")
assert_equal({ "foo" => "baz" }, ShopifyCli::Tasks::Schema.call(@context))
assert_equal({ "foo" => "baz" }, AdminAPI::Schema.get(@context))
end

def test_access
assert_equal(@test_obj.type('WebhookSubscriptionTopic'), @enum)
end

def test_get_names_from_enum
assert_equal(@test_obj.get_names_from_type('WebhookSubscriptionTopic'), ["APP_UNINSTALLED"])
end
end
end
Expand Down
26 changes: 0 additions & 26 deletions test/shopify-cli/helpers/schema_parser_test.rb

This file was deleted.

9 changes: 3 additions & 6 deletions test/test_helpers/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ module TestHelpers
module Schema
def setup
super
ShopifyCli::Tasks::Schema.stubs(:call).returns(schema)
@context.app_metadata[:schema] = schema
end

def schema
@schema ||= JSON.parse(File.read(File.join(ShopifyCli::ROOT, "test/fixtures/shopify_schema.json")))
json_data = File.read(File.join(ShopifyCli::ROOT, "test/fixtures/shopify_schema.json"))
schema = ShopifyCli::AdminAPI::Schema[JSON.parse(json_data)]
ShopifyCli::AdminAPI::Schema.stubs(:get).returns(schema)
end
end
end

0 comments on commit 7d78d8a

Please sign in to comment.