123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- # frozen_string_literal: true
- require 'test_helper'
- require_relative 'fixtures/graphql/dummy_schema'
- class GraphQLTest < Test::Unit::TestCase
- def setup
- ShopifyAPI::ApiVersion.version_lookup_mode = :define_on_unknown
- @fixture_location = Pathname('test/fixtures/graphql')
- @site = 'https://this-is-my-test-shop.myshopify.com'
- ShopifyAPI::Base.site = @site
- end
- def teardown
- ShopifyAPI::GraphQL.clear_clients
- end
- test '#initialize_clients creates a GraphQL::Client from local schema file' do
- version_fixtures('unstable') do |_dir|
- ShopifyAPI::GraphQL.initialize_clients
- assert(ShopifyAPI::GraphQL.client('unstable'))
- end
- end
- test '#initialize_clients handles multiple schema files' do
- version_fixtures('unstable', '2019-10') do |_dir|
- ShopifyAPI::GraphQL.initialize_clients
- assert(ShopifyAPI::GraphQL.client('unstable'))
- assert(ShopifyAPI::GraphQL.client('2019-10'))
- end
- end
- test '#initialize_clients ignores non JSON schema files' do
- version_fixtures('unstable', '2019-10') do |_dir|
- FileUtils.touch(ShopifyAPI::GraphQL.schema_location.join('nope.txt'))
- ShopifyAPI::GraphQL.initialize_clients
- assert(ShopifyAPI::GraphQL.client('unstable'))
- assert(ShopifyAPI::GraphQL.client('2019-10'))
- end
- end
- test '#initialize_clients raises if a JSON schema file is not named after a version' do
- version_fixtures do |dir|
- ShopifyAPI::GraphQL.schema_location = dir
- FileUtils.touch(ShopifyAPI::GraphQL.schema_location.join('nope.json'))
- assert_raises(ShopifyAPI::GraphQL::InvalidSchema) do
- ShopifyAPI::GraphQL.initialize_clients
- end
- end
- end
- test '#initialize_clients does not raise if raise_on_invalid_schema is set to false' do
- version_fixtures('unstable') do |dir|
- ShopifyAPI::GraphQL.schema_location = dir
- FileUtils.touch(ShopifyAPI::GraphQL.schema_location.join('nope.json'))
- ShopifyAPI::GraphQL.initialize_clients(raise_on_invalid_schema: false)
- assert(ShopifyAPI::GraphQL.client('unstable'))
- end
- end
- test '#client returns default schema if only one exists' do
- version_fixtures('unstable') do |_dir|
- ShopifyAPI::Base.api_version = 'unstable'
- ShopifyAPI::GraphQL.initialize_clients
- assert_instance_of(::GraphQL::Client, ShopifyAPI::GraphQL.client)
- end
- end
- test '#client accepts optional api_version parameter' do
- version_fixtures('unstable') do |_dir|
- ShopifyAPI::Base.api_version = 'unstable'
- ShopifyAPI::GraphQL.initialize_clients
- assert_instance_of(::GraphQL::Client, ShopifyAPI::GraphQL.client('unstable'))
- end
- end
- test '#client executes queries on specified API version' do
- version_fixtures('unstable', '2019-10') do |_dir|
- ShopifyAPI::Base.api_version = 'unstable'
- ShopifyAPI::GraphQL.initialize_clients
- ShopifyAPI::Base.site = 'https://this-is-my-test-shop.myshopify.com'
- client = ShopifyAPI::GraphQL.client('2019-10')
- assert_instance_of(::GraphQL::Client, client)
- query = client.parse(<<~GRAPHQL)
- {
- product {
- name
- }
- }
- GRAPHQL
- path = ShopifyAPI::ApiVersion.new('2019-10').construct_graphql_path
- stub_request(:post, "#{@site}#{path}").to_return(body: { product: { name: 'Shirt' } }.to_json)
- client.query(query)
- end
- end
- test '#client raises exception for version that does not exist' do
- version_fixtures('unstable') do |_dir|
- ShopifyAPI::Base.api_version = '2019-10'
- ShopifyAPI::GraphQL.initialize_clients
- assert_raises(ShopifyAPI::GraphQL::InvalidClient) do
- ShopifyAPI::GraphQL.client('2019-10')
- end
- end
- end
- test '#client lazily initializes clients' do
- version_fixtures('unstable') do |_dir|
- ShopifyAPI::Base.api_version = 'unstable'
- assert_raises(ShopifyAPI::GraphQL::InvalidClient) do
- ShopifyAPI::GraphQL.client('2019-10')
- end
- end
- end
- test '#client caches lookups' do
- version_fixtures('unstable') do |_dir|
- ShopifyAPI::Base.api_version = 'unstable'
- client1 = ShopifyAPI::GraphQL.client
- client2 = ShopifyAPI::GraphQL.client('unstable')
- assert_equal(client1, client2)
- end
- end
- test '#client creates execution adapter based off configured class' do
- class SuperDuperExecutionAdapter < ShopifyAPI::GraphQL::HTTPClient
- end
- ShopifyAPI::GraphQL.execution_adapter = SuperDuperExecutionAdapter
- version_fixtures('unstable') do |_dir|
- ShopifyAPI::Base.api_version = 'unstable'
- ShopifyAPI::GraphQL.initialize_clients
- assert_instance_of(SuperDuperExecutionAdapter, ShopifyAPI::GraphQL.client('unstable').execute)
- end
- ShopifyAPI::GraphQL.execution_adapter = nil
- end
- test '#client creates client based off configured class' do
- class SuperDuperClient < ::GraphQL::Client
- end
- ShopifyAPI::GraphQL.graphql_client = SuperDuperClient
- version_fixtures('unstable') do |_dir|
- ShopifyAPI::Base.api_version = 'unstable'
- ShopifyAPI::GraphQL.initialize_clients
- assert_instance_of(SuperDuperClient, ShopifyAPI::GraphQL.client('unstable'))
- end
- ShopifyAPI::GraphQL.clear_clients
- ShopifyAPI::GraphQL.graphql_client = nil
- end
- private
- def version_fixtures(*versions)
- Dir.mktmpdir do |dir|
- versions.each do |version|
- FileUtils.cp(@fixture_location.join("#{version}.json"), dir)
- end
- ShopifyAPI::GraphQL.schema_location = dir
- yield(dir)
- end
- end
- end
|