Skip to main content

Destructure collection elements in FOR loops  

Use destructuring syntax to extract specific properties from collection elements directly in the loop variable declaration. This eliminates the need for property access syntax within the loop body.
FOR {field1, field2, field3} IN collection {
    // Use field1, field2, field3 directly
}

FOR {object.field1, object.field2} IN collection {
    // Access nested properties
}
Destructuring makes your queries more readable and explicit about which properties are being used in the loop body.
When using the SDKs or curling the endpoint, the query name must match what is defined in the queries.hx file exactly.

Example 1: Basic destructuring

QUERY CreateUsersFromData (user_data: [{name: String, age: U8, email: String}]) =>
    FOR {name, age, email} IN user_data {
        user <- AddN<User>({
            name: name,
            age: age,
            email: email
        })
    }
    RETURN "Users created successfully"
Here’s how to run the query using the SDKs or curl
from helix.client import Client

client = Client(local=True, port=6969)

# Create users using destructuring
user_data = [
    {"name": "Alice", "age": 25, "email": "alice@example.com"},
    {"name": "Bob", "age": 30, "email": "bob@example.com"},
    {"name": "Charlie", "age": 28, "email": "charlie@example.com"},
    {"name": "Diana", "age": 22, "email": "diana@example.com"},
]

result = client.query("CreateUsersFromData", {"user_data": user_data})
print("Create result:", result)

Example 2: Nested property access with destructuring

QUERY LoadOrdersWithItems (
    order_data: [{
        customer: {name: String, email: String},
        items: [{product: String, quantity: I32, price: F64}]
    }]
) =>
    FOR {customer, items} IN order_data {
        order_node <- AddN<Order>({
            customer_name: customer.name,
            customer_email: customer.email,
            total_items: 0
        })
        FOR {product, quantity, price} IN items {
            item_node <- AddN<OrderItem>({
                product_name: product,
                quantity: quantity,
                unit_price: price
            })
            AddE<Contains>::From(order_node)::To(item_node)
        }
    }
    RETURN "Orders loaded successfully"
Here’s how to run the query using the SDKs or curl
from helix.client import Client

client = Client(local=True, port=6969)

# Load orders with nested destructuring
order_data = [
    {
        "customer": {"name": "Alice", "email": "alice@example.com"},
        "items": [
            {"product": "Laptop", "quantity": 1, "price": 999.99},
            {"product": "Mouse", "quantity": 2, "price": 29.99},
        ]
    },
    {
        "customer": {"name": "Bob", "email": "bob@example.com"},
        "items": [
            {"product": "Keyboard", "quantity": 1, "price": 79.99},
            {"product": "Monitor", "quantity": 2, "price": 299.99},
        ]
    },
]

result = client.query("LoadOrdersWithItems", {"order_data": order_data})
print("Load result:", result)

Example 3: Selective field extraction

QUERY CreateProductsFromCatalog (
    catalog: [{
        id: String,
        name: String,
        price: F64,
        internal_code: String,
        warehouse_location: String
    }]
) =>
    FOR {name, price} IN catalog {
        product <- AddN<Product>({
            name: name,
            price: price,
            stock: 100
        })
    }
    RETURN "Products created from catalog"
Here’s how to run the query using the SDKs or curl
from helix.client import Client

client = Client(local=True, port=6969)

# Create products extracting only needed fields
catalog = [
    {
        "id": "P001",
        "name": "Laptop",
        "price": 999.99,
        "internal_code": "WH-A-001",
        "warehouse_location": "Building A, Aisle 3"
    },
    {
        "id": "P002",
        "name": "Mouse",
        "price": 29.99,
        "internal_code": "WH-B-042",
        "warehouse_location": "Building B, Aisle 1"
    },
    {
        "id": "P003",
        "name": "Keyboard",
        "price": 79.99,
        "internal_code": "WH-A-015",
        "warehouse_location": "Building A, Aisle 5"
    },
]

result = client.query("CreateProductsFromCatalog", {"catalog": catalog})
print("Create result:", result)