# Tree fields

Source: https://swapcard.dev/content-api/examples/tree-fields


Create a tree field and manage its nodes. Tree fields are custom fields whose values form a hierarchy, for taxonomies such as categories, industries or topics. You can assign their values to exhibitors, people, products (`items` in the API) and sessions (`plannings` in the API).

Reference: [`TreeFieldDefinition`](../graphql-event-api-schema/objects/tree-field-definition.mdx) and [`TreeFieldNode`](../graphql-event-api-schema/objects/tree-field-node.mdx) objects

On this page you can:

- [Create a tree field](#1-create-a-fielddefinition-of-type-tree) with its initial structure.
- [Replace the whole tree](#2-update-a-fielddefinition-of-type-tree), before any value is assigned.
- [Add](#3-create-a-tree-node--createtreefieldnode), [update](#4-update-a-tree-node--updatetreefieldnode), [move](#5-move-a-tree-node--movetreefieldnode) and [delete](#6-delete-a-tree-node--deletetreefieldnode) nodes once the tree is in use.

## How tree fields work

A tree field is made of **nodes**, each identified by a path:

- A **selectable** node can be assigned to an entity.
- A **non-selectable** node is only a grouping label in the hierarchy.
- Each node has its own translated labels.

A path is a string that joins node identifiers with the `|~^` separator. For example, the sub-category `SUB-1`, under the category `CAT-1`, under `ROOT`, has the path `ROOT|~^CAT-1|~^SUB-1`.

## Before you begin

- An API key, sent in the `Authorization` header. The key acts with the roles and permissions of the user who created it. See [Authentication](/organizer/authentication).
- The ID of your event (`eventId`). See [Events](./graphql-event-api-examples-event.md).
- To manage nodes, the ID of the tree field (`fieldDefinitionId`). See [List custom fields](./graphql-event-api-examples-custom-fields.md#get-custom-fields-of-an-event).

## Create a tree field
Use the `createFieldDefinition` mutation with `type` set to `TREE` to create a tree field and its initial structure. Describe the structure with two inputs:

- `treeOptions`: the list of **selectable** nodes. Each option has a `path`: an array of node identifiers, from the root to the target node.
- `treeTranslations`: the translated labels of **every** node in the tree, selectable or not. Each entry has a `value` (the full node path joined with `|~^`) and a list of `translations`, one per language.

:::warning[Selectable vs. labeled nodes]
Not every node needs to be selectable. Intermediate (grouping) nodes can exist in `treeTranslations` without appearing in `treeOptions`. However, every node referenced in a path, selectable or not, must have an entry in `treeTranslations` so it can be displayed with a proper label.
:::

| Argument | Description |
| --- | --- |
| `eventId` | The ID of the event on which to create the custom field. |
| `name` | The name of the custom field. |
| `target` | The entity the custom field applies to (`PEOPLE`, `EXHIBITOR`, `SESSION` or `ITEM`). |
| `type` | Must be `TREE`. |
| `treeOptions` | The paths of the selectable nodes. |
| `treeTranslations` | The labels of every node of the tree, in every language. |

```graphql title="Mutation"
mutation createFieldDefinition($input: CreateFieldDefinitionV2Input!) {
  createFieldDefinition(input: $input) {
    event {
      id
    }
    fieldDefinition {
      ... on TreeFieldDefinition {
        id
        name
        tree {
          value
          path {
            value
            translations {
              language
              value
            }
          }
        }
      }
    }
    errors {
      code
      message
    }
  }
}
```

```json title="Variables"
{
  "input": {
    "eventId": "RXZlbnRfMQ==",
    "name": "Industry",
    "target": "EXHIBITOR",
    "type": "TREE",
    "isEditable": true,
    "isVisible": true,
    "translations": [
      { "language": "en_US", "name": "Industry" },
      { "language": "fr_FR", "name": "Secteur d'activité" }
    ],
    "treeOptions": [
      { "path": ["ROOT", "CAT-1"] },
      { "path": ["ROOT", "CAT-1", "SUB-1-1"] },
      { "path": ["ROOT", "CAT-1", "SUB-1-2"] },
      { "path": ["ROOT", "CAT-2"] },
      { "path": ["ROOT", "CAT-2", "SUB-2-1"] }
    ],
    "treeTranslations": [
      {
        "value": "ROOT",
        "translations": [
          { "language": "en_US", "value": "Root" },
          { "language": "fr_FR", "value": "Racine" }
        ]
      },
      {
        "value": "ROOT|~^CAT-1",
        "translations": [
          { "language": "en_US", "value": "Category 1" },
          { "language": "fr_FR", "value": "Catégorie 1" }
        ]
      },
      {
        "value": "ROOT|~^CAT-1|~^SUB-1-1",
        "translations": [
          { "language": "en_US", "value": "Sub Category 1-1" },
          { "language": "fr_FR", "value": "Sous-catégorie 1-1" }
        ]
      },
      {
        "value": "ROOT|~^CAT-1|~^SUB-1-2",
        "translations": [
          { "language": "en_US", "value": "Sub Category 1-2" },
          { "language": "fr_FR", "value": "Sous-catégorie 1-2" }
        ]
      },
      {
        "value": "ROOT|~^CAT-2",
        "translations": [
          { "language": "en_US", "value": "Category 2" },
          { "language": "fr_FR", "value": "Catégorie 2" }
        ]
      },
      {
        "value": "ROOT|~^CAT-2|~^SUB-2-1",
        "translations": [
          { "language": "en_US", "value": "Sub Category 2-1" },
          { "language": "fr_FR", "value": "Sous-catégorie 2-1" }
        ]
      }
    ]
  }
}
```

## Replace the whole tree
The `updateFieldDefinition` mutation also works on tree fields: it replaces the whole tree structure (`treeOptions` and `treeTranslations`) in a single call. Use it only while no value is assigned yet.

:::danger[Don't use updateFieldDefinition once values are assigned]
**Avoid using `updateFieldDefinition` to change the tree structure of a field whose values are already assigned to entities** (exhibitors, people, products or sessions). This mutation overwrites the whole tree and doesn't check existing assignments, which can leave orphaned or inconsistent values.

Once the tree is in use, use the node-level mutations instead:

- [`createTreeFieldNode`](#3-create-a-tree-node--createtreefieldnode)
- [`updateTreeFieldNode`](#4-update-a-tree-node--updatetreefieldnode)
- [`moveTreeFieldNode`](#5-move-a-tree-node--movetreefieldnode)
- [`deleteTreeFieldNode`](#6-delete-a-tree-node--deletetreefieldnode)

These mutations validate each change and keep data consistent when you add, rename, move or remove nodes.
:::

## Add a node
Use the `createTreeFieldNode` mutation to add a node to an existing tree, at a position relative to an existing reference node.

| Argument | Description |
| --- | --- |
| `fieldDefinitionId` | The ID of the tree field to add the node to. |
| `targetNode` | The full path of the reference node used for positioning, for example `ROOT\|~^CAT-1`. |
| `position` | Where to place the new node relative to `targetNode`: `ABOVE`, `BELOW` or `INSIDE`. |
| `nodeId` | The identifier of the new node, for example `SUB-NEW`. Combined with its parent path, it forms the full path of the new node. |
| `translations` | The translated labels of the new node. |
| `isSelectable` | Whether the new node can be assigned to entities (it is added to `treeOptions`) or is only a grouping label. |

```graphql title="Mutation"
mutation CreateTreeFieldNode($input: CreateTreeFieldNodeInput!) {
  createTreeFieldNode(input: $input) {
    fieldDefinition {
      ... on TreeFieldDefinition {
        id
        tree {
          value
          path {
            value
            translations {
              language
              value
            }
          }
        }
      }
    }
    errors {
      code
      message
      path
    }
  }
}
```

```json title="Variables"
{
  "input": {
    "fieldDefinitionId": "RmllbGREZWZpbml0aW9uXzY=",
    "targetNode": "ROOT|~^CAT-1",
    "position": "INSIDE",
    "nodeId": "SUB-1-3",
    "isSelectable": true,
    "translations": [
      { "language": "en_US", "value": "Sub Category 1-3" },
      { "language": "fr_FR", "value": "Sous-catégorie 1-3" }
    ]
  }
}
```

## Update a node
Use the `updateTreeFieldNode` mutation to change the translations of a node, whether it is selectable, or both. To move a node, use [`moveTreeFieldNode`](#5-move-a-tree-node--movetreefieldnode) instead.

| Argument | Description |
| --- | --- |
| `fieldDefinitionId` | The ID of the tree field. |
| `currentNodePath` | The full path of the node to update, for example `ROOT\|~^CAT-1\|~^SUB-1-1`. |
| `translations` | Optional. Replaces **all** the existing translations of the node. |
| `isSelectable` | Optional. Sets whether the node can be assigned to entities. |

```graphql title="Mutation"
mutation UpdateTreeFieldNode($input: UpdateTreeFieldNodeInput!) {
  updateTreeFieldNode(input: $input) {
    fieldDefinition {
      ... on TreeFieldDefinition {
        id
        tree {
          value
          path {
            value
            translations {
              language
              value
            }
          }
        }
      }
    }
    errors {
      code
      message
      path
    }
  }
}
```

```json title="Variables"
{
  "input": {
    "fieldDefinitionId": "RmllbGREZWZpbml0aW9uXzY=",
    "currentNodePath": "ROOT|~^CAT-1",
    "isSelectable": true,
    "translations": [
      { "language": "en_US", "value": "Category 1 — Updated" },
      { "language": "fr_FR", "value": "Catégorie 1 — Mise à jour" }
    ]
  }
}
```

## Move a node
Use the `moveTreeFieldNode` mutation to move a node, and all its descendants, to another position in the tree. Values already assigned to the node or its descendants follow the new path.

| Argument | Description |
| --- | --- |
| `fieldDefinitionId` | The ID of the tree field. |
| `currentNodePath` | The current full path of the node to move. |
| `targetNode` | The full path of the reference node for the destination. |
| `position` | Where to place the node relative to `targetNode`: `ABOVE`, `BELOW` or `INSIDE`. |

```graphql title="Mutation"
mutation MoveTreeFieldNode($input: MoveTreeFieldNodeInput!) {
  moveTreeFieldNode(input: $input) {
    fieldDefinition {
      ... on TreeFieldDefinition {
        id
        tree {
          value
          path {
            value
            translations {
              language
              value
            }
          }
        }
      }
    }
    errors {
      code
      message
      path
    }
  }
}
```

```json title="Variables"
{
  "input": {
    "fieldDefinitionId": "RmllbGREZWZpbml0aW9uXzY=",
    "currentNodePath": "ROOT|~^CAT-2|~^SUB-2-1",
    "targetNode": "ROOT|~^CAT-1|~^SUB-1-1",
    "position": "ABOVE"
  }
}
```

## Delete a node
Use the `deleteTreeFieldNode` mutation to delete a node and **all its descendants**. The response returns the IDs of the deleted `FieldValue` entities in `deletedFieldValueIds`, so you can update your own data.

:::danger[Cascading delete]
Deleting a branch node also deletes all its descendants. Any value assigned to the deleted nodes is removed from the related exhibitors, people, products or sessions.
:::

| Argument | Description |
| --- | --- |
| `fieldDefinitionId` | The ID of the tree field. |
| `currentNodePath` | The full path of the node to delete. |

```graphql title="Mutation"
mutation DeleteTreeFieldNode($input: DeleteTreeFieldNodeInput!) {
  deleteTreeFieldNode(input: $input) {
    deletedFieldValueIds
    fieldDefinition {
      ... on TreeFieldDefinition {
        id
        tree {
          value
          path {
            value
            translations {
              language
              value
            }
          }
        }
      }
    }
    errors {
      code
      message
      path
    }
  }
}
```

```json title="Variables"
{
  "input": {
    "fieldDefinitionId": "RmllbGREZWZpbml0aW9uXzY=",
    "currentNodePath": "ROOT|~^CAT-1"
  }
}
```
