This plugin transforms a GraphQL upstream into a traditional endpoint by mapping URIs into GraphQL queries.
Configuration Reference
This plugin is compatible with DB-less mode.
In DB-less mode, you configure Kong Gateway declaratively. Therefore, the Admin API is mostly read-only. The only tasks it can perform are all related to handling the declarative config, including:
- Setting a target's health status in the load balancer
- Validating configurations against schemas
- Uploading the declarative configuration using the
/config
endpoint
Enable the plugin on a service
Enable the plugin globally
A plugin which is not associated to any service, route, or consumer is considered global, and will be run on every request. Read the Plugin Reference and the Plugin Precedence sections for more information.
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter | Description |
---|---|
name
required Type: string |
The name of the plugin, in this case degraphql . |
service.id
Type: string |
The ID of the Service the plugin targets. |
enabled
required Type: boolean Default value: true |
Whether this plugin will be applied. |
Usage
DeGraphQL needs a graphql endpoint to query. As an example, we are going to
build a REST API around https://api.github.com GraphQL service. For that reason
examples are going to include the header Authorization: Bearer some-token
.
Note this plugin differs from other plugins as far as configuration goes. It
needs to be activated on a service that points to a GraphQL endpoint
(sans /graphql
).
1. Create a Service and a Route in Kong:
$ curl -X POST http://localhost:8001/services \
--data name="github" \
--data url="https://api.github.com"
$ curl -X POST http://localhost:8001/services/github/routes \
--data paths="/api"
2. Configure the Plugin on the Service
The plugin takes over the service. From this point on, the service represents our REST api and not the graphql endpoint itself. It will return a 404 Not Found if no DeGraphQL routes have been configured.
$ curl -X POST http://localhost:8001/services/github/plugins \
--data name="degraphql"
3. Configure DeGraphQL Routes on the Service
Once the Plugin is activated on a Service, we can add our own routes to build our service, by defining URIs and associating them to GraphQL queries.
$ curl -X POST http://localhost:8001/services/github/degraphql/routes \
--data uri="/me" \
--data query="query { viewer { login } }"
$ curl http://localhost:8000/api/me \
--header "Authorization: Bearer some-token"
{
"data": {
"viewer": {
"login": "you"
}
}
}
GraphQL Query Variables can be defined on URIs:
$ curl -X POST http://localhost:8001/services/github/degraphql/routes \
--data uri='/:owner/:name' \
--data query='query ($owner:String! $name:String!){
repository(owner:$owner, name:$name) {
name
forkCount
description
}
}'
$ curl http://localhost:8000/api/kong/kong \
--header "Authorization: Bearer some-token"
{
"data": {
"repository": {
"description": "🦍 The Cloud-Native API Gateway ",
"forkCount": 2997,
"name": "kong"
}
}
}
The same Variables can also be provided as GET arguments:
$ curl -X POST http://localhost:8001/services/github/degraphql/routes \
--data uri='/repo' \
--data query='query ($owner:String! $name:String!){
repository(owner:$owner, name:$name) {
name
forkCount
description
}
}'
$ curl "http://localhost:8000/api/repo?owner=kong&name=kuma" \
--header "Authorization: Bearer some-token"
{
"data": {
"repository": {
"description": "🐻 The Universal Service Mesh",
"forkCount": 48,
"name": "kuma"
}
}
}
Available endpoints
List defined DeGraphQL Routes for a service
Create a DeGraphQL Route for a Service
Attributes | Description |
---|---|
uri |
path to map to a GraphQL query |
query |
GraphQL query to map to uri |
Edit a DeGraphQL Route for a Service
Attributes | Description |
---|---|
uri |
path to map to a GraphQL query |
query |
GraphQL query to map to uri |
Delete a DeGraphQL Route for a Service