Skip to content

Example 4 Route with BranchPipe

Fernando Koch edited this page Mar 30, 2026 · 3 revisions

Example 4: Route With BranchPipe

Goal

Understand how GraphK routes one request to one matching branch.

Scenario

A request reaches your system, but not every request should follow the same path. You need one route to win and execute.

Example

import graphk


branch = graphk.BranchPipe(
    nodes=[
        (graphk.Matcher(task="route-a"), graphk.SequencePipe(nodes=[graphk.demo.RouteNode("A")])),
        (graphk.Matcher(task="route-b"), graphk.SequencePipe(nodes=[graphk.demo.RouteNode("B")])),
        (graphk.Matcher(task="route-c"), graphk.SequencePipe(nodes=[graphk.demo.RouteNode("C")])),
    ],
)

program = graphk.Program(
    {"route": graphk.SequencePipe(nodes=[branch])}
)
emitter = graphk.Emitter(program)

emitter.request("route", graphk.Session(task="route-b"))
print(emitter.response())

What Happens?

  • the emitter selects the route pipeline from the program
  • all branch tests are checked
  • the matching branch is selected
  • only one branch executes

Why This Example Matters

This is GraphK’s main exclusive-routing pattern.

Run The Full Example

Clone this wiki locally