public updateVertexProperties()

in src/Explorer/Graph/GraphExplorerComponent/GraphExplorer.tsx [330:420]


  public updateVertexProperties(editedProperties: EditedProperties): Q.Promise<GremlinClient.GremlinRequestResult> {
    const partitionKeyProperty = this.props.collectionPartitionKeyProperty;

    // aggregate all the properties, remove dropped ones
    const finalProperties = editedProperties.existingProperties.concat(editedProperties.addedProperties);

    // Compose the query
    const pkId = editedProperties.pkId;
    let updateQueryFragment = "";

    finalProperties.forEach((p) => {
      // Partition key cannot be updated
      if (p.key === partitionKeyProperty) {
        return;
      }

      // DO NOT UPDATE Multi-value properties (as this is not supported)
      if (p.values.length === 1) {
        updateQueryFragment += `.Property("${GraphUtil.escapeDoubleQuotes(p.key)}", ${GraphUtil.getQuotedPropValue(
          p.values[0],
        )})`;
      }
    });

    let promise: Q.Promise<GremlinClient.GremlinRequestResult> = null;

    // Compute dropped keys (partition key cannot be dropped)
    const droppedKeys: string[] = [];
    $.each(editedProperties.droppedKeys, (index: number, key: string) => {
      if (key !== partitionKeyProperty) {
        droppedKeys.push(key);
      }
    });

    if (updateQueryFragment.length === 0 && droppedKeys.length === 0) {
      GraphExplorer.reportToConsole(ConsoleDataType.Info, "Nothing to update");
      this.setNodePropertiesViewMode(NodeProperties.Mode.READONLY_PROP);
      return Q.resolve({
        data: [],
        isIncomplete: false,
      });
    }

    if (droppedKeys.length > 0) {
      // TODO Wait for dropping to end. Can we drop all of them in a single query?
      // Must execute these drops sequentially to avoid a 500 "{"Message":"An error has occurred."}"
      promise = this.submitToBackend(
        `g.V(${pkId}).properties("${GraphUtil.escapeDoubleQuotes(droppedKeys[0])}").drop()`,
      );
      for (let i = 1; i < droppedKeys.length; i++) {
        promise = promise.then(() => {
          return this.submitToBackend(
            `g.V(${pkId}).properties("${GraphUtil.escapeDoubleQuotes(droppedKeys[i])}").drop()`,
          );
        });
      }
    } else {
      promise = Q.resolve({
        data: [],
        isIncomplete: false,
      });
    }

    // Now when drops are done, update remaining properties
    // Note: execute g.V(id) if updateQueryFragment is '' (nothing to update), in order to get a vertex response
    // to update the in-memory graph
    promise = promise.then(() => {
      return this.submitToBackend(`g.V(${pkId})${updateQueryFragment}`);
    });

    // Update in-memory graph and close editor
    promise
      .then((result: GremlinClient.GremlinRequestResult) => {
        // Update graph style dropdown options
        this.collectNodeProperties(this.originalGraphData.vertices);

        // Update graph (in case property is being shown)
        this.updateInMemoryGraph(result.data);
        this.updateGraphData(this.originalGraphData, this.state.igraphConfig);
      })
      .then(
        () => {
          this.setNodePropertiesViewMode(NodeProperties.Mode.READONLY_PROP);
        },
        (error: string) => {
          GraphExplorer.reportToConsole(ConsoleDataType.Error, "Failed to update vertex properties: " + error);
        },
      );

    return promise;
  }