/** * We get the node interface and field from the relay library. * * The first method is the way we resolve an ID to its object. The second is the * way we resolve an object that implements node to its type. */ protected static function getNodeDefinition() { if (self::$nodeDefinition === null) { $nodeDefinition = Relay::nodeDefinitions(function ($globalId) { $idComponents = Relay::fromGlobalId($globalId); if ($idComponents['type'] === 'Faction') { return StarWarsData::getFaction($idComponents['id']); } else { if ($idComponents['type'] === 'Ship') { return StarWarsData::getShip($idComponents['id']); } else { return null; } } }, function ($object) { return isset($object['ships']) ? self::getFactionType() : self::getShipType(); }); self::$nodeDefinition = $nodeDefinition; } return self::$nodeDefinition; }