Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper abstraction over graphql #229

Open
aidanlister opened this issue Sep 27, 2022 · 0 comments
Open

Proper abstraction over graphql #229

aidanlister opened this issue Sep 27, 2022 · 0 comments

Comments

@aidanlister
Copy link

aidanlister commented Sep 27, 2022

Hi there,

I'm trying to switch over to this library from the ProdPerfect/monday library and I wasn't able to get it working with even a simple example. I could do board.get_items() but if I added any of the columns it would either build the query incorrectly or time out. I don't have a huge data set (250 items with 50 columns), but I see Monday is clamping down on API resource limits pretty heavily as of next week [1] so I suspect there needs to be more control over exactly what is fetched.

This is what I ended up with, querying graphql directly:

import requests

# Define the column IDs we care about (and the corresponding label for those headings)
MONDAY_COLS = (
    ("name", "Name"),
    ("text7", "Account code"),
    ("status", "Customer Status"),
    ("date5", "Loss Date"),
    ("loss_reason", "Loss Reason"),
    ("status7", "Product"),
    ("dropdown", "Sales Lead"),
    ("status35", "Attribution"),
    ("weblink8", "Server URL"),
    ("status65", "Currency"),
)

# Generic GraphQL helper (read only)
def monday_query(query):
    api_url = "https://api.monday.com/v2"
    headers = {"Authorization" : MONDAY_ACCESS_KEY}
    data = {'query' : query}
    r = requests.post(url=api_url, json=data, headers=headers)
    return r.json()

# Grab the column values for the Column IDs and Item IDs provided
def get_column_values_for_ids(ids, cols):
    id_str = ','.join(ids)
    field_str = ','.join([field for field, _ in cols])
    query = """{
        items (limit: 100, ids: [%s]) {
            name column_values (ids: [%s]) {
                title id type text
            }
        }
    }""" % (id_str, field_str)
    data = monday_query(query)
    return data['data']['items']


def get_items_from_monday(cols, board_id, logger=None):
    all_items = []
    page_no = 1
    while True:
        items_query = """{
            boards (ids: [%s]) {
                items (limit: 100, page: %s) {
                    id
                }
            }
        }""" % (str(board_id), str(page_no))
        res = monday_query(items_query)
        items = res['data']['boards'][0]['items']
        ids = [item['id'] for item in items]
        if not ids:
            break

        logger(f"Fetching data for {len(ids)} items ...")
        data = get_column_values_for_ids(ids, cols)
        all_items.extend(data)
        page_no += 1

    return all_items

Specifically,

  • I'm limiting the columns that get returned with name column_values (ids: [%s]) {
  • I'm fetching 100 item IDs at a time, then fetching the column values for those 100 IDs (100 being the new maximum the Monday API allows you to fetch).

Given the new API limits, is more fine grained control over the data fetched something that you'd implement?

Thanks for the work on the library to date.

[1] https://community.monday.com/t/adjustments-to-api-rate-limiting/18611

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant