File size: 6,352 Bytes
eca5751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
GraphQL Client Tool - Gửi GraphQL query/mutation tới một endpoint.
Author: Hieu Louis (2026)
Dùng stdlib urllib (fallback) hoặc requests nếu có. Hỗ trợ variables + headers.
"""
from __future__ import annotations

import json
from typing import Any, Dict, Optional

from .base import Tool, ToolResult, ToolContext, ToolCategory, ToolSafety


class GraphQLClientTool(Tool):
    """Gửi GraphQL query/mutation tới một endpoint HTTP."""

    category = ToolCategory.WEB  # theo spec: category=WEB
    safety = ToolSafety.MODERATE  # network call nhưng query GraphQL
    requires_confirmation = False

    @property
    def name(self) -> str:
        return "graphql_client"

    @property
    def description(self) -> str:
        return (
            "Gửi GraphQL query hoặc mutation tới một endpoint. Hỗ trợ variables, "
            "headers (auth), timeout. Trả về JSON response."
        )

    @property
    def parameters(self) -> Dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "endpoint": {"type": "string", "description": "GraphQL endpoint URL (https://...)"},
                "query": {"type": "string", "description": "GraphQL query/mutation string"},
                "variables": {"type": "object", "description": "Biến cho GraphQL operation"},
                "operation_name": {"type": "string", "description": "Tên operation (nếu nhiều op trong query)"},
                "headers": {"type": "object", "description": "HTTP headers (Authorization, Content-Type, ...)"},
                "method": {"type": "string", "enum": ["POST", "GET"], "description": "HTTP method (default POST)"},
                "timeout": {"type": "integer", "description": "Request timeout (s)"},
            },
            "required": ["endpoint", "query"],
        }

    def validate_args(self, args: Dict[str, Any]) -> Optional[str]:
        if not args.get("endpoint"):
            return "Missing required arg: endpoint"
        if not args.get("query"):
            return "Missing required arg: query"
        if not str(args["endpoint"]).startswith(("http://", "https://")):
            return "endpoint phải là URL http(s)://"
        return None

    def execute(self, args: Dict[str, Any], context: ToolContext) -> ToolResult:
        endpoint: str = args["endpoint"]
        query: str = args["query"]
        variables = args.get("variables") or {}
        operation_name = args.get("operation_name")
        headers: Dict[str, str] = args.get("headers") or {}
        method = str(args.get("method") or "POST").upper()
        timeout = int(args.get("timeout") or context.timeout or 30)

        # Dry-run // dry-run
        if context.dry_run:
            return ToolResult(
                success=True,
                output=f"[dry-run] Would send {method} GraphQL to {endpoint}",
                metadata={"dry_run": True, "endpoint": endpoint, "method": method, "operation_name": operation_name},
            )

        payload = {
            "query": query,
            "variables": variables,
        }
        if operation_name:
            payload["operationName"] = operation_name

        # Ưu tiên requests (nếu có), fallback urllib // prefer requests, fallback urllib
        try:
            import requests  # type: ignore
            use_requests = True
        except ImportError:
            use_requests = False

        try:
            if use_requests:
                # POST application/json (chuẩn GraphQL) // standard JSON POST
                if method == "POST":
                    resp = requests.post(  # type: ignore[union-attr]
                        endpoint,
                        json=payload,
                        headers=headers,
                        timeout=timeout,
                    )
                else:
                    # GET với query string // GET with querystring
                    import urllib.parse as up
                    qs = up.urlencode({"query": query, "variables": json.dumps(variables)})
                    resp = requests.get(  # type: ignore[union-attr]
                        f"{endpoint}?{qs}",
                        headers=headers,
                        timeout=timeout,
                    )
                status = resp.status_code
                try:
                    body = resp.json()
                except Exception:
                    body = {"raw": resp.text}
            else:
                # Fallback urllib // urllib fallback
                import urllib.request as ur
                import urllib.parse as up

                if method == "POST":
                    data = json.dumps(payload).encode("utf-8")
                    req_headers = dict(headers)
                    req_headers.setdefault("Content-Type", "application/json")
                    req = ur.Request(endpoint, data=data, headers=req_headers, method="POST")
                else:
                    qs = up.urlencode({"query": query, "variables": json.dumps(variables)})
                    req = ur.Request(f"{endpoint}?{qs}", headers=headers, method="GET")

                with ur.urlopen(req, timeout=timeout) as r:  # noqa: S310
                    status = r.status
                    raw = r.read().decode("utf-8", errors="replace")
                try:
                    body = json.loads(raw)
                except Exception:
                    body = {"raw": raw}

            # GraphQL trả về 200 ngay cả khi có errors // GraphQL may have errors
            has_errors = isinstance(body, dict) and bool(body.get("errors"))
            return ToolResult(
                success=(200 <= status < 300) and not has_errors,
                output=json.dumps(body, ensure_ascii=False, indent=2),
                error=(json.dumps(body.get("errors"), ensure_ascii=False, indent=2) if has_errors else None),
                return_code=status,
                metadata={
                    "endpoint": endpoint,
                    "method": method,
                    "status_code": status,
                    "has_errors": has_errors,
                    "operation_name": operation_name,
                },
            )
        except Exception as e:
            return ToolResult(success=False, error=str(e), return_code=1)