In this tutorial we’ll learn how to send requests to Igloo using Python, we’ll assume that you are already familiar with the concepts of GraphQL requests and how Igloo works, if not you can check out our Getting Started series.
Quick sidenote: if you are just interested in the code and not the explanation, at the bottom of this page you’ll find the complete function to send any GraphQL request using Python.
Sending an HTTP request
Let’s start by sending a first GraphQL request to Igloo, to do this we’ll use the requests
library (install it with pip3 install requests
) to send the HTTP request and the json
standard library to format the request.
The key ingredients of our request are:
- the payload: it should be a JSON containing a field
query
whose value is the query or mutation to execute in Igloo - the headers: the request should have an
Content-Type
header specifying that the payload is a JSON and anAuthorization
header that contains your access token - the request verb: requests in GraphQL are always POST requests, even when you are not modifying data
Let’s start by writing a script that fetches our email:
If you substitute your token in the code and run it you should get a response like
{"data":{"user":{"email":"john@igloo.ooo"}}}
Parsing the response
In general the response of any Igloo request is a JSON containing a field data
if it was successful and a field errors
if something bad happened.
We can parse the response using the json.loads
function:
To avoid unexpected issues we should check if the response contains any errors before reading the data, to do that you can use the following script: