GraphQlIntrospectionTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl;
  8. use Magento\Framework\GraphQl\Schema\Type\InputObjectType;
  9. use Magento\Framework\GraphQl\Schema\Type\ObjectType;
  10. use Magento\Framework\ObjectManagerInterface;
  11. class GraphQlIntrospectionTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Framework\GraphQl\SchemaFactory */
  14. private $schemaFactory;
  15. /** @var ObjectManagerInterface */
  16. private $objectManager;
  17. protected function setUp()
  18. {
  19. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  20. $this->schemaFactory = $this->objectManager->get(\Magento\Framework\GraphQl\SchemaFactory::class);
  21. }
  22. public function testIntrospectionQuery()
  23. {
  24. $emptySchema = $this->schemaFactory->create(
  25. [
  26. 'query' => new ObjectType(
  27. [
  28. 'name' => 'Query',
  29. 'description' =>'Description at type level',
  30. 'fields' => ['a' => \GraphQL\Type\Definition\Type::string()]
  31. ]
  32. )
  33. ]
  34. );
  35. $request =
  36. <<<QUERY
  37. query IntrospectionQuery {
  38. __schema {
  39. queryType { name }
  40. types{
  41. ...FullType
  42. }
  43. }
  44. }
  45. fragment FullType on __Type{
  46. name
  47. description
  48. kind
  49. fields(includeDeprecated:true){
  50. name
  51. args{
  52. ...InputValue
  53. }
  54. }
  55. }
  56. fragment TypeRef on __Type {
  57. kind
  58. name
  59. ofType{
  60. kind
  61. name
  62. }
  63. }
  64. fragment InputValue on __InputValue {
  65. name
  66. description
  67. type { ...TypeRef }
  68. defaultValue
  69. }
  70. QUERY;
  71. $response = \GraphQL\GraphQL::executeQuery($emptySchema, $request);
  72. $output = $response->toArray()['data']['__schema'];
  73. $this->assertEquals('Query', $output['queryType']['name']);
  74. $this->assertEquals($output['types'][0]['kind'], 'OBJECT');
  75. $expectedFragment =
  76. [
  77. 'name' => 'Query',
  78. 'description' => 'Description at type level',
  79. 'kind' => 'OBJECT',
  80. 'fields' => [
  81. [
  82. 'name' => 'a',
  83. 'args' => []
  84. ]
  85. ]
  86. ];
  87. $this->assertContains($expectedFragment, $output['types']);
  88. }
  89. /**
  90. * Tests an InputObjectType with NON Null field and description at Field level
  91. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  92. */
  93. public function testIntrospectsInputObjectWithNonNullInputField()
  94. {
  95. $testInputObject = new InputObjectType(
  96. [
  97. 'name' => 'ProductFilterInput',
  98. 'fields' => [
  99. 'attributeA' => [
  100. 'type' => \GraphQL\Type\Definition\Type::nonNull(
  101. \GraphQL\Type\Definition\Type::string()
  102. ),
  103. 'description' => 'testDescriptionForA'
  104. ],
  105. 'attributeB' => [
  106. 'type' => \GraphQL\Type\Definition\Type::listOf(
  107. \GraphQL\Type\Definition\Type::string()
  108. )
  109. ],
  110. 'attributeC' => ['type' => \GraphQL\Type\Definition\Type::string(), 'defaultValue' => null],
  111. 'attributeD' => [
  112. 'type' => \GraphQL\Type\Definition\Type::string(),
  113. 'defaultValue' => 'test',
  114. 'description' => 'testDescriptionForD'
  115. ],
  116. ]
  117. ]
  118. );
  119. $TestType = new ObjectType([
  120. 'name' => 'Query',
  121. 'fields' => [
  122. 'field' => [
  123. 'type' => \GraphQL\Type\Definition\Type::string(),
  124. 'args' => ['complex' => ['type' => $testInputObject]],
  125. 'resolve' => function ($args) {
  126. return json_encode($args['complex']);
  127. }
  128. ]
  129. ]
  130. ]);
  131. $testSchema = $this->schemaFactory->create(
  132. ['query' => $TestType]
  133. );
  134. $request =
  135. <<<QUERY
  136. {
  137. __schema {
  138. types {
  139. kind
  140. name
  141. inputFields {
  142. name
  143. description
  144. type { ...TypeRef }
  145. defaultValue
  146. }
  147. }
  148. }
  149. }
  150. fragment TypeRef on __Type {
  151. kind
  152. name
  153. ofType {
  154. kind
  155. name
  156. ofType {
  157. kind
  158. name
  159. ofType {
  160. kind
  161. name
  162. }
  163. }
  164. }
  165. }
  166. QUERY;
  167. $response = \GraphQL\GraphQL::executeQuery($testSchema, $request);
  168. $expectedResult =
  169. [
  170. 'kind'=> 'INPUT_OBJECT',
  171. 'name'=> 'ProductFilterInput',
  172. 'inputFields'=> [
  173. [
  174. 'name'=> 'attributeA',
  175. 'description'=> 'testDescriptionForA',
  176. 'type'=> [
  177. 'kind'=> 'NON_NULL',
  178. 'name'=> null,
  179. 'ofType'=> [
  180. 'kind'=> 'SCALAR',
  181. 'name'=> 'String',
  182. 'ofType'=> null
  183. ]
  184. ],
  185. 'defaultValue'=> null
  186. ],
  187. [
  188. 'name'=> 'attributeB',
  189. 'description'=> null,
  190. 'type'=> [
  191. 'kind'=> 'LIST',
  192. 'name'=> null,
  193. 'ofType'=> [
  194. 'kind'=> 'SCALAR',
  195. 'name'=> 'String',
  196. 'ofType'=> null
  197. ]
  198. ],
  199. 'defaultValue'=> null
  200. ],
  201. [
  202. 'name'=> 'attributeC',
  203. 'description'=> null,
  204. 'type'=> [
  205. 'kind'=> 'SCALAR',
  206. 'name'=> 'String',
  207. 'ofType'=> null
  208. ],
  209. 'defaultValue'=> 'null'
  210. ],
  211. [
  212. 'name'=> 'attributeD',
  213. 'description'=> 'testDescriptionForD',
  214. 'type'=> [
  215. 'kind'=> 'SCALAR',
  216. 'name'=> 'String',
  217. 'ofType'=> null
  218. ],
  219. 'defaultValue'=> '"test"'
  220. ]
  221. ]
  222. ];
  223. $output = $response->toArray()['data']['__schema']['types'];
  224. $this->assertContains($expectedResult, $output);
  225. }
  226. /**
  227. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  228. */
  229. public function testIntrospectsIncludeTheDeprecatedParameter()
  230. {
  231. $testSchema = $this->schemaFactory->create(
  232. [
  233. 'query' => new ObjectType(
  234. [
  235. 'name' => 'Query',
  236. 'fields' => [
  237. 'deprecated' => [
  238. 'type' => \GraphQL\Type\Definition\Type::string(),
  239. 'deprecationReason' =>'Deprecated in an older version'
  240. ],
  241. 'nonDeprecated' => [
  242. 'type' => \GraphQL\Type\Definition\Type::string()
  243. ]
  244. ]
  245. ]
  246. )
  247. ]
  248. );
  249. $request =
  250. <<<QUERY
  251. {
  252. __type(name:"Query")
  253. {
  254. name
  255. kind
  256. fields(includeDeprecated:true){
  257. name
  258. type{
  259. kind
  260. name
  261. }
  262. description
  263. isDeprecated
  264. deprecationReason
  265. }
  266. }
  267. }
  268. QUERY;
  269. $response = \GraphQL\GraphQL::executeQuery($testSchema, $request);
  270. $output = $response->toArray()['data']['__type'];
  271. $expectedResult =
  272. [
  273. "name" =>"Query",
  274. "kind" =>"OBJECT",
  275. "fields" => [
  276. [
  277. 'name'=> 'deprecated',
  278. 'type'=> [
  279. 'kind'=> 'SCALAR',
  280. 'name'=> 'String'
  281. ],
  282. 'description'=> null,
  283. 'isDeprecated'=> true,
  284. 'deprecationReason'=> 'Deprecated in an older version'
  285. ],
  286. [
  287. 'name'=> 'nonDeprecated',
  288. 'type'=> [
  289. 'kind'=> 'SCALAR',
  290. 'name'=> 'String'
  291. ],
  292. 'description'=> null,
  293. 'isDeprecated'=> false,
  294. 'deprecationReason'=> null
  295. ]
  296. ]
  297. ];
  298. $this->assertEquals($expectedResult, $output);
  299. }
  300. }