Back to EIP

PatternSelective consumer filters the messages which come from a message channel using a criteria.
How Ballerina helps

Ballerina supports rich set of messaging protocols. Some of these protocols supports channels with filtering capabilities. Below example uses GraphQL's filtering capabilities to select the data it receives. If a protocol does not support filtering, user may write their own logic using Ballerina control flow constructs such as if-else and match statements.

Selective ConsumerMessage FilterContent Based RouterMessage
Copy
import ballerina/graphql;
import ballerina/io;

type InventoryResponse record {|
    record {|Inventory[] products;|} data;
|};

type Inventory record {|
    string name;
    int productsCount;
|};

type CsvRecord record {|
    string name;
    RequestType requestType;
|};

enum RequestType {
    REQUIRED,
    URGENT
};

final graphql:Client shopify = check new ("http://blackwellsbooks.myshopify.com.balmock.io");

public function main(string category) returns error? {
    string csvFilePath = "./resources/orderRequests.csv";
    string document = string `{ products(productType: "${category}") { name, productsCount } } `;
    InventoryResponse inventories = check shopify->execute(document);
    CsvRecord[] csvContent = [];
    foreach var {name, productsCount} in inventories.data.products {
        if productsCount < 10 {
            csvContent.push({name: name, requestType: URGENT});
        } else if productsCount < 25 {
            csvContent.push({name: name, requestType: REQUIRED});
        }
    }
    check io:fileWriteCsv(csvFilePath, csvContent);
}