Skip to content

Modify the graph

Now that you have run the full example, you can practice Gremlin mutations (write operations that change the graph, unlike the read-only queries from the previous page). In this activity you will:

  • Add a merchant vertex and connect accounts with addV/addE
  • Update user loyalty tiers with property
  • Remove low-value transactions with drop

These changes live in a copy of the example script so you can experiment freely.

Create a working copy

  1. Copy the example script so you can experiment without touching the original file. Run the command inside python/basic.

    Terminal window
    cp example.py example_mutations.py
  2. Open example_mutations.py in your editor.

Add mutation helpers

  1. Paste the apply_mutations helper just above the if __name__ == "__main__": block at the bottom of the file.

    def apply_mutations(g):
    # Mutation 1: addV/addE for merchant purchases
    print("\nMUTATION 1: Created merchant vertex and connected accounts A1 and A3 with purchased_from edges.")
    merchant = g.add_v("Merchant") \
    .property("merchantId", "M1") \
    .property("name", "Pacific Outfitters") \
    .property("industry", "Retail") \
    .next()
    for account_id, amount in [("A1", 640), ("A3", 430)]:
    account = g.V().hasLabel("Account").has("accountId", account_id).next()
    g.add_e("purchased_from") \
    .from_(account) \
    .to(merchant) \
    .property("amount", amount) \
    .property("currency", "USD") \
    .iterate()
    # Mutation 2: property updates for loyalty tiers
    print("MUTATION 2: Set loyaltyTier=PLATINUM for Alice and loyaltyTier=GOLD for Bob.")
    g.V().hasLabel("User").has("name", "Alice").property("loyaltyTier", "PLATINUM").iterate()
    g.V().hasLabel("User").has("name", "Bob").property("loyaltyTier", "GOLD").iterate()
    # Mutation 3: drop low-value transactions
    print("MUTATION 3: Removing Transaction edges with amount < 200.")
    low_value = g.E().hasLabel("Transaction").has("amount", P.lt(200))
    removed = low_value.count().next()
    if removed:
    g.E().hasLabel("Transaction").has("amount", P.lt(200)).drop().iterate()
    print(f"Removed {removed} Transaction edge(s) below the threshold.")
    else:
    print("No Transaction edges fell below the threshold.")
    # Mutation summary: read back the changes
    print("\nVERIFY MUTATIONS: Reading back new edges and loyalty tiers before cleanup.")
    merchant_edges = g.E().hasLabel("purchased_from") \
    .valueMap("amount", "currency").toList()
    loyalty = g.V().hasLabel("User").has("loyaltyTier") \
    .valueMap("name", "loyaltyTier").toList()
    print(f"Merchant edges: {merchant_edges}")
    print(f"Loyalty tiers: {loyalty}")

    This helper mirrors the mutation patterns demonstrated on the Query usage page by creating new elements, updating properties, and deleting edges:

    • Mutation 1 – addV/addE adds a Merchant vertex and connects accounts with purchased_from edges so you can see how new entities are introduced into the existing graph.
    • Mutation 2 – property overwrites the loyaltyTier property on specific users, demonstrating how repeated runs simply update the same vertices.
    • Mutation 3 – drop removes every Transaction edge below the threshold, letting you measure the impact with the pre-drop count.
    • Mutation summary uses valueMap to print the new merchant edges and loyalty tiers before the dataset is cleared.
  2. Call the helper from main().

    Inside the main() function (right after the four query printouts and before g.V().drop().iterate()), add:

    apply_mutations(g)

    Leave the cleanup logic in place so the script still drops all vertices and edges before exiting. The existing if __name__ == "__main__": block can remain unchanged because it already calls main().

Run and verify

  1. Execute the mutated example.

    Terminal window
    python3 example_mutations.py
    ...
    QUERY 4: Properties of Bob:
    age: 35
    name: Bob
    userId: U2
    MUTATION 1: Created merchant vertex and connected accounts A1 and A3 with purchased_from edges.
    MUTATION 2: Set loyaltyTier=PLATINUM for Alice and loyaltyTier=GOLD for Bob.
    MUTATION 3: Removing Transaction edges with amount < 200.
    Removed 9 Transaction edge(s) below the threshold.
    VERIFY MUTATIONS: Reading back new edges and loyalty tiers before cleanup.
    Merchant edges: [{'amount': 430, 'currency': 'USD'}, {'amount': 640, 'currency': 'USD'}]
    Loyalty tiers: [{'loyaltyTier': ['PLATINUM'], 'name': ['Alice']}, {'loyaltyTier': ['GOLD'], 'name': ['Bob']}]
    Dropping Dataset.
    Closing Connection...
    Example response

    The sample output shows the three mutation steps logging their work. Because the script drops the dataset afterward, printing the verification lists is the easiest way to inspect the temporary changes.

  2. Confirm the output lists the three mutation steps before the teardown lines. Rerun the script any time you want to adjust the mutation helper or practice additional addV, property, and drop variations.

Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?