public static function typeNameMetaFieldDef()
{
if (!isset(self::$map['__typename'])) {
self::$map['__typename'] = FieldDefinition::create(['name' => '__typename', 'type' => Type::nonNull(Type::string()), 'description' => 'The name of the current Object type at runtime.', 'args' => [], 'resolve' => function ($source, $args, $context, ResolveInfo $info) {
return $info->parentType->name;
}]);
}
return self::$map['__typename'];
}
/** * Not exactly the same as the executor's definition of getFieldDef, in this * statically evaluated environment we do not always have an Object type, * and need to handle Interface and Union types. * * @return FieldDefinition */ private static function _getFieldDef(Schema $schema, Type $parentType, Field $fieldAST) { $name = $fieldAST->name->value; $schemaMeta = Introspection::schemaMetaFieldDef(); if ($name === $schemaMeta->name && $schema->getQueryType() === $parentType) { return $schemaMeta; } $typeMeta = Introspection::typeMetaFieldDef(); if ($name === $typeMeta->name && $schema->getQueryType() === $parentType) { return $typeMeta; } $typeNameMeta = Introspection::typeNameMetaFieldDef(); if ($name === $typeNameMeta->name && ($parentType instanceof ObjectType || $parentType instanceof InterfaceType || $parentType instanceof UnionType)) { return $typeNameMeta; } if ($parentType instanceof ObjectType || $parentType instanceof InterfaceType) { $fields = $parentType->getFields(); return isset($fields[$name]) ? $fields[$name] : null; } return null; }