Python Interfacing and Examples

Because most GraphQL APIs are single-endpoint and POST only, interfacing Programatically is incredibly easy. We recommend using the graphqlclient Python package. With this package, you’ll set up your session by: 1 Connecting to the endpoint: client = GraphQLClient("<URL>") 2. Attaching your authorization header client.inject_token('Bearer <TOKEN>')

From that point, any query or mutation is run in the same way: client.execute( "<Query JSON string>") or client.execute( "<Query JSON string>", {"var1" : val1} ) if you want to pass variables.

Variable format must be as a dict, with the key as a string equal to the variable name, value as the expected input format for the function.

# Two things to run this script
# 1. run "pip install graphqlclient"
# 2. Fill in <API-KEY-HERE> (https://app.labelbox.com/settings/apikey)

import json
from graphqlclient import GraphQLClient
client = GraphQLClient('https://api.labelbox.com/graphql')
client.inject_token('Bearer <API-KEY-HERE>')

def me():
res_str = client.execute("""
query GetUserInformation {
user {
id
email
organization{
id
}
}
}
""")

res = json.loads(res_str)
return res['data']['user']

def getUser(userID):
#Takes in a user ID string and returns information about that user
res_str = client.execute("""
query getUserInformation($userID : ID!){
user(where: {id: $userID}){
id
email
organization{
id
}
}
}
""", {"userID":userID})

res = json.loads(res_str)
return res['data']['user']

if __name__ == "__main__":
user_info = me()
print(user_info)

with_var = getUser(user_info['id'])
print(with_var)


How did we do?