# 图数据库API接口模块

本模块提供了与Neo4j图数据库交互的HTTP API接口，用于前端或其他服务调用。

## 功能概述

图数据库API模块为前端应用提供了一组REST接口，用于执行各种图数据库操作，如执行查询、创建节点和关系、获取子图数据等。这些接口统一采用JSON格式进行数据交换，并提供标准化的错误处理和响应格式。

## API接口

### 1. 执行Cypher查询 (/graph/query)

- **URL**: `/graph/query`
- **方法**: POST
- **描述**: 执行自定义Cypher查询并返回结果
- **请求参数**:
  ```json
  {
    "cypher": "MATCH (n:Person) WHERE n.name = $name RETURN n",
    "params": {
      "name": "张三"
    }
  }
  ```
- **返回数据**:
  ```json
  {
    "code": 200,
    "success": true,
    "message": "success",
    "data": [
      {
        "n": {
          "_id": 123,
          "_labels": ["Person"],
          "name": "张三",
          "age": 30
        }
      }
    ]
  }
  ```

### 2. 创建节点 (/graph/node/create)

- **URL**: `/graph/node/create`
- **方法**: POST
- **描述**: 创建一个新节点
- **请求参数**:
  ```json
  {
    "labels": ["Person", "Employee"],
    "properties": {
      "name": "张三",
      "age": 30,
      "department": "技术部"
    }
  }
  ```
- **返回数据**:
  ```json
  {
    "code": 200,
    "success": true,
    "message": "success",
    "data": {
      "n": {
        "_id": 123,
        "_labels": ["Person", "Employee"],
        "name": "张三",
        "age": 30,
        "department": "技术部"
      }
    }
  }
  ```

### 3. 创建关系 (/graph/relationship/create)

- **URL**: `/graph/relationship/create`
- **方法**: POST
- **描述**: 在两个节点之间创建关系
- **请求参数**:
  ```json
  {
    "startNodeId": 123,
    "endNodeId": 456,
    "type": "KNOWS",
    "properties": {
      "since": 2020,
      "relationship": "同事"
    }
  }
  ```
- **返回数据**:
  ```json
  {
    "code": 200,
    "success": true,
    "message": "success",
    "data": {
      "r": {
        "_id": 789,
        "_type": "KNOWS",
        "_start_node_id": 123,
        "_end_node_id": 456,
        "since": 2020,
        "relationship": "同事"
      }
    }
  }
  ```

### 4. 获取子图 (/graph/subgraph)

- **URL**: `/graph/subgraph`
- **方法**: POST
- **描述**: 获取以指定节点为起点的子图
- **请求参数**:
  ```json
  {
    "nodeIds": [123],
    "relationshipTypes": ["KNOWS", "WORKS_WITH"],
    "maxDepth": 2
  }
  ```
- **返回数据**:
  ```json
  {
    "code": 200,
    "success": true,
    "message": "success",
    "data": {
      "nodes": [
        {
          "id": 123,
          "labels": ["Person"],
          "name": "张三",
          "age": 30
        },
        {
          "id": 456,
          "labels": ["Person"],
          "name": "李四",
          "age": 28
        }
      ],
      "relationships": [
        {
          "id": 789,
          "type": "KNOWS",
          "source": 123,
          "target": 456,
          "since": 2020
        }
      ]
    }
  }
  ```

## 技术实现

本模块基于Flask框架实现API接口，并使用core/graph模块提供的核心功能。主要技术点包括：

- RESTful API设计
- 请求参数验证与处理
- 异常处理与错误响应
- JSON序列化

## 依赖关系

本模块依赖于core/graph模块中的核心功能实现：

```python
from app.core.graph import (
    connect_graph,
    create_or_get_node,
    create_relationship,
    get_subgraph,
    execute_cypher_query
)
``` 