|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace TheCodingMachine\GraphQLite\Mappers\Proxys; |
| 5 | + |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use GraphQL\Error\InvariantViolation; |
| 9 | +use GraphQL\Type\Definition\FieldDefinition; |
| 10 | +use GraphQL\Type\Definition\InterfaceType; |
| 11 | +use GraphQL\Type\Definition\ObjectType; |
| 12 | +use GraphQL\Type\Definition\ResolveInfo; |
| 13 | +use RuntimeException; |
| 14 | +use TheCodingMachine\GraphQLite\Types\MutableInterface; |
| 15 | +use TheCodingMachine\GraphQLite\Types\NoFieldsException; |
| 16 | +use function get_class; |
| 17 | + |
| 18 | +/** |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +trait MutableAdapterTrait |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var ObjectType|InterfaceType |
| 25 | + */ |
| 26 | + private $type; |
| 27 | + /** |
| 28 | + * @var string|null |
| 29 | + */ |
| 30 | + private $className; |
| 31 | + |
| 32 | + /** @var string */ |
| 33 | + private $status = MutableInterface::STATUS_PENDING; |
| 34 | + |
| 35 | + /** @var array<callable> */ |
| 36 | + private $fieldsCallables = []; |
| 37 | + |
| 38 | + /** @var FieldDefinition[]|null */ |
| 39 | + private $finalFields; |
| 40 | + |
| 41 | + /** |
| 42 | + * @throws InvariantViolation |
| 43 | + */ |
| 44 | + public function assertValid(): void |
| 45 | + { |
| 46 | + $this->type->assertValid(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + public function jsonSerialize() |
| 53 | + { |
| 54 | + return $this->type->jsonSerialize(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return string |
| 59 | + */ |
| 60 | + public function toString() |
| 61 | + { |
| 62 | + return $this->type->toString(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return string |
| 67 | + */ |
| 68 | + public function __toString() |
| 69 | + { |
| 70 | + return $this->type->__toString(); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * @param string $name |
| 76 | + * |
| 77 | + * @return FieldDefinition |
| 78 | + * |
| 79 | + * @throws Exception |
| 80 | + */ |
| 81 | + public function getField($name): FieldDefinition |
| 82 | + { |
| 83 | + if ($this->status === MutableInterface::STATUS_PENDING) { |
| 84 | + throw new RuntimeException('You must freeze() a '.get_class($this).' before fetching its fields.'); |
| 85 | + } |
| 86 | + |
| 87 | + return $this->type->getField($name); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param string $name |
| 92 | + * |
| 93 | + * @return bool |
| 94 | + */ |
| 95 | + public function hasField($name): bool |
| 96 | + { |
| 97 | + if ($this->status === MutableInterface::STATUS_PENDING) { |
| 98 | + throw new RuntimeException('You must freeze() a '.get_class($this).' before fetching its fields.'); |
| 99 | + } |
| 100 | + |
| 101 | + return $this->type->hasField($name); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return FieldDefinition[] |
| 106 | + * |
| 107 | + * @throws InvariantViolation |
| 108 | + */ |
| 109 | + public function getFields(): array |
| 110 | + { |
| 111 | + if ($this->finalFields === null) { |
| 112 | + if ($this->status === MutableInterface::STATUS_PENDING) { |
| 113 | + throw new RuntimeException('You must freeze() a '.get_class($this).' before fetching its fields.'); |
| 114 | + } |
| 115 | + |
| 116 | + $this->finalFields = $this->type->getFields(); |
| 117 | + foreach ($this->fieldsCallables as $fieldsCallable) { |
| 118 | + $this->finalFields = FieldDefinition::defineFieldMap($this, $fieldsCallable()) + $this->finalFields; |
| 119 | + } |
| 120 | + if (empty($this->finalFields)) { |
| 121 | + throw NoFieldsException::create($this->name); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return $this->finalFields; |
| 126 | + } |
| 127 | + |
| 128 | + public function freeze(): void |
| 129 | + { |
| 130 | + $this->status = self::STATUS_FROZEN; |
| 131 | + } |
| 132 | + |
| 133 | + public function getStatus(): string |
| 134 | + { |
| 135 | + return $this->status; |
| 136 | + } |
| 137 | + |
| 138 | + public function addFields(callable $fields): void |
| 139 | + { |
| 140 | + if ($this->status !== MutableInterface::STATUS_PENDING) { |
| 141 | + throw new RuntimeException('Tried to add fields to a frozen MutableInterfaceType.'); |
| 142 | + } |
| 143 | + $this->fieldsCallables[] = $fields; |
| 144 | + } |
| 145 | +} |
0 commit comments