By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
The pricing logic — quote an order and apply a member discount — is useful in the support graph and in checkout, so it deserves to be its own graph rather than a helper that both code paths copy. LangGraph composes graphs the same way it composes functions: a compiled graph is a valid node. The interesting part is the boundary. The child has its own state schema with its own private keys, the parent has a different schema, and something has to map between them in both directions. In this lab you build the child, wrap it in a parent node with explicit mapping, and test the child alone as well as the seam.
There are two composition styles, and the difference is who owns the mapping. When the parent and child schemas share the keys they need, you can add the compiled child directly as a node and let the runtime pass the overlapping channels through. When the schemas differ — as they usually do once the child is reused across products — wrap the child in a node function that maps parent state in, calls the child graph and returns only the public result. The wrapper is where defaults, validation and key renames live.
The child graph is ordinary LangGraph: a TypedDict with the three keys it needs, two nodes and a linear edge. It compiles and runs on its own, which is the first test to write — if the child cannot be exercised without the parent, the boundary is in the wrong place.
from typing import TypedDict
from langgraph.graph import END, START, StateGraph
class PricingState(TypedDict):
query: str
price_cents: int
discount_cents: int
def quote(state: PricingState) -> dict:
# A real node would read the catalogue under this child's own state.
return {"price_cents": 4200}
def apply_discount(state: PricingState) -> dict:
member = "member" in state["query"].lower()
return {"discount_cents": 500 if member else 0}
builder = StateGraph(PricingState)
builder.add_node("quote", quote)
builder.add_node("apply_discount", apply_discount)
builder.add_edge(START, "quote")
builder.add_edge("quote", "apply_discount")
builder.add_edge("apply_discount", END)
pricing_graph = builder.compile()
member = pricing_graph.invoke(
{"query": "member quote", "price_cents": 0, "discount_cents": 0}
)
guest = pricing_graph.invoke(
{"query": "quote please", "price_cents": 0, "discount_cents": 0}
)
print(member["price_cents"], member["discount_cents"])
print(guest["price_cents"], guest["discount_cents"])The wrapper node below renames message to query, supplies the child's initial values and returns only amount_due_cents. That last choice is the important one: the parent's schema gains the answer it needs, not the child's internals. If the child later renames price_cents, only the wrapper and the child's tests change.
from typing import TypedDict
from langgraph.graph import END, START, StateGraph
from pricing_graph import pricing_graph # the compiled child from above
class SupportState(TypedDict):
message: str
amount_due_cents: int
reply: str
def run_pricing(state: SupportState) -> dict:
"""The boundary: parent keys in, child result out."""
child = pricing_graph.invoke(
{"query": state["message"], "price_cents": 0, "discount_cents": 0}
)
return {"amount_due_cents": child["price_cents"] - child["discount_cents"]}
def draft_reply(state: SupportState) -> dict:
return {"reply": f"${state['amount_due_cents'] / 100:.2f} due"}
builder = StateGraph(SupportState)
builder.add_node("pricing", run_pricing)
builder.add_node("draft_reply", draft_reply)
builder.add_edge(START, "pricing")
builder.add_edge("pricing", "draft_reply")
builder.add_edge("draft_reply", END)
support_graph = builder.compile()
result = support_graph.invoke(
{"message": "member quote", "amount_due_cents": 0, "reply": ""}
)
print(result["amount_due_cents"])
print(result["reply"])
print("price_cents" in result)The parent result has amount_due_cents and reply, and the False line proves the child's private key did not leak into the parent state. That is the boundary working. When the schemas do overlap enough to add the child directly with builder.add_node("pricing", pricing_graph), the same review applies: know which keys pass through, which are lost and which are required by the child, and still validate the seam in a test.
| Key | Owner | Crosses the boundary? |
|---|---|---|
message | Parent | Parent input only; renamed to query by the wrapper |
query | Child | Mapped in by the wrapper; never appears in parent state |
price_cents, discount_cents | Child | Private to the child; not returned by the wrapper |
amount_due_cents | Parent | The mapped result: the only pricing value the parent keeps |
reply | Parent | Final answer, built from the mapped result |
Test the child graph by itself so its rules are covered without the parent, then test the parent for the mapping. Two assertions make the boundary a contract rather than a convention: a child-only key must not appear in the parent's result, and the mapped value must equal the child's public result. A third test covers the default supplied at the boundary, and a fourth covers the non-member rule end to end.
import pytest
from pricing_graph import pricing_graph
from support_graph import support_graph
def test_child_graph_works_alone():
result = pricing_graph.invoke(
{"query": "member quote", "price_cents": 0, "discount_cents": 0}
)
assert result["price_cents"] == 4200
assert result["discount_cents"] == 500
def test_parent_maps_only_the_public_result():
result = support_graph.invoke(
{"message": "member quote", "amount_due_cents": 0, "reply": ""}
)
assert result["amount_due_cents"] == 3700
assert "price_cents" not in result
assert "discount_cents" not in result
def test_non_member_pays_the_list_price():
result = support_graph.invoke(
{"message": "quote please", "amount_due_cents": 0, "reply": ""}
)
assert result["amount_due_cents"] == 4200
def test_boundary_supplies_the_child_defaults():
child = pricing_graph.invoke(
{"query": "quote please", "price_cents": 0, "discount_cents": 0}
)
assert child["discount_cents"] == 0Key takeaways
A compiled graph is a valid node: composition is how multi-purpose logic becomes a reusable component instead of copied code.
Parent and child have separate state schemas; the boundary maps parent state in and the public result out, and nothing else should cross.
Prefer an explicit wrapper node when schemas differ, and add the child directly only when the shared keys are genuinely the contract.
Test the child alone, then test the seam: mapped value correct, private keys absent, defaults supplied.
A clear boundary lets the child evolve (rename a key, add a node) without changing the parent or its tests.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.