graphql_test.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. require_relative 'fixtures/graphql/dummy_schema'
  4. class GraphQLTest < Test::Unit::TestCase
  5. def setup
  6. ShopifyAPI::ApiVersion.version_lookup_mode = :define_on_unknown
  7. @fixture_location = Pathname('test/fixtures/graphql')
  8. @site = 'https://this-is-my-test-shop.myshopify.com'
  9. ShopifyAPI::Base.site = @site
  10. end
  11. def teardown
  12. ShopifyAPI::GraphQL.clear_clients
  13. end
  14. test '#initialize_clients creates a GraphQL::Client from local schema file' do
  15. version_fixtures('unstable') do |_dir|
  16. ShopifyAPI::GraphQL.initialize_clients
  17. assert(ShopifyAPI::GraphQL.client('unstable'))
  18. end
  19. end
  20. test '#initialize_clients handles multiple schema files' do
  21. version_fixtures('unstable', '2019-10') do |_dir|
  22. ShopifyAPI::GraphQL.initialize_clients
  23. assert(ShopifyAPI::GraphQL.client('unstable'))
  24. assert(ShopifyAPI::GraphQL.client('2019-10'))
  25. end
  26. end
  27. test '#initialize_clients ignores non JSON schema files' do
  28. version_fixtures('unstable', '2019-10') do |_dir|
  29. FileUtils.touch(ShopifyAPI::GraphQL.schema_location.join('nope.txt'))
  30. ShopifyAPI::GraphQL.initialize_clients
  31. assert(ShopifyAPI::GraphQL.client('unstable'))
  32. assert(ShopifyAPI::GraphQL.client('2019-10'))
  33. end
  34. end
  35. test '#initialize_clients raises if a JSON schema file is not named after a version' do
  36. version_fixtures do |dir|
  37. ShopifyAPI::GraphQL.schema_location = dir
  38. FileUtils.touch(ShopifyAPI::GraphQL.schema_location.join('nope.json'))
  39. assert_raises(ShopifyAPI::GraphQL::InvalidSchema) do
  40. ShopifyAPI::GraphQL.initialize_clients
  41. end
  42. end
  43. end
  44. test '#initialize_clients does not raise if raise_on_invalid_schema is set to false' do
  45. version_fixtures('unstable') do |dir|
  46. ShopifyAPI::GraphQL.schema_location = dir
  47. FileUtils.touch(ShopifyAPI::GraphQL.schema_location.join('nope.json'))
  48. ShopifyAPI::GraphQL.initialize_clients(raise_on_invalid_schema: false)
  49. assert(ShopifyAPI::GraphQL.client('unstable'))
  50. end
  51. end
  52. test '#client returns default schema if only one exists' do
  53. version_fixtures('unstable') do |_dir|
  54. ShopifyAPI::Base.api_version = 'unstable'
  55. ShopifyAPI::GraphQL.initialize_clients
  56. assert_instance_of(::GraphQL::Client, ShopifyAPI::GraphQL.client)
  57. end
  58. end
  59. test '#client accepts optional api_version parameter' do
  60. version_fixtures('unstable') do |_dir|
  61. ShopifyAPI::Base.api_version = 'unstable'
  62. ShopifyAPI::GraphQL.initialize_clients
  63. assert_instance_of(::GraphQL::Client, ShopifyAPI::GraphQL.client('unstable'))
  64. end
  65. end
  66. test '#client executes queries on specified API version' do
  67. version_fixtures('unstable', '2019-10') do |_dir|
  68. ShopifyAPI::Base.api_version = 'unstable'
  69. ShopifyAPI::GraphQL.initialize_clients
  70. ShopifyAPI::Base.site = 'https://this-is-my-test-shop.myshopify.com'
  71. client = ShopifyAPI::GraphQL.client('2019-10')
  72. assert_instance_of(::GraphQL::Client, client)
  73. query = client.parse(<<~GRAPHQL)
  74. {
  75. product {
  76. name
  77. }
  78. }
  79. GRAPHQL
  80. path = ShopifyAPI::ApiVersion.new('2019-10').construct_graphql_path
  81. stub_request(:post, "#{@site}#{path}").to_return(body: { product: { name: 'Shirt' } }.to_json)
  82. client.query(query)
  83. end
  84. end
  85. test '#client raises exception for version that does not exist' do
  86. version_fixtures('unstable') do |_dir|
  87. ShopifyAPI::Base.api_version = '2019-10'
  88. ShopifyAPI::GraphQL.initialize_clients
  89. assert_raises(ShopifyAPI::GraphQL::InvalidClient) do
  90. ShopifyAPI::GraphQL.client('2019-10')
  91. end
  92. end
  93. end
  94. test '#client lazily initializes clients' do
  95. version_fixtures('unstable') do |_dir|
  96. ShopifyAPI::Base.api_version = 'unstable'
  97. assert_raises(ShopifyAPI::GraphQL::InvalidClient) do
  98. ShopifyAPI::GraphQL.client('2019-10')
  99. end
  100. end
  101. end
  102. test '#client caches lookups' do
  103. version_fixtures('unstable') do |_dir|
  104. ShopifyAPI::Base.api_version = 'unstable'
  105. client1 = ShopifyAPI::GraphQL.client
  106. client2 = ShopifyAPI::GraphQL.client('unstable')
  107. assert_equal(client1, client2)
  108. end
  109. end
  110. test '#client creates execution adapter based off configured class' do
  111. class SuperDuperExecutionAdapter < ShopifyAPI::GraphQL::HTTPClient
  112. end
  113. ShopifyAPI::GraphQL.execution_adapter = SuperDuperExecutionAdapter
  114. version_fixtures('unstable') do |_dir|
  115. ShopifyAPI::Base.api_version = 'unstable'
  116. ShopifyAPI::GraphQL.initialize_clients
  117. assert_instance_of(SuperDuperExecutionAdapter, ShopifyAPI::GraphQL.client('unstable').execute)
  118. end
  119. ShopifyAPI::GraphQL.execution_adapter = nil
  120. end
  121. test '#client creates client based off configured class' do
  122. class SuperDuperClient < ::GraphQL::Client
  123. end
  124. ShopifyAPI::GraphQL.graphql_client = SuperDuperClient
  125. version_fixtures('unstable') do |_dir|
  126. ShopifyAPI::Base.api_version = 'unstable'
  127. ShopifyAPI::GraphQL.initialize_clients
  128. assert_instance_of(SuperDuperClient, ShopifyAPI::GraphQL.client('unstable'))
  129. end
  130. ShopifyAPI::GraphQL.clear_clients
  131. ShopifyAPI::GraphQL.graphql_client = nil
  132. end
  133. private
  134. def version_fixtures(*versions)
  135. Dir.mktmpdir do |dir|
  136. versions.each do |version|
  137. FileUtils.cp(@fixture_location.join("#{version}.json"), dir)
  138. end
  139. ShopifyAPI::GraphQL.schema_location = dir
  140. yield(dir)
  141. end
  142. end
  143. end