Can I have a graphQL API as a upstream using kong gateway?

What do I need to do to configure properly a simple graphQL API in the Kong gateway (open source)?
Do I need to use a plugin?

basically, I have a graphQL API running ``http://localhost:5005/graphql```

and here what I did in kong:
service

{
    "host": "localhost",
    "id": "bf209028-20b8-48b4-84ed-87096114d7cf",
    "protocol": "http",
    "read_timeout": 60000,
    "tls_verify_depth": null,
    "port": 5005,
    "updated_at": 1610631073,
    "ca_certificates": null,
    "created_at": 1610630921,
    "connect_timeout": 60000,
    "write_timeout": 60000,
    "name": "hop-data-service",
    "retries": 5,
    "path": "/",
    "tls_verify": null,
    "tags": [
        "dev"
    ],
    "client_certificate": null
}

route

{
    "next": null,
    "data": [
        {
            "id": "4c170be9-ffb0-4d4c-b61a-79f701493da6",
            "tags": null,
            "updated_at": 1610631819,
            "destinations": null,
            "headers": null,
            "protocols": [
                "http",
                "https"
            ],
            "strip_path": false,
            "created_at": 1610630973,
            "request_buffering": true,
            "hosts": null,
            "name": "default",
            "service": {
                "id": "bf209028-20b8-48b4-84ed-87096114d7cf"
            },
            "paths": [
                "/graphql"
            ],
            "preserve_host": false,
            "regex_priority": 0,
            "response_buffering": true,
            "sources": null,
            "methods": [
                "POST"
            ],
            "https_redirect_status_code": 426,
            "path_handling": "v1",
            "snis": null
        }
    ]
}

when making a request to http://{{kong-server}}/graphql I am getting the message below:

{
  "message": "An invalid response was received from the upstream server"
}

I just found the DeGraphQL plugin but it is only available in the kong enterprise subscription. Is there another way to have that using the kong API gateway (open source)?

You don’t any plugins to move graphQL API behind Kong Community edition.

You can configure your SERVICE with service.path=/graphql. Add a ROUTE with route.path=/. (You can also set route.path to anything else as well.)

You are ready to hit curl {kongIP:port}/{route.path}

@hassanfarid I am facing the same issue while configuring the service for graphQL API in open source edition. I followed the same steps which you had posted above. But getting 404 Not found issue.

My backend endpoint looks like this http://localhost:80/query and using graphQL query to call my backend api.

I configured my service as below
{
“host”: “localhost”,
“id”: “b3e2c794-99d5-48d8-8522-54b78a89f396”,
“protocol”: “http”,
“read_timeout”: 60000,
“tls_verify_depth”: null,
“port”: 80,
“updated_at”: 1611241742,
“ca_certificates”: null,
“created_at”: 1611241742,
“connect_timeout”: 60000,
“write_timeout”: 60000,
“name”: “test”,
“retries”: 5,
“path”: “/query”,
“tls_verify”: null,
“tags”: null,
“client_certificate”: null
}

I configured my route as below:

{
“id”: “04ba3459-6eaa-491e-8af1-ae9335e813d9”,
“tags”: null,
“paths”: null,
“destinations”: null,
“headers”: null,
“protocols”: [
“http”,
“https”
],
“strip_path”: true,
“created_at”: 1611241759,
“request_buffering”: true,
“hosts”: [
“localhost”
],
“name”: null,
“updated_at”: 1611241759,
“snis”: null,
“preserve_host”: false,
“regex_priority”: 0,
“methods”: null,
“sources”: null,
“response_buffering”: true,
“https_redirect_status_code”: 426,
“path_handling”: “v0”,
“service”: {
“id”: “b3e2c794-99d5-48d8-8522-54b78a89f396”
}
}

While hitting the request via postman

http://konghost:80/query + graphQL query data as a body

I am getting error as 404 method not found

@rviegas Is that working for you at open source edition?

Actually, the issue is as you are running kong in a docker container, localhost means the kong container itself, and what you should do is run your graphql service also in a docker container within the same network as your kong and configure your service with the IP of your graphql service.

Getting your GraphQL service IP
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'  your-container-name
1 Like

Thanks for the reply @rviegas Let me try it

