# gRPC Client

> 调用 gRPC 服务方法。

Source: https://docs.sommatic.ai/zh/nodes/io.grpc/

- 标识: `io.grpc`
- 类别: 输入与输出
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

调用 gRPC 服务方法。

## 配置

- `connection_id` (string, 必填): gRPC 端点的凭据。接受 `certificate`（用于 mTLS 的 TLS 证书 + 密钥）或 `object`（主机 + .proto 路径 + 高级配置）类型的凭据。
- `service` (string, 必填): 服务名称
- `method` (string, 必填): 方法名称
- `payload` (object, 可选): 请求消息

## 输出端口

- `out` (默认): gRPC 响应消息
- `error`: gRPC 错误码

## 输入契约

- `connection_id` (string, 必填)
- `service` (string, 必填)
- `method` (string, 必填)
- `payload` (object, 可选)

## 输出契约

- `response` (object, 可选): gRPC 响应消息

## 在表达式中

后续节点可通过 `$("gRPC Client").item.json.<路径>` 读取此节点输出中的内容（请把 `gRPC Client` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("gRPC Client").item.json.response` (object) = `{…}`: gRPC 响应消息
- `$("gRPC Client").item.json.response.order_id` (string) = `"ord_123"` (编辑器不提示)
- `$("gRPC Client").item.json.response.status` (string) = `"CONFIRMED"` (编辑器不提示)
- `$("gRPC Client").item.json.response.total_cents` (string) = `"25000"` (编辑器不提示)


## 工作原理

调用 gRPC 服务的一个一元方法，服务由编排器可访问的 `.proto` 文件描述，主机和 proto 设置取自凭据。

1. 解析配置。`connection_id`、`service` 和 `method` 缺失时回退到输入 payload 中的同名键。
2. 构建请求消息：配置的 `payload` 为非空对象时使用它；否则使用输入数据中的 `payload.payload`，或整个输入数据。
3. 解析凭据配置：`host`（`host:port`）、`protoPath`（编排器文件系统上的文件）、可选的 `packageName` 和 `secure`。
4. 加载 proto（保留字段原始名称，long 和 enum 转为字符串，并填充默认值），依次查找 `packageName` 与 `service`；`secure` 为 true 时建立 TLS 通道，否则建立明文通道。
5. 以该消息调用 `method`；若密钥含 `token`，则附带元数据 `Authorization: Bearer <token>`。响应以 `{ response }` 从 `out` 输出。

## 在工作流中

### 节点

```json
{
  "node_id": "node-confirm-order",
  "label": "Confirm order",
  "operator_slug": "io.grpc",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "service": "OrderService",
    "method": "ConfirmOrder",
    "payload": {
      "order_id": "{{ $json.order_id }}"
    }
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "response": {
      "order_id": "ord_123",
      "status": "CONFIRMED",
      "total_cents": "25000"
    }
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

方法返回了响应。`response` 为解码后的响应消息。

```json
{
  "response": {
    "order_id": "ord_123",
    "status": "CONFIRMED",
    "total_cents": "25000"
  }
}
```

### `error`

调用返回 gRPC 状态错误（数据为 `{ error, code }`，含数字 gRPC 状态码），或准备阶段失败（数据为 `{ error }`）。

```json
{
  "error": "14 UNAVAILABLE: No connection established",
  "code": 14
}
```

## 代码中的行为

- `connection_id`: 凭据配置须包含 `host`、`protoPath`、可选的 `packageName` 和 `secure`；密钥中只读取 `token`。选择器接受 `certificate` 凭据，但不会使用客户端证书：TLS 使用系统信任库且不带客户端证书（不支持 mTLS）。
- `payload`: 请求消息。空对象视为未设置，此时改为发送输入数据。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `protoPath missing in connection config` | `error` | 凭据配置中缺少 `protoPath`。请添加编排器主机上存在的 `.proto` 文件路径。 |
| `Package <packageName> not found in proto` | `error` | `packageName` 与 proto 的顶层包不匹配。只查找单段名称（例如 `helloworld`，而非 `acme.orders`）。 |
| `Service <service> not found` | `error` | 所选包中未定义 `service`。 |
| `Method <method> not found` | `error` | `method` 不是该服务的方法。请使用 proto 中的原始名称。 |
| `connection_id is required` | `error` | 未选择凭据。 |

## 说明与限制

- 仅支持一元调用，不处理流式方法。调用未设置截止时间，也不会重试。
- `.proto` 文件在每次运行时从编排器文件系统读取，不会随工作流上传。
- `response` 中的 64 位整数和枚举以字符串形式返回。


## 契约（JSON Schema）

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

```json
{
  "slug": "io.grpc",
  "name": "gRPC Client",
  "category": "io",
  "version": "1.0.0",
  "description": "Call gRPC Service method.",
  "capabilities": [
    "grpc",
    "microservices",
    "protobuf"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "gRPC Credential",
        "description": "Credential for the gRPC endpoint. Accepts credentials of type `certificate` (TLS cert + key for mTLS) or `object` (host + .proto path + advanced config).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "certificate",
            "object"
          ],
          "types": [
            "grpc"
          ]
        }
      },
      "service": {
        "type": "string",
        "description": "Service Name"
      },
      "method": {
        "type": "string",
        "description": "Method Name"
      },
      "payload": {
        "type": "object",
        "description": "Request Message"
      }
    },
    "required": [
      "connection_id",
      "service",
      "method"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "service": {
        "type": "string"
      },
      "method": {
        "type": "string"
      },
      "payload": {
        "type": "object"
      }
    },
    "required": [
      "connection_id",
      "service",
      "method"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "response": {
        "type": "object",
        "description": "gRPC Response Message"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Response",
      "description": "gRPC Response Message",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "gRPC Error Code",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "microservices"
  ],
  "tags": [
    "grpc",
    "rpc"
  ],
  "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": "gRPC Credential",
      "description": "Credential for the gRPC endpoint. Accepts credentials of type `certificate` (TLS cert + key for mTLS) or `object` (host + .proto path + advanced config).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "certificate",
          "object"
        ],
        "types": [
          "grpc"
        ]
      }
    },
    "service": {
      "type": "string",
      "description": "Service Name"
    },
    "method": {
      "type": "string",
      "description": "Method Name"
    },
    "payload": {
      "type": "object",
      "description": "Request Message"
    }
  },
  "required": [
    "connection_id",
    "service",
    "method"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "service": {
      "type": "string"
    },
    "method": {
      "type": "string"
    },
    "payload": {
      "type": "object"
    }
  },
  "required": [
    "connection_id",
    "service",
    "method"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "response": {
      "type": "object",
      "description": "gRPC Response Message"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Response",
    "description": "gRPC Response Message",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "gRPC Error Code",
    "is_default": false,
    "supports_condition": false
  }
]
```
