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
-
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 -
Open
example_mutations.pyin your editor.
Add mutation helpers
-
Paste the
apply_mutationshelper just above theif __name__ == "__main__":block at the bottom of the file.def apply_mutations(g):# Mutation 1: addV/addE for merchant purchasesprint("\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 tiersprint("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 transactionsprint("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 changesprint("\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
Merchantvertex and connects accounts withpurchased_fromedges so you can see how new entities are introduced into the existing graph. - Mutation 2 – property overwrites the
loyaltyTierproperty on specific users, demonstrating how repeated runs simply update the same vertices. - Mutation 3 – drop removes every
Transactionedge below the threshold, letting you measure the impact with the pre-drop count. - Mutation summary uses
valueMapto print the new merchant edges and loyalty tiers before the dataset is cleared.
- Mutation 1 – addV/addE adds a
-
Call the helper from
main().Inside the
main()function (right after the four query printouts and beforeg.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 callsmain().
Run and verify
-
Execute the mutated example.
Terminal window python3 example_mutations.py...QUERY 4: Properties of Bob:age: 35name: BobuserId: U2MUTATION 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.
-
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, anddropvariations.