全部產品
Search
文件中心

API Gateway:DeGraphQL外掛程式

更新時間:Mar 13, 2025

de-graphql外掛程式通過將URIs映射到GraphQL查詢,從而可以將GraphQL上遊轉換為傳統服務進行訪問。

運行屬性

外掛程式執行階段:認證階段 外掛程式執行優先順序:430

參數配置

參數

描述

預設

gql

graphql 查詢

不可為空

endpoint

graphql 查詢端點

/graphql

timeout

查詢連線逾時,單位毫秒

5000

domain

服務網域名稱,當服務來源是DNS配置

外掛程式使用

https://github.com/alibaba/higress/issues/268

測試組態

apiVersion: networking.higress.io/v1
kind: McpBridge
metadata:
  name: default
  namespace: higress-system
spec:
  registries:
  - domain: api.github.com
    name: github
    port: 443
    type: dns
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    higress.io/destination: github.dns
    higress.io/upstream-vhost: "api.github.com"
    higress.io/backend-protocol: HTTPS
  name: github-api
  namespace: higress-system
spec:
  ingressClassName: higress  
  rules:
  - http:
      paths:
      - backend:
          resource:
            apiGroup: networking.higress.io
            kind: McpBridge
            name: default
        path: /api
        pathType: Prefix
---
apiVersion: extensions.higress.io/v1alpha1
kind: WasmPlugin
metadata:
  name: de-graphql-github-api
  namespace: higress-system
spec:
  matchRules:
  - ingress:
    - github-api
    config:
      timeout: 5000
      endpoint: /graphql
      domain: api.github.com
      gql: |
           query ($owner:String! $name:String!){
              repository(owner:$owner, name:$name) {
                name
                forkCount
                description
             }
           }
  url: oci://higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/de-graphql:1.0.0

測試結果

curl "http://localhost/api?owner=alibaba&name=higress" -H "Authorization: Bearer some-token"

{
	"data": {
		"repository": {
			"description": "Next-generation Cloud Native Gateway",
			"forkCount": 149,
			"name": "higress"
		}
	}
}

GraphQL介紹

GraphQL 端點

REST API 有多個端點,GraphQL API 只有一個端點。

https://api.github.com/graphql

與 GraphQL 通訊

由於 GraphQL 操作由多行 JSON 組成,可以使用 curl 或任何其他採用 HTTP 的庫。

在 REST 中,HTTP 謂詞確定執行的操作。 在 GraphQL 中,執行查詢要提供 JSON 請求體,因此 HTTP 謂詞為 POST。 唯一的例外是內省查詢,它是一種簡單的 GET 到終結點查詢。

GraphQL POST 請求參數

標準的 GraphQL POST 請求情況如下:

  • 添加 HTTP 要求頭: Content-Type: application/json

  • 使用 JSON 格式的請求體

  • JSON 請求體包含三個欄位

    • query:查詢文檔,必填

    • variables:變數,選填

    • operationName:操作名稱,選填,查詢文檔有多個操作時必填

    {
      "query": "{viewer{name}}",
      "operationName": "",
      "variables": {
        "name": "value"
      }
    }

GraphQL 基本參數類型

  • 基本參數類型包含: String, Int, Float, Boolean

  • [類型]代表數組,例如:[Int]代表整型數組

  • GraphQL 基本參數傳遞

    • 小括弧內定義形參,注意:參數需要定義類型

    • !(歎號)代表參數不可為空

      query ($owner : String!, $name : String!) {
        repository(owner: $owner, name: $name) {
          name
          forkCount
          description
        }
      }

GitHub GraphQL 測試

使用 curl 命令查詢 GraphQL,使用有效 JSON 請求體發出 POST 請求。 有效請求體必須包含一個名為 query 的字串。

curl https://api.github.com/graphql -X POST \
-H "Authorization: bearer <PAT>" \
-d "{\"query\": \"query { viewer { login }}\"}"

{
  "data": {
    "viewer": {
      "login": "2456868764"
    }
  }
}
curl 'https://api.github.com/graphql' -X POST \
-H 'Authorization: bearer <PAT>' \
-d '{"query":"query ($owner: String!, $name: String!) {\n  repository(owner: $owner, name: $name) {\n    name\n    forkCount\n    description\n  }\n}\n","variables":{"owner":"2456868764","name":"higress"}}'

{
  "data": {
    "repository": {
      "name": "higress",
      "forkCount": 149,
      "description": "Next-generation Cloud Native Gateway | 下一代雲原生網關"
    }
  }
}

參考文檔