Skip to content

Commit f0c906b

Browse files
committed
add __typename field to GraphQLObjectType
1 parent 152a2e7 commit f0c906b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Types/GraphQLObjectType.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ class GraphQLObjectType extends GraphQLType
1212
protected $description;
1313

1414
private $fields;
15+
private $internalFields;
1516
private $interfaces;
1617
private $isTypeOfFn;
1718

1819
public function __construct(string $type, string $description, \Closure $fields, ?array $interfaces = null, ?\Closure $isTypeOfFn = null)
1920
{
20-
$bt = debug_backtrace();
21-
$caller = array_shift($bt); // Get first array
22-
2321
$this->type = $type;
2422
$this->description = $description;
2523

2624
$this->fields = $fields;
25+
26+
// create internal fields
27+
$this->internalFields = [
28+
"__typename" => new GraphQLTypeField("__typename", new GraphQLNonNull(new GraphQLString()), "Name of the type of the object", function () use ($type) {
29+
return $type;
30+
})
31+
];
32+
33+
2734
$this->interfaces = $interfaces ?? [];
2835
$this->isTypeOfFn = $isTypeOfFn ?? function ($value, $contextValue, $info) {
2936
// if $value is array, check if all keys match
@@ -75,6 +82,8 @@ public function getFields(): array
7582
foreach ($allFields as $field) {
7683
$fields[$field->getName()] = $field;
7784
}
85+
86+
$fields = array_merge($fields, $this->internalFields);
7887
return $fields;
7988
}
8089

0 commit comments

Comments
 (0)