vendor/webonyx/graphql-php/src/Type/Definition/TypeWithFields.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace GraphQL\Type\Definition;
  4. use GraphQL\Utils\Utils;
  5. use function array_keys;
  6. abstract class TypeWithFields extends Type implements HasFieldsType
  7. {
  8.     /**
  9.      * Lazily initialized.
  10.      *
  11.      * @var array<string, FieldDefinition>
  12.      */
  13.     private $fields;
  14.     private function initializeFields() : void
  15.     {
  16.         if (isset($this->fields)) {
  17.             return;
  18.         }
  19.         $fields       $this->config['fields'] ?? [];
  20.         $this->fields FieldDefinition::defineFieldMap($this$fields);
  21.     }
  22.     public function getField(string $name) : FieldDefinition
  23.     {
  24.         Utils::invariant($this->hasField($name), 'Field "%s" is not defined for type "%s"'$name$this->name);
  25.         return $this->findField($name);
  26.     }
  27.     public function findField(string $name) : ?FieldDefinition
  28.     {
  29.         $this->initializeFields();
  30.         if (! isset($this->fields[$name])) {
  31.             return null;
  32.         }
  33.         if ($this->fields[$name] instanceof UnresolvedFieldDefinition) {
  34.             $this->fields[$name] = $this->fields[$name]->resolve();
  35.         }
  36.         return $this->fields[$name];
  37.     }
  38.     public function hasField(string $name) : bool
  39.     {
  40.         $this->initializeFields();
  41.         return isset($this->fields[$name]);
  42.     }
  43.     /** @inheritDoc */
  44.     public function getFields() : array
  45.     {
  46.         $this->initializeFields();
  47.         foreach ($this->fields as $name => $field) {
  48.             if (! ($field instanceof UnresolvedFieldDefinition)) {
  49.                 continue;
  50.             }
  51.             $this->fields[$name] = $field->resolve();
  52.         }
  53.         return $this->fields;
  54.     }
  55.     /** @inheritDoc */
  56.     public function getFieldNames() : array
  57.     {
  58.         $this->initializeFields();
  59.         return array_keys($this->fields);
  60.     }
  61. }