Connecting to a GraphQL API Using Python

Let’s automate and remove the margin for error, trying to get rid of the front end one step at a time.

What exactly is GraphQL? GraphQL in its simplest terms a query language used for the front end. We send a request and retrieve certain data back. GraphQL is also advanced enough to make changes in the data called mutations. But that requires a whole new article to explain.

So the main reason I wanted to connect to GraphQL directly is because we have a web application where we must manually fill in fields one by one. Not the most effective use of my time if we know it’s repetitive.

The old method I used was thru Selenium, which also caused room for error whenever front end engineers made some changes. So I did some research and decided why not just send the data directly thru GraphQL. I still grab data from SalesForce, I have an article showing you how to do that here. I would then process all that data and send to the GraphQL endpoint.

But I am getting sidetracked, let’s connect to GraphQL using Python and get some data back!

Getting Started
Assuming you already have Python installed the main modules you need are:
1. requests (used to connect to GraphQL)
2. json (used to parse GraphQL data)
3. pandas (used for visibility of our data)

Let’s import these modules into a new Python script.

import requests
import json
import pandas as pd

For tutorial purposes, we will connect to a GraphQL endpoint that does not require authentication. We will connect to a Rick and Morty GraphQL API!

Let’s start with something simple, a dataset of characters will do. The information I want from each of them is the name, status, species, type and gender. We can set this GraphQL query as a string and set it as a variable like this:

query = """query {
    characters {
    results {
      name
      status
      species
      type
      gender
    }
  }
}"""

The people who made this made it really easy for us to connect. We get a limit of 10000 requests per day, so use them sparingly. The next few lines of code we set the URL, send the request to them aka the query. If successful we should get a status_code of 200 and text in the format of a string.

url = 'https://rickandmortyapi.com/graphql/'
r = requests.post(url, json={'query': query})
print(r.status_code)
print(r.text)

Connecting to a GraphQL API Using Python

The r.text is in the format of a string. This is where the module json comes in. Let’s transform this string to a JSON format so we can move it to a DataFrame.

json_data = json.loads(r.text)

We only want name, status, species, type, and gender. So before we push it to a DataFrame, since it is a nested JSON we want to move further into the nest. Now we can send it to be translated to a DataFrame. We can do that with the following code:

df_data = json_data[‘data’][‘characters’][‘results’]
df = pd.DataFrame(df_data)

Our DataFrame should now look like this.
Connecting to a GraphQL API Using Python

Now that is has been transferred to a pandas DataFrame the possibilities are endless!

If you enjoy the content, please feel free to support me on Patreon!

#Python #GraphQL #api

Connecting to a GraphQL API Using Python
11.25 GEEK