How can I expose an existing graphql endpoint as a REST endpoint?

Hello,

I know we can convert existing rest endpoint to graphql as explained in REST to GraphQL the easy way with Tyk - Tyk API Management

But Is there any way I can do the reverse of it? I mean is it possible to convert existing GraphQL endpoints to REST endpoint via TYK?

Hey @sumeet.kumar.olx , I’m from the GraphQL team. Happy to help you on this!
Actually, it’s quite simple to do this.

Step 1: Add your GraphQL API to tyk. You’ve probably already done this.
It can be configured as internal if you don’t want to directly expose the GraphQL API,
but it’s not required.

Step 2: Create a new REST API.

Step3: Go to the Endpoint designer on the API from step 2. Create a new virtual Endpoint.
From this virtual Endpoint, create a POST Request to the (internal) GraphQL API.

Here’s a similar example that shows you how to make a request from within the virtual Endpoint: custom-plugin-examples/plugins/ve_formdata-post at master · TykTechnologies/custom-plugin-examples · GitHub

Once you’ve properly configured the virtual Endpoint, you’ve successfully created a REST API from your GraphQL API, all from within tyk.

Here’s an overview of the request chain:

Client → Tyk Gateway → Virtual Endpoint → (internal) GraphQL API → GraphQL upstream

I hope this helps.
Best,
Jens

For a more performant solution than virtual endpoints, but a little less usable, this can also be achieved by creating a Tyk HTTP api on top of your GraphQL api. You could then apply body transforms, header transforms and method transforms to expose a REST fascade to your GraphQL APl.

Let’s take the countries.trevorblades.com GraphQL api as an example.

echo -n eyJxdWVyeSI6InF1ZXJ5IHsgY29udGluZW50cyB7IGNvZGUgbmFtZSB9IH0ifQ== | b
ase64 --decode
{"query":"query { continents { code name } }"}

API Definition Snippet

"extended_paths": {
            "transform": [
              {
                "template_data": {
                  "input_type": "json",
                  "template_mode": "blob",
                  "enable_session": false,
                  "template_source": "eyJxdWVyeSI6InF1ZXJ5IHsgY29udGluZW50cyB7IGNvZGUgbmFtZSB9IH0ifQ==",
                  "input": "",
                  "output": ""
                },
                "path": "/continents",
                "method": "GET"
              },
              {
                "template_data": {
                  "input_type": "json",
                  "template_mode": "blob",
                  "enable_session": false,
                  "template_source": "eyJxdWVyeSI6InF1ZXJ5IHsgY291bnRyaWVzIHsgY29kZSBuYW1lIGNhcGl0YWwgfSB9IiwidmFyaWFibGVzIjp7ImNvZGUiOiJHQiJ9fQ==",
                  "input": "",
                  "output": ""
                },
                "path": "/countries",
                "method": "GET"
              }
            ],
            "transform_response": [],
            "transform_headers": [
              {
                "delete_headers": [],
                "add_headers": {
                  "Content-Type": "application/json"
                },
                "path": "/continents",
                "method": "GET",
                "act_on": false
              },
              {
                "delete_headers": [],
                "add_headers": {
                  "Content-Type": "application/json"
                },
                "path": "/countries",
                "method": "GET",
                "act_on": false
              }
            ],
            "transform_response_headers": [],
            "url_rewrites": [
              {
                "path": "/continents",
                "method": "GET",
                "match_pattern": "/continents",
                "rewrite_to": "https://countries.trevorblades.com/",
                "triggers": []
              },
              {
                "path": "/countries",
                "method": "GET",
                "match_pattern": "/countries",
                "rewrite_to": "https://countries.trevorblades.com/",
                "triggers": []
              }
            ],
            "method_transforms": [
              {
                "path": "/continents",
                "method": "GET",
                "to_method": "POST"
              },
              {
                "path": "/countries",
                "method": "GET",
                "to_method": "POST"
              }
            ]
          }

Example:

curl -s http://localhost:8080/trevorblades/continents | jq
{
  "data": {
    "continents": [
      {
        "code": "AF",
        "name": "Africa"
      },
      {
        "code": "AN",
        "name": "Antarctica"
      },
      {
        "code": "AS",
        "name": "Asia"
      },
      {
        "code": "EU",
        "name": "Europe"
      },
      {
        "code": "NA",
        "name": "North America"
      },
      {
        "code": "OC",
        "name": "Oceania"
      },
      {
        "code": "SA",
        "name": "South America"
      }
    ]
  }
}

You can now apply cache plugins and other middlewares.

Thanks @Jens_Neuse for pointing this out to me. But I’m using open source version of Tyk Gateway. How can I achieve the same in OS Gateway without Dashboard.

Hi @sumeet.kumar.olx , the open source gateway is a full GraphQL server in it’s own right, but the magic that handles transformations between REST/SOAP/GraphQL via low/no-code is part of the proprietary product. That means that any transforms you wish to create in open source will need to be created manually.

Thanks for quick response @James
Can you point me in the right direction? Any tutorial around it to achieve the same with open source version.

Also how can I monitor the request in open source version?

@sumeet.kumar.olx I don’t know of any tutorial to do something like that manually, it will be quite involved and not a small task!

When you say monitor the request, have you looked at Tyk Pump? It takes the analytics from the gateway and exports them to whatever BI tool or data sink you want. There are a bunch of pumps available, or you can write your own quite simply.

1 Like