Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass in multiple cursors as variables to GitHub GraphQL API?

I'm looking up organization members based on a list of organization ids. Each org has a paged list of members with an endCursor. Since each endCursor will be different and each org has different numbers of members (and different number of pages), how can I pass in different cursors back as variables? If so, how would each cursor be associated to the org ID from the previous query?

query($orgIds:[ID!]!, $page_cursor:String) { // not sure how to pass in the cursor when different length lists are returned
    nodes(ids:$orgIds) {
        ... on Organization {
            id
            members(first: 100, after: $page_cursor) {
            edges {
                node {
                    id
                }
            }
            pageInfo {
                endCursor
                hasNextPage
            }
        }    
    }
}

I've read http://graphql.org/learn/pagination/ but I'm not seeing anything related to passing in multiple cursors for the same edge list.

like image 213
David Avatar asked Jun 06 '17 05:06

David


1 Answers

I haven't found any details in the graphql specs on how supply an array of cursors for the same edge list. GitHub would have to come up with a custom feature for that. Though I have a feeling it is not quite what you are looking for.

A cursor exist per node, so if you add cursor field to your edges request, you will get the cursors for all nodes within your request.

edges {
    cursor
    node {
        id
    }
}

Response would become something like this:

"edges": [
    {
      "cursor": "Y3Vyc29yOnYyOpLOAANaVM4AA1pU",
      "node": {
        "id": "MDQ6VXNlcjIxOTczMg=="
      }
    },

Please note that endCursor is not the same if you change the "first:100" parameter to let's say "first:5", because endCursor would be the last cursor of the last node of the 5 first results.

The only reference you will have from your cursor ID to your orginazation ID, would be that the structure of the object being returned from GitHub's graphql API. Any cursor in your example is a child of a specific organisation.

From my point of view, It would be up to your client to remember that reference if needed afterwards. With that in mind, you might want to simply iterate through the pages of a single organisation, before you go to the next. (supplying only 1 organisation pr request, and not an array).

like image 93
Lars Emil Christensen Avatar answered Oct 22 '22 06:10

Lars Emil Christensen