(Advanced) Conditional Subgraph Execution

There will be times when you actually only want to execute some portion of the graph upon satisfying some condition. In this case, you may inject the node to a procedure expecting a provider<future<...>> so that you may conditionally trigger execution yourself after checking the condition:

Fig 1:


graph function getHomepage(userId : UserId) -> future<Homepage> {
    root homepage <- renderPage(@basePage, @maybeUpgradeBanner);
    node basePage <- getBasePageFromDB();
    node maybeUpgradeBanner
        <- getOptionalUpgradeBannerFromDB(
               @userIsPremium,
               @upgradeBanner  # <-- "Lazy Subgraph" injection requested.
           );
    node userIsPremium <- checkPremiumFromDB(userId);
    node upgradeBanner <- getUpgradeBannerFromDB(@specialOffers);
    node specialOffers <- getSpecialOffers();
}

function getOptionalUpgradeBannerFromDB(
    alreadyPremium: boolean,
    getUpgradeBannerFromDBProvider: provider<future<Html>>
) -> future<Html> {
    if (alreadyPremium) {
        return futures::immediateFuture(getBasicBanner());
    }
    # Actually invoke the lazy subgraph here since the user isn't premium.
    return getUpgradeBannerFromDBProvider();
}

function getUpgradeBannerFromDB(specialOffers: [Offer]) -> future<Html> {
  # ...
  # Just an example - real implementation would send DB query.
  _ = specialOffers;
  return futures::immediateFuture(Html("<div>some html</div>"));
}

provider getSpecialOffers() -> future<[Offer]> {
  # ...
  # Just an example - real implementation would send an RPC to some downstream service.
  var res: [Offer] = [];
  return futures::immediateFuture(res);
}

# ...

Read closely above. The getOptionalUpgradeBannerFromDB() function above expects an arg of type provider<future<Html>> which is injected as a lazy subgraph rooted at node upgradeBanner. In this way, two of the nodes within the overall getHomepage() graph will only run conditionally upon determining that the user is not already a "premium" member.

    graph TD
    basePage --> homePage
    maybeUpgradeBanner --> homePage
    userIsPremium --> maybeUpgradeBanner
    upgradeBanner -.-> maybeUpgradeBanner
    specialOffers -.-> upgradeBanner
    subgraph Conditional Subgraph
        upgradeBanner
        specialOffers
    end