degraphql

Description

The degraphql Plugin is used to support decoding RESTful API to GraphQL.

Attributes

NameTypeRequiredDescription
querystringTrueThe GraphQL query sent to the upstream
operation_namestringFalseThe name of the operation, is only required if multiple operations are present in the query.
variablesarrayFalseThe variables used in the GraphQL query

Example usage

Start GraphQL server

We use docker to deploy a GraphQL server demo as the backend.

  1. docker run -d --name grapql-demo -p 8080:8080 npalm/graphql-java-demo

After starting the server, the following endpoints are now available:

Enable Plugin

Query list

If we have a GraphQL query like this:

  1. query {
  2. persons {
  3. id
  4. name
  5. }
  6. }

We can execute it on http://localhost:8080/playground, and get the data as below:

  1. {
  2. "data": {
  3. "persons": [
  4. {
  5. "id": "7",
  6. "name": "Niek"
  7. },
  8. {
  9. "id": "8",
  10. "name": "Josh"
  11. },
  12. ......
  13. ]
  14. }
  15. }

Now we can use RESTful API to query the same data that is proxy by APISIX.

First, we need to create a route in APISIX, and enable the degreaph plugin on the route, we need to define the GraphQL query in the plugin’s config.

  1. curl --location --request PUT 'http://localhost:9180/apisix/admin/routes/1' \
  2. --header 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
  3. --header 'Content-Type: application/json' \
  4. --data-raw '{
  5. "uri": "/graphql",
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:8080": 1
  10. }
  11. },
  12. "plugins": {
  13. "degraphql": {
  14. "query": "{\n persons {\n id\n name\n }\n}\n"
  15. }
  16. }
  17. }'

We convert the GraphQL query

  1. {
  2. persons {
  3. id
  4. name
  5. }
  6. }

to JSON string "{\n persons {\n id\n name\n }\n}\n", and put it in the plugin’s configuration.

Then we can query the data by RESTful API:

  1. curl --location --request POST 'http://localhost:9080/graphql'

and get the result:

  1. {
  2. "data": {
  3. "persons": [
  4. {
  5. "id": "7",
  6. "name": "Niek"
  7. },
  8. {
  9. "id": "8",
  10. "name": "Josh"
  11. },
  12. ......
  13. ]
  14. }
  15. }

Query with variables

If we have a GraphQL query like this:

  1. query($name: String!, $githubAccount: String!) {
  2. persons(filter: { name: $name, githubAccount: $githubAccount }) {
  3. id
  4. name
  5. blog
  6. githubAccount
  7. talks {
  8. id
  9. title
  10. }
  11. }
  12. }
  13. variables:
  14. {
  15. "name": "Niek",
  16. "githubAccount": "npalm"
  17. }

we can execute it on http://localhost:8080/playground, and get the data as below:

  1. {
  2. "data": {
  3. "persons": [
  4. {
  5. "id": "7",
  6. "name": "Niek",
  7. "blog": "https://040code.github.io",
  8. "githubAccount": "npalm",
  9. "talks": [
  10. {
  11. "id": "19",
  12. "title": "GraphQL - The Next API Language"
  13. },
  14. {
  15. "id": "20",
  16. "title": "Immutable Infrastructure"
  17. }
  18. ]
  19. }
  20. ]
  21. }
  22. }

We convert the GraphQL query to JSON string like "query($name: String!, $githubAccount: String!) {\n persons(filter: { name: $name, githubAccount: $githubAccount }) {\n id\n name\n blog\n githubAccount\n talks {\n id\n title\n }\n }\n}", so we create a route like this:

  1. curl --location --request PUT 'http://localhost:9180/apisix/admin/routes/1' \
  2. --header 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
  3. --header 'Content-Type: application/json' \
  4. --data-raw '{
  5. "uri": "/graphql",
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:8080": 1
  10. }
  11. },
  12. "plugins": {
  13. "degraphql": {
  14. "query": "query($name: String!, $githubAccount: String!) {\n persons(filter: { name: $name, githubAccount: $githubAccount }) {\n id\n name\n blog\n githubAccount\n talks {\n id\n title\n }\n }\n}",
  15. "variables": [
  16. "name",
  17. "githubAccount"
  18. ]
  19. }
  20. }
  21. }'

We define the variables in the plugin’s config, and the variables is an array, which contains the variables’ name in the GraphQL query, so that we can pass the query variables by RESTful API.

Query the data by RESTful API that proxy by APISIX:

  1. curl --location --request POST 'http://localhost:9080/graphql' \
  2. --header 'Content-Type: application/json' \
  3. --data-raw '{
  4. "name": "Niek",
  5. "githubAccount": "npalm"
  6. }'

and get the result:

  1. {
  2. "data": {
  3. "persons": [
  4. {
  5. "id": "7",
  6. "name": "Niek",
  7. "blog": "https://040code.github.io",
  8. "githubAccount": "npalm",
  9. "talks": [
  10. {
  11. "id": "19",
  12. "title": "GraphQL - The Next API Language"
  13. },
  14. {
  15. "id": "20",
  16. "title": "Immutable Infrastructure"
  17. }
  18. ]
  19. }
  20. ]
  21. }
  22. }

which is the same as the result of the GraphQL query.

It’s also possible to get the same result via GET request:

  1. curl 'http://localhost:9080/graphql?name=Niek&githubAccount=npalm'
  1. {
  2. "data": {
  3. "persons": [
  4. {
  5. "id": "7",
  6. "name": "Niek",
  7. "blog": "https://040code.github.io",
  8. "githubAccount": "npalm",
  9. "talks": [
  10. {
  11. "id": "19",
  12. "title": "GraphQL - The Next API Language"
  13. },
  14. {
  15. "id": "20",
  16. "title": "Immutable Infrastructure"
  17. }
  18. ]
  19. }
  20. ]
  21. }
  22. }

In the GET request, the variables are passed in the query string.

Delete Plugin

To remove the degraphql Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

degraphql - 图1note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/graphql",
  5. "plugins": {},
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:8080": 1
  10. }
  11. }
  12. }'