Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 4

GraphQL: Why using "last"  in "history" return error

Solved! Go to Solution.

Why getting "last"  commit using "history" return error?

error:Backwards pagination requires a `before` cursor to paginate the connection.

 

Return "first" works. Do I miss something?

 

Query:

{
  repository(owner: "octocat", name: "Hello-World") {
    first: object(expression: "master") {
      ... on Commit {
        history(path: "README", last:1) {
          edges {
            node {
              commitUrl
              committedDate
            }
          }
        }
      }
    }
  }
}

Error result:

{
  "data": {
    "repository": {
      "first": null
    }
  },
  "errors": [
    {
      "path": [
        "repository",
        "first",
        "history"
      ],
      "locations": [
        {
          "line": 5,
          "column": 9
        }
      ],
      "message": "Backwards pagination requires a `before` cursor to paginate the connection."
    }
  ]
}
3 Replies
Solution
Community Manager
Message 2 of 4

Re: GraphQL: Why using "last"  in "history" return error

What it is saying is that in order to give you the last X items, it needs to know from where to get the last X items, which in this case requires a cursor. You can retrieve a cursor by using the query:

 

{
  repository(owner: "octocat", name: "Hello-World") {
    first: object(expression: "master") {
      ... on Commit {
        history(path: "README", first: 10) {
          edges {
            node {
              commitUrl
              committedDate
            }
            cursor
          }
        }
      }
    }
  }
}

which returns:

 

{
  "data": {
    "repository": {
      "first": {
        "history": {
          "edges": [
            {
              "node": {
                "commitUrl": "https://github.com/octocat/Hello-World/commit/762941318ee16e59dabbacb1b4049eec22f0d303",
                "committedDate": "2011-09-14T04:42:41Z"
              },
              "cursor": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d 0"
            },
            {
              "node": {
                "commitUrl": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
                "committedDate": "2011-01-26T19:06:08Z"
              },
              "cursor": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d 1"
            }
          ]
        }
      }
    }
  }
}

Then you can take the cursor and execute:

 

{
  repository(owner: "octocat", name: "Hello-World") {
    first: object(expression: "master") {
      ... on Commit {
        history(path: "README", last: 1, before: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d 1") {
          edges {
            node {
              commitUrl
              committedDate
            }
            cursor
          }
        }
      }
    }
  }
}

to retrieve:

 

{
  "data": {
    "repository": {
      "first": {
        "history": {
          "edges": [
            {
              "node": {
                "commitUrl": "https://github.com/octocat/Hello-World/commit/762941318ee16e59dabbacb1b4049eec22f0d303",
                "committedDate": "2011-09-14T04:42:41Z"
              },
              "cursor": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d 0"
            }
          ]
        }
      }
    }
  }
}

But that doesn't give you the last element in the list, it gives you the item before the last one. So if you really want the last item in the list, you may want to write something like this:

 

{
  repository(owner: "octocat", name: "Hello-World") {
    first: object(expression: "master") {
      ... on Commit {
        history(path: "README") {
          edges {
            cursor
          }
        }
      }
    }
  }
}

Paginate through until you get to the last page, take the second to last cursor entry, and then execute:

 

{
  repository(owner: "octocat", name: "Hello-World") {
    first: object(expression: "master") {
      ... on Commit {
        history(path: "README", first: 1, after: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d 0") {
          nodes {
            commitUrl
            committedDate
          }
        }
      }
    }
  }
}

to retrieve:

 

{
  "data": {
    "repository": {
      "first": {
        "history": {
          "nodes": [
            {
              "commitUrl": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
              "committedDate": "2011-01-26T19:06:08Z"
            }
          ]
        }
      }
    }
  }
}

I hope that helps!

Copilot Lvl 2
Message 3 of 4

Re: GraphQL: Why using "last"  in "history" return error

Thanks a lot!

Is there a simpler way to find the first commit for a file?

Community Manager
Message 4 of 4

Re: GraphQL: Why using "last"  in "history" return error

Not that I know of through the GraphQL API, no.