How to consume GraphQL APIs from Azure Logic Apps
- Published on
- Reading time
- Authors
- Name
- Simon Waight
- Mastodon
- @simonwaight
If you've been reading my blog for a while then you will see that I am a heavy user of RESTful APIs in my solutions, going so far as to build a custom Azure Logic Apps Connector for Meetup's REST API. I've used this Connector in a few places and blog about one example previously in Integrate Meetup announcements with Microsoft Teams using Azure Logic Apps and Adaptive Cards.
Recently my solution for Meetup integration ceased working as Meetup decided to shift their API from REST to GraphQL without providing any advance notice. This change has quite a big impact on API consumers because the client interaction model and API endpoints are completely different between the REST and GraphQL. Thankfully GraphQL still utilises HTTP primitives and returns JSON responses so we at least have a starting point for our new client code.
In this post I am going to look at how you can call a GraphQL API from within an Azure Logic App by using the in-built HTTP Action.
Our sample query
For the purpose of this post I am going to use a GraphQL query against the Meetup GraphQL API which uses the predefined groupByUrlname query and use it to return the next scheduled event for the specified group. The sample is shown below.
query
{
groupByUrlname(urlname: "Azure-Sydney-User-Group")
{
name
id
upcomingEvents(input: {first: 1})
{
pageInfo
{
hasNextPage
hasPreviousPage
startCursor
endCursor
}
count
edges
{
cursor
node
{
id
title
eventUrl
images
{
id
baseUrl
preview
}
venue
{
name
}
dateTime
duration
timezone
endTime
isOnline
shortUrl
}
}
}
}
}
This query shows one of the benefits of GraphQL espoused by its proponents - the ability to send what is effectively two queries (get group AND event details) to the API at once and receive a single response. I must admit this is really handy and helped remove a step in my Logic App.
Using in a Logic App
As I hinted at above, GraphQL still uses all the usual HTTP primitives, so we can use the in-built HTTP Action in Logic Apps to call the Meetup GraphQL API.
The Meetup API documentation includes a few samples that show how to call the GraphQL API using curl which is a really handy way to expose exactly what is required. I've included a sample below.
# Originally from: https://www.meetup.com/api/guide/#p02-querying-section
query='query($eventId: ID) {
event(id: $eventId) {
title
description
dateTime
}
}
'
variables='{
"eventId": "276754274"
}'
query="$(echo $query)"
curl -X POST https://api.meetup.com/gql \
-H 'Authorization: Bearer {YOUR_TOKEN}' \
-H 'Content-Type: application/json' \
-d @- <<EOF
{"query": "$query", "variables": "$variables"}
The key items of note are:
- Wrap the GraphQL query and any variables in the body of your request as a JSON payload. Specify 'query' and 'variables' with their own JSON property.
- Set Content-Type to application/json.
- Use the POST HTTP verb.
- The URI to hit is https://api.meetup.com/gql.
Given I'm retrieving information on public Meetup groups I don't need to provide an Authorization token, but apart from that I can use the rest of the curl sample to help me setup my Logic App HTTP Action as shown below.
The response that comes back is in a JSON format which I will parse into an object to make it easier to use elsewhere in my Logic App. The easiest way to build the schema is to run the HTTP Action once, look at the 'output' of the Action in the Run, copy the response and then use the "Use sample payload to generate schema" option in the Parse JSON Action.
In the HTTP Action I am dynamically replacing the URL of the Meetup group with the RowKey from an Azure Storage table. I am doing this because my data design means the RowKey contains the URL component of the meetup group - i.e. "Azure-Sydney-User-Group".
The net result of this implementation is that my Meetup-to-Teams bridge is functional again, and in fact is now simplified with only a single Meetup API call. Unfortunately, I can't use my Custom Connector for the Logic App any more, but I at least my integration works and I can keep across new meetups as they are announced!
Happy Days! 😎