|
10 | 10 | use GraphQL\Type\Definition\InputObjectType; |
11 | 11 | use GraphQL\Type\Definition\InterfaceType; |
12 | 12 | use GraphQL\Type\Definition\ObjectType; |
| 13 | +use GraphQL\Type\Definition\ResolveInfo; |
13 | 14 | use GraphQL\Type\Definition\Type; |
14 | 15 | use GraphQL\Type\Definition\UnionType; |
15 | 16 | use Murtukov\PHPCodeGenerator\ArrowFunction; |
@@ -329,6 +330,10 @@ protected function buildConfig(array $config): Collection |
329 | 330 | $configLoader->addItem('resolveType', $this->buildResolveType($c->resolveType)); |
330 | 331 | } |
331 | 332 |
|
| 333 | + if (isset($c->isTypeOf)) { |
| 334 | + $configLoader->addItem('isTypeOf', $this->buildIsTypeOf($c->isTypeOf)); |
| 335 | + } |
| 336 | + |
332 | 337 | if (isset($c->resolveField)) { |
333 | 338 | $configLoader->addItem('resolveField', $this->buildResolve($c->resolveField)); |
334 | 339 | } |
@@ -923,6 +928,30 @@ protected function buildResolveType($resolveType) |
923 | 928 | return $resolveType; |
924 | 929 | } |
925 | 930 |
|
| 931 | + /** |
| 932 | + * Builds an arrow function from a string with an expression prefix, |
| 933 | + * otherwise just returns the provided value back untouched. |
| 934 | + * |
| 935 | + * Render example: |
| 936 | + * |
| 937 | + * fn($className) => (($className = "App\\ClassName") && $value instanceof $className) |
| 938 | + * |
| 939 | + * @param mixed $isTypeOf |
| 940 | + */ |
| 941 | + private function buildIsTypeOf($isTypeOf): ArrowFunction |
| 942 | + { |
| 943 | + if (EL::isStringWithTrigger($isTypeOf)) { |
| 944 | + $expression = $this->expressionConverter->convert($isTypeOf); |
| 945 | + |
| 946 | + return ArrowFunction::new(Literal::new($expression), 'bool') |
| 947 | + ->setStatic() |
| 948 | + ->addArguments('value', 'context') |
| 949 | + ->addArgument('info', ResolveInfo::class); |
| 950 | + } |
| 951 | + |
| 952 | + return ArrowFunction::new($isTypeOf); |
| 953 | + } |
| 954 | + |
926 | 955 | /** |
927 | 956 | * Creates and array from a formatted string. |
928 | 957 | * |
|
0 commit comments