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.

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)

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.