Skip to content

Commit d914619

Browse files
authored
Merge pull request #985 from thedustin/fix-missing-is-type-of
Adds isTypeOf to TypeBuilder
2 parents cc3a0fe + 1b5b182 commit d914619

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/Generator/TypeBuilder.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GraphQL\Type\Definition\InputObjectType;
1111
use GraphQL\Type\Definition\InterfaceType;
1212
use GraphQL\Type\Definition\ObjectType;
13+
use GraphQL\Type\Definition\ResolveInfo;
1314
use GraphQL\Type\Definition\Type;
1415
use GraphQL\Type\Definition\UnionType;
1516
use Murtukov\PHPCodeGenerator\ArrowFunction;
@@ -329,6 +330,10 @@ protected function buildConfig(array $config): Collection
329330
$configLoader->addItem('resolveType', $this->buildResolveType($c->resolveType));
330331
}
331332

333+
if (isset($c->isTypeOf)) {
334+
$configLoader->addItem('isTypeOf', $this->buildIsTypeOf($c->isTypeOf));
335+
}
336+
332337
if (isset($c->resolveField)) {
333338
$configLoader->addItem('resolveField', $this->buildResolve($c->resolveField));
334339
}
@@ -923,6 +928,30 @@ protected function buildResolveType($resolveType)
923928
return $resolveType;
924929
}
925930

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+
926955
/**
927956
* Creates and array from a formatted string.
928957
*

0 commit comments

Comments
 (0)