GraphQL is a powerful query language that enables developers to easily access and edit API data. With the help of nice little features from API tools like Postman or Insomnia – it is also possible to use GraphQL API to automate workflows and processes in Power Automate. This article will provide an overview of how you can make GraphQL API requests in Power Automate and parse its responses with an HTTP-action intended for (but not limited to) REST API.
For the demo purpose, I will extract traffic information from my site through my CDN provider Cloudflare and send me a notification on my phone when it has reached an all time high value of visitors.
The problems we are solving in Power Automate:
- Learn how to work with dynamic values in Power Automate and GraphQL requests.
- Get to know how to extract stringified JSON payload (raw GraphQL query string) through Insomnia or Postman and use it as a template for HTTP actions in Power Automate.
Prerequisites:
- Power Automate Premium license to use HTTP-actions
- Insomnia client application or Postman client application to work with GraphQL API.
- Access to a GraphQL API that you can send request to.
- For this demo I am going to use Cloudflare GraphQL (Cloudflare API v4 Documentation)
- Find the GraphQL Official API overview on Cloudflare API Documentation
- Useful link: Schema docs for the Cloudflare GraphQL Analytics API (johnspurlock.com) (Gives me a list of what’s possible to do with a free and pro plan (starred = pro plan) – some query fields listed are no longer in use after experimenting and has changed over time).
Setting up Cloudflare API access
In this demo, I am going to fetch a summary of web-traffic from Cloudflare and push out a notification when it is at its record high to my cellphone. To start, I first need to setup my existing Cloudflare API access for my website along with a token I can use. For this I start with logging in to my Cloudflare account and navigate to my Profile Account settings:
Building our GraphQL query and getting the raw request payload as string
For this part I am going to use my preferred API platform Insomnia. I will also show the Postman method to get the payload as well. But first, head over to your websites overview section.
Now, in order to be able to query requests, Cloudflare requires two headers included. Below is the query details:
URI:
https://api.cloudflare.com/client/v4/graphql
Header:
Authorization: Bearer {API Token}
X-AUTH-EMAIL: {your registered Cloudflare mail-account}
Now create your request, below is the query syntax for unique visitors.
Body:
query {
viewer {
zones(filter: { zoneTag: "{Your Zone ID}" }) {
httpRequests1dGroups(filter: { date: "2023-01-21" }, limit: 1) {
uniq {
uniques
}
}
}
}
}
Here’s the Postman equivalent method to extract the same JSON body:
Great! We got the necessary information we need, lets head to Power Automate and create our notification flow!
Create a scheduled Power Automate flow to notify record high visitor counts
Now to prepare our Power Automate flow, I want to include a Dataverse table that the flow can use to track previous data and store new data from our , each time it runs. I basically want it to be a log it can lookup previous data records and compare before sending me a new notification.
Today’s date:
formatDateTime(utcNow(), 'yyyy-MM-dd')
Run the flow once from here on so that we can examine the response payload.
First Compose – zones array:
first(body('HTTP_-_Cloudflare_Unique_Visitors')?['data']?['viewer']?['zones'])
Second Compose – unique values:
first(outputs('Compose_-_zones_array')?['httpRequests1dGroups'])?['uniq']?['uniques']
Great! Now that we have extracted the unique visitor value to the second compose action, we can focus on the Dataverse row filter to check if there are any records from the past with a value greater than our current unique visitor value. If there are no previous records higher than our current value, then we should check if there have been any values recorded for today’s date. <- this last logic will be used to check if I’ve already received a notification today, and if so – don’t send me another one for the rest of the day.
Add a “List rows” action for Dataverse. For “Table name” field, select the Dataverse table that we created earlier on. In the “Filter name” field – identify your Dataverse column’s logical name first, then paste in the syntax:
crb1d_uniquevisitors gt @{outputs('Compose_-_uniques_value')}and createdon lt @{startOfDay(utcNow())}
Then set “Row count” field to 1 – as we only need one result back.
Note:
Hurray! We’re now done extracting the values. Let’s continue with the final piece and define the “Conditions” actions, sending our notification!
First condition syntax:
empty(first(outputs('List_rows')?['body/value'])?['createdon'])
Second condition syntax:
first(outputs('List_rows')?['body/value'])?['crb1d_uniquevisitors']
Your website is now breaking last record high of visitors, passing @{sub(outputs('Compose_-_zones_array'),1)}!
Congratulations! We’ve now setup a notification flow for my website whenever I get a record high visitors! 😂
Summary
Through this demo, we’ve learned how to create an integration to Cloudflare’s GraphQL API by using Power Automates HTTP-action.
We’ve also had some hands on experience working with some complicated and nested values from the response and dig through the response payload to get our values by using the Compose actions with custom PowerFx formulas.
Last but not least, we’ve learned how to filter our Dataverse table by using multiple conditions to return specific row items when matched, and used a condition action to limit the notification triggers.
Leave a Reply