Dynamic Utilities Overview
Author:
Kirill Gaiduk
Changed on:
15 Aug 2025
Overview
The `util-dynamic`
library is a collection of utility functions that allow you to generate GraphQL queries and mutations at runtime. This enables the creation of highly configurable and reusable rules that can adapt to different needs without requiring code changes.
Prerequisites
These articles assumes you're familiar with:
- Java
- Maven
- JUnit
Key points
- Dynamic Queries: Fetch any field from an entity at runtime by providing a POJO class that defines the desired data structure, eliminating the need for pre-compiled queries.
- Dynamic Mutations: Update any field on an entity by passing a simple
`Map`
of key-value pairs, allowing for the creation of generic "field setter" rules. - Dynamic Connections: Easily query and iterate over paginated one-to-many relationships (like
`order.items`
), as the utility handles all the pagination logic for you. - Advanced JSON Path: A powerful
`getJsonPath`
method that can retrieve data from either the event or the entity itself, automatically determining the correct source. - Enhanced JSON Utilities: This bundle includes a more advanced
`JsonUtils`
with powerful methods for navigating and transforming complex JSON structures.
Value Proposition
The primary value of the Dynamic Utilities is flexibility.
- Configurable Rules: Workflow designers can change what data a Rule fetches or updates by modifying Rule properties, without needing a developer to write and deploy new code.
- Reusable Logic: You can create a single, generic rule (e.g., "SetEntityField") that can be used across many different entity types and scenarios, reducing code duplication.
- Adaptive Workflows: Build workflows that can adapt to different conditions at runtime by dynamically querying or updating the data that matters most in a given context.
Apollo Android Context
The Fluent Rules SDK relies on Apollo Android to execute GraphQL queries. Apollo's method of pre-compiling queries into Java classes:
- Enhances type safety
- And boosts performance
However, it also has drawbacks:
- Pre-compiled queries restrict Workflow designers from dynamically accessing or updating data, as they require new queries to be developed and compiled each time.
- Additionally, the pre-compilation process limits the ability to create universal Rules that apply similar logic across different entity types.
The Dynamic Utilities work around these limitations and enhance Rule configurability.
Explanation through an Example
Here's how Dynamic Utilities solve the Apollo limitation by creating a single, reusable rule that can update any field on any entity:
1import com.fluentcommerce.util.dynamic.DynamicUtils;
2import com.fluentretail.api.model.attribute.Attribute;
3import java.util.Map;
4
5public class GenericFieldUpdateRule implements Rule {
6 @Override
7 public void run(Context context) {
8 // Get the field to update from rule properties
9 String fieldName = context.getProp("fieldName");
10 String fieldValue = context.getProp("fieldValue");
11
12 // Create a simple map with the field update
13 Map<String, Object> updates = Map.of(fieldName, fieldValue);
14
15 // Use dynamic mutation to update the entity
16 // This works for ANY entity type without pre-compiled queries
17 DynamicUtils.updateEntity(
18 context,
19 context.getEvent().getEntityId(),
20 updates
21 );
22 }
23}