# GraphQL Client

> Execute GraphQL Query or Mutation.

Source: https://docs.sommatic.ai/en/nodes/io.graphql/

- Slug: `io.graphql`
- Family: Input and output
- Version: 1.0.0
- Status: Available
- Execution: async

Execute GraphQL Query or Mutation.

## Configuration

- `connection_id` (string, required): Credential for the GraphQL endpoint. Accepts credentials of type `api-key`, `basic-auth`, or `oauth2` (bearer token).
- `query` (string, required): GraphQL Query/Mutation
- `variables` (object, optional)

## Output ports

- `out` (default): GraphQL data response
- `error`: GraphQL errors or network error

## Input contract

- `connection_id` (string, required)
- `query` (string, required)
- `variables` (object, optional)

## Output contract

- `data` (object, optional): GraphQL Data
- `errors` (array, optional): GraphQL Errors

## In expressions

What a later node can read from this node's output with `$("GraphQL Client").item.json.<path>` (write your node's label instead of `GraphQL Client`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("GraphQL Client").item.json.data` (object) = `{…}`: GraphQL Data
- `$("GraphQL Client").item.json.errors` (array): GraphQL Errors **The worker does not return it: reads as undefined.**
- `$("GraphQL Client").item.json.data.order` (object) = `{…}` (Not suggested by the editor)


## How it works

Sends one GraphQL query or mutation as an HTTP POST to the endpoint of a credential and returns `data`, or the GraphQL `errors` through the error port.

1. Resolves the config. `connection_id`, `query` and `variables` fall back to the same keys of the incoming payload; `connection_id` and `query` are required.
2. Resolves the credential. The endpoint is `config.url` (or `config.endpoint`); extra headers come from `config.headers`.
3. Adds authentication from the secret, first match wins: `token` → `Authorization: Bearer`, `apiKey` → header `config.headerName` (default `x-api-key`), `username` + `password` → `Authorization: Basic`.
4. POSTs `{ query, variables }` as JSON.
5. If the response has a non-empty `errors` array, leaves through `error` with `{ errors }`; otherwise returns `{ data }` through `out`.

## In a workflow

### Node

```json
{
  "node_id": "node-fetch-order",
  "label": "Fetch order",
  "operator_slug": "io.graphql",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "query": "query Order($id: ID!) { order(id: $id) { id status } }",
    "variables": {
      "id": "{{ $json.order_id }}"
    }
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "data": {
      "order": {
        "id": "ord_123",
        "status": "PAID"
      }
    }
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The server answered 2xx without GraphQL errors. `data` is the `data` field of the GraphQL response.

```json
{
  "data": {
    "order": {
      "id": "ord_123",
      "status": "PAID"
    }
  }
}
```

### `error`

The response carries GraphQL `errors` (data is `{ errors }`), or the request failed (data is `{ error, response }`, with `response` the body of a non-2xx answer).

```json
{
  "errors": [
    {
      "message": "Cannot query field \"stat\" on type \"Order\".",
      "locations": [
        {
          "line": 1,
          "column": 32
        }
      ]
    }
  ]
}
```

## Behaviour in the code

- `connection_id`: Accepts `basic-auth`, `oauth2` and `object` credentials whose secret is an object with `token`, `apiKey` or `username` + `password`. An `api-key` credential stored as a plain string adds no header. A value starting with `http` is used as the endpoint URL itself, without authentication.
- `variables`: Sent as-is; expressions inside are resolved first.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `connection_id is required` | `error` | No credential selected. Pick a GraphQL credential. |
| `query is required` | `error` | The query is empty in the config and in the incoming payload. |
| `Request failed with status code <status>` | `error` | The server answered non-2xx. `response` holds its body. Check the endpoint and the authentication. |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | The credential does not exist or this workflow cannot use it. |

## Notes and limits

- A response with both `data` and `errors` (partial success) goes to `error` and the partial `data` is dropped.
- No request timeout and no retries are applied.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "io.graphql",
  "name": "GraphQL Client",
  "category": "io",
  "version": "1.0.0",
  "description": "Execute GraphQL Query or Mutation.",
  "capabilities": [
    "graphql",
    "api",
    "http"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "GraphQL Credential",
        "description": "Credential for the GraphQL endpoint. Accepts credentials of type `api-key`, `basic-auth`, or `oauth2` (bearer token).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "api-key",
            "basic-auth",
            "oauth2"
          ],
          "types": [
            "graphql",
            "http"
          ]
        }
      },
      "query": {
        "type": "string",
        "description": "GraphQL Query/Mutation"
      },
      "variables": {
        "type": "object"
      }
    },
    "required": [
      "connection_id",
      "query"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "query": {
        "type": "string"
      },
      "variables": {
        "type": "object"
      }
    },
    "required": [
      "connection_id",
      "query"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "data": {
        "type": "object",
        "description": "GraphQL Data"
      },
      "errors": {
        "type": "array",
        "description": "GraphQL Errors"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Data",
      "description": "GraphQL data response",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Errors",
      "description": "GraphQL errors or network error",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io"
  ],
  "tags": [
    "graphql",
    "api"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "GraphQL Credential",
      "description": "Credential for the GraphQL endpoint. Accepts credentials of type `api-key`, `basic-auth`, or `oauth2` (bearer token).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "api-key",
          "basic-auth",
          "oauth2"
        ],
        "types": [
          "graphql",
          "http"
        ]
      }
    },
    "query": {
      "type": "string",
      "description": "GraphQL Query/Mutation"
    },
    "variables": {
      "type": "object"
    }
  },
  "required": [
    "connection_id",
    "query"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "query": {
      "type": "string"
    },
    "variables": {
      "type": "object"
    }
  },
  "required": [
    "connection_id",
    "query"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "description": "GraphQL Data"
    },
    "errors": {
      "type": "array",
      "description": "GraphQL Errors"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Data",
    "description": "GraphQL data response",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Errors",
    "description": "GraphQL errors or network error",
    "is_default": false,
    "supports_condition": false
  }
]
```