@Heema_Hinduja / @rviegas - here’s how my configurations look,

Service: (Most of them are defaults, only thing I have configured here are host, port, name, path)

{
		"host": "10.32.9.73",
		"id": "039536b9-b451-49f2-938e-abe654b96919",
		"protocol": "http",
		"read_timeout": 60000,
		"tls_verify_depth": null,
		"port": 11020,
		"updated_at": 1606337009,
		"ca_certificates": null,
		"created_at": 1606248336,
		"connect_timeout": 60000,
		"write_timeout": 60000,
		"name": "app-gateway",
		"retries": 5,
		"path": "\/graphql",
		"tls_verify": null,
		"tags": null,
		"client_certificate": null
	}

Route: (Most of them are defaults, only thing I have configured here are (service.id, path)

{
		"id": "246e468a-9fc3-4bc7-a790-84b981914234",
		"tags": null,
		"paths": ["\/app-gateway"],
		"destinations": null,
		"headers": null,
		"protocols": ["http", "https"],
		"strip_path": true,
		"created_at": 1606248364,
		"request_buffering": true,
		"hosts": null,
		"name": null,
		"updated_at": 1606248364,
		"snis": null,
		"preserve_host": false,
		"regex_priority": 0,
		"methods": null,
		"sources": null,
		"response_buffering": true,
		"https_redirect_status_code": 426,
		"path_handling": "v0",
		"service": {
			"id": "039536b9-b451-49f2-938e-abe654b96919"
		}
	}

Beyond this point for testing it, you can run following command.

curl http://10.32.9.73:8000/app-gateway

Notes:

  • your backend graphQL service should be running.
  • curl elimnates the cors issue, which can be resolved later on using plugin.

Good. Let me know here if it works fine or you are facing another problem.

fingers crossed.

@hassanfarid I tried the same steps you had mentioned but I getting Error: Parse Error: Expected HTTP/

@Heema_Hinduja Could you put here your service and route configurations?

@rviegas @hassanfarid

My Service config:

{
“host”: “upstream_backend_service_host”,
“id”: “3071ecb6-a6e7-4b0b-a4b4-20ccb69578b5”,
“protocol”: “http”,
“read_timeout”: 60000,
“tls_verify_depth”: null,
“port”: 80,
“updated_at”: 1611308686,
“ca_certificates”: null,
“created_at”: 1611308686,
“connect_timeout”: 60000,
“write_timeout”: 60000,
“name”: “upstream_backend_host_name”,
“retries”: 5,
“path”: “/query”,
“tls_verify”: null,
“tags”: null,
“client_certificate”: null
}

My Route config:

{
“next”: null,
“data”: [
{
“id”: “831e9880-a5cd-483f-b597-6d930ebe68f1”,
“tags”: null,
“paths”: [
“/app-gateway”
],
“destinations”: null,
“headers”: null,
“protocols”: [
“http”,
“https”
],
“strip_path”: true,
“created_at”: 1611309414,
“request_buffering”: true,
“hosts”: null,
“name”: null,
“updated_at”: 1611309414,
“snis”: null,
“preserve_host”: false,
“regex_priority”: 0,
“methods”: null,
“sources”: null,
“response_buffering”: true,
“https_redirect_status_code”: 426,
“path_handling”: “v0”,
“service”: {
“id”: “3071ecb6-a6e7-4b0b-a4b4-20ccb69578b5”
}
}
]
}

Curl:

curl -X POST
http://kong_host_name:9080/app-gateway
-H ‘accept: application/json’
-H ‘accept-encoding: gzip, deflate, br’
-H ‘cache-control: no-cache’
-H ‘connection: keep-alive’
-H ‘content-type: application/json’
-H ‘dnt: 1’
-H ‘origin: upstream_host’
-H ‘postman-token: ab470068-5eae-7f70-8918-b45f93cfd46c’
-d ‘’

Response:

POST http://in-data-kong-1.sit.n3b.bookmyshow.org:9080/query
Error: Parse Error: Expected HTTP/

Note: I am facing the same issue even while sending sample HTTP request given in this kong document

I think you need to make sure if your service you are trying to put behind Kong is working fine. Did you test it? If yes, how the request looks like when testing your service directly? Based on that, we can check the service/route configurations in Kong are right.