SchemaGenerator.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Framework\GraphQl\Schema;
  8. use Magento\Framework\GraphQl\ConfigInterface;
  9. use Magento\Framework\GraphQl\Schema;
  10. use Magento\Framework\GraphQl\Schema\Type\TypeRegistry;
  11. use Magento\Framework\GraphQl\SchemaFactory;
  12. /**
  13. * Generate a query field and concrete types for GraphQL schema
  14. */
  15. class SchemaGenerator implements SchemaGeneratorInterface
  16. {
  17. /**
  18. * @var SchemaFactory
  19. */
  20. private $schemaFactory;
  21. /**
  22. * @var ConfigInterface
  23. */
  24. private $config;
  25. /**
  26. * @var TypeRegistry
  27. */
  28. private $typeRegistry;
  29. /**
  30. * @param SchemaFactory $schemaFactory
  31. * @param ConfigInterface $config
  32. * @param TypeRegistry $typeRegistry
  33. */
  34. public function __construct(
  35. SchemaFactory $schemaFactory,
  36. ConfigInterface $config,
  37. TypeRegistry $typeRegistry
  38. ) {
  39. $this->schemaFactory = $schemaFactory;
  40. $this->config = $config;
  41. $this->typeRegistry = $typeRegistry;
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function generate() : Schema
  47. {
  48. $schema = $this->schemaFactory->create(
  49. [
  50. 'query' => $this->typeRegistry->get('Query'),
  51. 'mutation' => $this->typeRegistry->get('Mutation'),
  52. 'typeLoader' => function ($name) {
  53. return $this->typeRegistry->get($name);
  54. },
  55. 'types' => function () {
  56. $typesImplementors = [];
  57. foreach ($this->config->getDeclaredTypes() as $type) {
  58. $typesImplementors [] = $this->typeRegistry->get($type['name']);
  59. }
  60. return $typesImplementors;
  61. }
  62. ]
  63. );
  64. return $schema;
  65. }
  66. }