public function saveVertex($graph, $document, $collection = null)
    {
        if ($graph instanceof Graph) {
            $graph = $graph->getKey();
        }

        if (is_array($document)) {
            $_documentClass = $this->_documentClass;

            $document = $_documentClass::createFromArray($document);
        }

        if ($collection === null) {
            $vertexCollections      = $this->getVertexCollections($graph);
            $vertexCollectionsCount = count($vertexCollections);
            if ($vertexCollectionsCount !== 1) {
                throw new ClientException('A collection must be provided.');
            }

            if ($vertexCollectionsCount === 1) {
                $collection = $vertexCollections[0];
            }
        }

        $data = $document->getAll();
        $url  = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_VERTEX, $collection]);

        $response = $this->getConnection()->post($url, $this->json_encode_wrapper($data));

        // This makes sure that if we're in batch mode, it will not go further and choke on the checks below.
        // Caution: Instead of a document ID, we are returning the batchpart object.
        // The Id of the BatchPart can be retrieved by calling getId() on it.
        // We're basically returning an object here, in order not to accidentally use the batch part id as the document id
        if ($batchPart = $response->getBatchPart()) {
            return $batchPart;
        }

        $jsonArray = $response->getJson();
        $vertex    = $jsonArray['vertex'];

        $document->setInternalId($vertex[Vertex::ENTRY_ID]);
        $document->setRevision($vertex[Vertex::ENTRY_REV]);

        $document->setIsNew(false);

        return $document->getInternalId();
    }