Skip to content

feat: degraphql plugin proposal and simple implement #8787

Description

@Sn0rt

Description

DeGraphQL allows users to map URIs to fixed GraphQL queries through configuration, thereby converting GraphQL endpoints into ordinary endpoints.

parameter

name type requried default value format describe
endpoint string yes /    
query string yes “” graphql string The body of graphql that actually needs to be executed

enable plugin

You can enable the plugin in a specific route with the following command:

curl "http://127.0.0.1:9180/apisix/admin/routes/1"  \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
  "uri": "/me",
  "plugins": {
    "degraphql": {
      "endpoint": "/graphql",
      "query": "{\"query\":\"query{viewer{login}}\"}"
    }
  },
  "upstream": {
    "scheme":"https",
    "nodes": {
      "api.github.com:443": 1
    },
    "type": "roundrobin"
  }
}'

user case 1

the user want map a resetful api /me to [github.com/login](http://github.com/login) info

  1. enable plugin
curl "http://127.0.0.1:9180/apisix/admin/routes/1"  \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
  "uri": "/me",
  "plugins": {
    "degraphql": {
      "endpoint": "/graphql",
      "query": "{\"query\":\"query{viewer{login}}\"}"
    }
  },
  "upstream": {
    "scheme":"https",
    "nodes": {
      "api.github.com:443": 1
    },
    "type": "roundrobin"
  }
}'
  1. query route

to query the route info for double check

$ curl -s -X GET -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' "http://127.0.0.1:9180/apisix/admin/routes/1"  | jq

{
  "value": {
    "status": 1,
    "plugins": {
      "degraphql": {
        "query": "{\"query\":\"query{viewer{login}}\"}",
        "endpoint": "/graphql"
      }
    },
    "update_time": 1675435553,
    "uri": "/me",
    "priority": 0,
    "create_time": 1675396064,
    "upstream": {
      "pass_host": "pass",
      "type": "roundrobin",
      "hash_on": "vars",
      "nodes": {
        "api.github.com:443": 1
      },
      "scheme": "https"
    },
    "id": "1"
  },
  "createdIndex": 31,
  "modifiedIndex": 735,
  "key": "/apisix/routes/1"
}
  1. test

to send a request to /me path

curl -i -X GET "http://127.0.0.1:9080/me" -H "Host: api.github.com" -H "Authorization: bearer xxx"

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 38
Connection: keep-alive
Date: Thu, 02 Feb 2023 10:39:37 GMT
X-OAuth-Scopes: audit_log, gist, notifications, project, user
X-Accepted-OAuth-Scopes: repo
github-authentication-token-expiration: 2023-05-03 07:06:06 UTC
X-GitHub-Media-Type: github.v4; format=json
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4992
X-RateLimit-Reset: 1675335703
X-RateLimit-Used: 8
X-RateLimit-Resource: graphql
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
Vary: Accept-Encoding, Accept, X-Requested-With
X-GitHub-Request-Id: D111:7589:2F097:32432:63DB92E8
Server: APISIX/3.1.0

{"data":{"viewer":{"login":"Sn0rt"}}}

user case 2

a new RESTful API /describe?owner=apache&name=apisix to query the repo description

  1. create a route
curl "http://127.0.0.1:9180/apisix/admin/routes/2" -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
  "uri": "/describe",
  "plugins": {
    "degraphql": {
      "endpoint": "/graphql",
      "query": "{\"query\":\"query{repository(owner:\\\"$owner\\\",name:\\\"$name\\\"){description}}\"}"
    }
  },
  "upstream": {
    "scheme":"https",
    "nodes": {
      "api.github.com:443": 1
    },
    "type": "roundrobin"
  }
}'
  1. test

send a request to the /describe

curl -i -X GET "http://127.0.0.1:9080/describe?owner=apache&name=apisix" -H "Host: api.github.com" -H "Authorization: bearer xx"

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 71
Connection: keep-alive
Date: Sat, 04 Feb 2023 05:59:00 GMT
X-OAuth-Scopes: audit_log, gist, notifications, project, user
X-Accepted-OAuth-Scopes: repo
github-authentication-token-expiration: 2023-05-03 07:06:06 UTC
X-GitHub-Media-Type: github.v4; format=json
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4989
X-RateLimit-Reset: 1675492836
X-RateLimit-Used: 11
X-RateLimit-Resource: graphql
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
Vary: Accept-Encoding, Accept, X-Requested-With
X-GitHub-Request-Id: D196:25BA:5B0836:628EB6:63DDF423
Server: APISIX/3.1.0
Apisix-Plugins: degraphql

{"data":{"repository":{"description":"The Cloud-Native API Gateway"}}}

reference

https://docs.konghq.com/hub/kong-inc/degraphql/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions