How to configure an HTTP request using requestTemplate parameter

We have seen HTTP request configuration using the inline variable. Now in this second post to configure HTTP request, let’s check configuration of HTTP request using requestTemplate object.

Let’s use the @http directive to send an HTTP request to the website https://newsapi.org/. A request to its APIs provides worldwide news headlines and articles.

First we will create the RequestTemplate object in the GraphQL editor and format the request. Let’s create requestTemplate - top-headlines.

mutation{  
  upsert(values:{  
    RequestTemplate:[  
      {  
        name:"top-headlines",  
        request: """{"q": "${vars.phrase}"}"""  
      }  
    ]  
  }){  
    id  
  }  
}

Here is the schema for HTTP request using requestTemplate parameter.

type Mutation {
  getTopHeadlines(phrase: String): Json @http(
    method: GET,
    url: "https://newsapi.org/v2/top-headlines?q=$vars.phrase&apiKey=$settings.APIKEY",
    headers: """{"Content-Type": "application/json"}""",
    requestTemplate: "top-headlines"
    saveAs: "Result"
  )
}
type Result {
   status: String,
   totalResults: Int,
   articles:Json
}
  • $vars.phrase field holds the input phrase to search related news headlines. The same has been used in the requestTemplate - top-headlines
  • Notice the use of APIKEY environment variable. The value of this field has to be set at runtime and the same gets sent over in the HTTP request url.
  • Result is the data type that saves the API response.

Let’s run the http request now by executing the getTopHeadlines function.

mutation{
  getTopHeadlines(phrase:"glacier")
}

Cross check the response in the Result object.

{
  find(type: Result, arcql: "*") {
    edges {
      node {
        ... on Result {
          status
          totalResults
          articles          
        }
      }
      cursor
    }
  }
}
#result
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "status": "ok",
            "totalResults": 1,
            "articles": [
              {
                "source": {
                  "id": "cbs-news",
                  "name": "CBS News"
                },
                "author": "CBS News",
                "title": "Ice shelf on Antarctica's \"doomsday glacier\" could shatter in next 5 years, researchers warn",
                "description": "In a report presented at the world's largest earth science conference this week, researchers revealed that the ice shelf on the Thwaites Glacier in Antarctica could collapse in just a few years. CBS News meteorologist and climate specialist Jeff Berardelli jo…",
                "url": "https://www.cbsnews.com/video/ice-shelf-on-antarcticas-doomsday-glacier-could-shatter-in-next-5-years-researchers-warn/",
                "urlToImage": "https://cbsnews2.cbsistatic.com/hub/i/r/2021/12/17/78c33e48-a535-4cc6-8cfc-331cb6cd7d1a/thumbnail/1200x630/1f7d660a6d0ca784924a3311b3fa247d/cbsn-fusion-ice-shelf-on-antarcticas-doomsday-glacier-could-shatter-in-next-5-years-researchers-warn-thumbnail-857932-640x360.jpg",
                "publishedAt": "2021-12-17T08:02:01+00:00",
                "content": "Watch CBSN Live\r\nCopyright © 2021 CBS Interactive Inc. All rights reserved.\r\nGet browser notifications for breaking news, live events, and exclusive reporting.\r\nNot NowTurn On"
              }
            ]
          },
          "cursor": "01FQ41NX2E55P3METW29D1A033"
        }
      ]
    }
  }
}