Skip to main content
How to make request using graph-ql

How To Integrate GraphQL with Python

in this python tutorial, I’ll let you know how to integrate GraphQL with python3. GraphQL is a strongly typed query language that describes how to request data. We’ll create a python script that’ll return JSON as a response.

What’s GraphQL

GraphQL is a query language for the front end. We send a request and receive data in response. Everything is declared as a graph in GraphQL. You ask for what you want, and you’ll get it. There’s nothing more to say, and nothing less to say.

There are the following module dependencies into this python project:

  • requests – to send the http request.
  • json -This module is used to convert string data into json type data.

I’m not building a graph-ql server to get data; instead, I’m utilising third-party GraphQL endpoints that don’t require authentication. We’ll use the Rick and Morty GraphQL API Rick and Morty GraphQL API . We have a daily restriction of 10000 requests.

You can also checkout other python tutorials:

There are following information which is wanted from each of them. We can set this GraphQL query as a string and set it as variable lias follows:

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

Let’s send them a request, often known as a query. If everything goes well, we should get a status code of 200 and text in string format.

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

We are sending requests using the HTTP Post method and print the status code.

response.text has all data in string format. We’ll use python json module that helps to convert strings into JSON format. The load() method helps to convert string to JSON format.

json_data = json.loads(r.text)

Leave a Reply

Your email address will not be published. Required fields are marked *