Skip to content

Access Data in ListOf

Alexis ROLLAND edited this page Jan 1, 2020 · 5 revisions

Data created in ListOf can be accessed in two ways:

  • Directly from ListOf database using an ODBC or JDBC connection
  • Over http from the GraphQL API

Using Python via ODBC

Here is an example how to connect to ListOf database in Python via ODBC. It requires the package pyodbc.

import pyodbc

# Set connection string
login = 'postgres'
password = 'change_me'
connection_string = 'driver={PostgreSQL Unicode};server=localhost;port=5432;database=listof;'
connection_string = f'{connection_string}uid={login};'
connection_string = connection_string = f'{connection_string}pwd={password};'

# Connect to database
connection = pyodbc.connect(connection_string)
connection.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
connection.setencoding(encoding='utf-8')

# Execute dummy query for testing
cursor = connection.cursor()
cursor.execute("select 'Hello World';")
row = cursor.fetchone()
print(row)

Using Python via Http

Here is an example how to fetch data from ListOf GraphQL API in Python via http. It requires the package requests. Note if the list belongs to the Public user group, the http request can be executed without authentication. If the list belongs to another user group, you must first authenticate with a user and provide the token in the header of the following request.

Example without authentication

import requests

url = "http://localhost:5432/graphql"
headers = {'Content-Type': 'application/json'}
payload = {'query': '{allMyLists{nodes{id,myAttribute}}}'} # Some dummy GraphQL query
response = requests.post(url, headers=headers, json=payload)
data = response.json()
print(data)

Example with authentication

import requests

# Get token
url = "http://localhost:5432/graphql"
headers = {'Content-Type': 'application/json'}
payload = {'query': 'mutation{authenticateUser(input:{userEmail:"a@b.com",userPassword:"123"}){sysToken}}'} # Mutation to authenticate user
response = requests.post(url, headers=headers, json=payload)
data = response.json()
token = data['data']['authenticateUser']['sysToken']
headers['Authorization'] = 'Bearer ' + token 

# Get data
payload = {'query': '{allMyLists{nodes{id,myAttribute}}}'} # Some dummy GraphQL query
response = requests.post(url, headers=headers, json=payload)
data = response.json()
print(data)