Template Syntax & Node Communication ๐
Build powerful workflows by connecting your nodes with our intuitive template syntax. Learn how to share and transform data between nodes using handlebars expressions.
๐ TLDR: Use handlebars syntax {{expression}}
to access node results, context data, and transform values using JSONata. That's all you need to know to get started!
Why Care About Node Communication? ๐ค
Node communication is the backbone of powerful workflows:
- ๐ Chain API calls (pass auth tokens from login to subsequent requests)
- ๐ฏ Build dynamic URLs (use user IDs from one API to fetch their orders)
- ๐ ๏ธ Combine data (merge user profile with their latest orders)
- ๐ Share secrets (use API keys from context across multiple requests)
- ๐ Transform responses (reshape API responses to match your needs)
๐ก Think of nodes as team members - the better they communicate, the smoother your workflow runs!
Using Template Syntax โจ
Every node can access results from previous nodes using handlebars syntax:
import { GraphCompose } from '@graphcompose/sdk';
const workflow = new GraphCompose()
.addHttpNode('send_welcome_email')
.withBody({
to: '{{ results.create_user.email }}', // Access email from previous node
name: '{{ results.create_user.firstName }}' // Access name from previous node
});
Sharing Context ๐
Need to share data across your whole workflow? Access the global context:
import { GraphCompose } from '@graphcompose/sdk';
const workflow = new GraphCompose()
.setContext({
apiKey: process.env.API_KEY, // Available to all nodes
baseUrl: "https://api.example.com" // Shared configuration
})
.addHttpNode('make_request')
.withHeaders({
'x-api-key': '{{ context.apiKey }}' // Access from context
})
.get('{{ context.baseUrl }}/users'); // Build URL dynamically
Transforming Data ๐
Need to reshape data between nodes? Use JSONata expressions inside handlebars:
import { GraphCompose } from '@graphcompose/sdk';
const workflow = new GraphCompose()
.addHttpNode('prepare_order')
.withBody({
userId: '{{ results.get_user.id }}', // Access nested result data
items: '{{ results.get_cart.items.{ "productId": id, "quantity": quantity, "price": price * quantity } }}', // Transform array items
total: '{{ $sum(results.get_cart.items.(price * quantity)) }}' // Calculate total using JSONata
});
Common Patterns ๐ซ
Here are some cool ways nodes can work together:
Building Request Data ๐๏ธ
import { GraphCompose } from '@graphcompose/sdk';
const workflow = new GraphCompose()
.addHttpNode('create_order')
.withBody({
user: '{{ results.get_user }}', // Include entire user object
items: '{{ results.get_cart.items }}', // Array of cart items
shipping: '{{ results.get_address }}' // Full shipping details
});
Passing Authentication ๐
import { GraphCompose } from '@graphcompose/sdk';
const workflow = new GraphCompose()
.addHttpNode('authenticated_request')
.withHeaders({
Authorization: '{{ $concat("Bearer ", results.get_token.access_token) }}' // Combine token with prefix
});
Best Practices ๐ก
Keep your node communication clean and efficient:
- ๐ฏ Make dependencies clear and explicit
- ๐ Use descriptive node IDs
- ๐ Keep transformations simple and focused
- โจ Validate data between critical steps
- ๐จ Structure workflows for easy data flow
Want to learn more about JSONata?
JSONata is a powerful query language for JSON data. You can visit JSONata.org for more information and a playground to test your expressions.