GraphQL - Loop through results for sending multiple emails?

How can I loop through an array of results from a GraphQL query? My use case is I need to send multiple emails (execute the Email node one or more times). Here’s the format of the GraphQL query response:

Welcome to the community @ChaseP!

That could be done like this:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json.data = {\n  user_accounts: [\n    {\n      email: '[email protected]',\n      first_name: 'jim',\n    },\n    {\n      email: '[email protected]',\n      first_name: 'dean',\n    }\n  ]\n}\nreturn items;"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        400,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.data.user_accounts.map(account => {\n  return { json: account };\n})"
      },
      "name": "Split Hosts",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "toEmail": "={{$node[\"Split Hosts\"].json[\"email\"]}}",
        "options": {}
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        800,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Split Hosts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Hosts": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

@jan

Super, thank you!

I’m using n8n to process Hasura Events.

Y’all should write up an article on Hasura + n8n, as these seem like a pretty powerful scalable yet low-code combination for building a backend.

1 Like

Sounds interesting @ChaseP! I’ll reach out to my friends at Hasura and see how we can collaborate on something cool. Thanks for the suggestion :slight_smile:

1 Like

@jan, I think I nearly have this working. But as it iterates through items returned by “Map Assignees” Function node, only values of the first item from the Function node are making it to the email. All items which make it past the IF node do trigger an email to be sent for each item. But only the first email contains values (the others have blank/nil values). Hope that makes sense.

Here’s a gist of the config: https://gist.github.com/cpursley/34c2b678e3726666defb114112de336b

Any suggestions? Thank you.

Could you please remove the first 3 Nodes (so up to Get-Values) and replace them with a single Function-Node which returns some Mock-Data (exactly the same data you have there, you can obviously anonymize it). That would so be much easier to see the problem. Thanks!

Hi @jan, sure - here’s an example with mock data. I left in the old nodes in there but just disabled them (so that you can reference): https://gist.github.com/cpursley/a617a194ddc1f35ecb1464f79eabdda2

I appreciate the help - I’m really excited about the possibilities of n8n!

Ah now see the problem.

n8n replaces all the values of an expression with the data of the current item-index. The problem is that the “Get Values” Node has only 1 item and the “Send Email” Node has multiple ones (in the example 3). For the first expression it finds a value (as both nodes have an item with index 0) but as it then gets to the other items it can not find any.
So what you have to do is to tell n8n that you always want to use the data of the first item. So instead of writing the expression like this:

{{$node["Get Values"].json["data"]["updatedBy"][0]["full_name"]}}

You have to do it like this:

{{$items("Get Values")[0].json["data"]["updatedBy"][0]["full_name"]}}

Like that it gets all the items and then specifically selects the first one.

Ah, super - that works perfectly!

Is there way to set up variables? For example, a way to reuse $node["Get Values"].json["data"] without needing to write it all out each time in the expressions / templates?

No that is sadly not possible for expressions. You could create the email text in a Function-Node. There it would be very simple to do that.