Using graphQL to read status of Data Elements

I’m interested in retrieving metadata items that are endorsed to a certain level and have been experimenting with graphQL to get this metadata.

Here’s a sample query which looks for items at “candidate” level only:

image

The first result can be seen in this screen:
image

A couple questions:

  1. Why is the first retrieved record present? It has a state of Standard, not Candidate.
  2. I can see in pageInfo that there are more pages to retrieve. How can I get page 2 instead of page 1?
  3. Is there a reason why we need to specify “candidate” in the query with all lower case, when the field we’re searching for in the results will start with a capital letter “Candidate”?
  4. How would I modify this query to pick up all data elements that are Candidate or Recorded? graphQL doesn’t like lines like “dataElements(status: “candidate”, status: “recorded”)”

Appreciate any tips :slight_smile:

OK I think I have Question 2 sorted.

graphQL doesn’t seem to like more than 100 results returned. If I try for 101, I’ll get a friendly error message stating the hard limit:

So we instead ask for a modest 100, and request some pageInfo data on where our block of 100 results starts, ends and whether or not we can expect an additional page:

image

  • endCursor: a kind of bookmark at the end of our set of results for this page (here we’re asking for 100 if we have that many or more). That is, this will display the cursor (bookmark) for the last item in our result set.

  • hasNextPage: tells us whether or not there is another page available after this one

  • cursor: the bookmark or identifier of the data element of interest. Each of the 100 results will have their own one.

So, let’s say we have a result set where hasNextPage is indeed true. How do we get page 2? The answer lies in the value of endCursor of your current page, and an extra parameter you pass to dataElements()

Now we know endCursor for page 1 has a value of “YXJyYXljb25uZWN0aW9uOjk5”, so if we add this in against the dataElements(after) parameter, we can retrieve page 2:

And we know there is indeed a page 3 also… knowing how this works makes it easy to pull it together into a code loop to extract all values regardless of how many pages it takes.

Sorry if this is obvious to folk, I haven’t needed to do this until now :slight_smile:

Still curious about Questions 1, 3 and 4 if anyone knows.

Hi Michael,

Sorry for the delay on this one. I’m glad to see you answered your 2nd question. I’ve added some more details below and answered the rest as well.

  1. Why is the first retrieved record present? It has a state of Standard, not Candidate.

I think this is a bug in the GraphQL, where is is searching for any item that has “ever had a status” of Candidate, not searching for items where the “current status” is Candidate. We’ll look into corecting this, and will also look at adding some extra queries for “status level X or above” and “ever had a status of X” as well.

  1. I can see in pageInfo that there are more pages to retrieve. How can I get page 2 instead of page 1?

In GraphQL you request a page, you can specify the point the “cursor” starts at by adding a “after” parameter. For example, you can request the first 5 metadata items from an Aristotle Metadata Registry like this:

query {
  metadata (first: 5) {
    pageInfo {
      endCursor
      hasNextPage
    }
   	edges {
      node {	
      	uuid
        name
      }
    } 
  }
}

Which will return a pageInfo section below that will help you paginate the query:

{
  "data": {
    "metadata": {
      "pageInfo": {
        "endCursor": "YXJyYXljb25uZWN0aW9uOjQ=",
        "hasNextPage": true
      },
      "edges": [
      	# A list of metadata here.
      ]
    }
  }
}

The important section is the endCursor (in this case YXJyYXljb25uZWN0aW9uOjQ=) which is GraphQL idendifier for the last item in this set.

In the next query you can modify the query to request items after this cursor to get the next set:

  metadata (first: 5, after: "YXJyYXljb25uZWN0aW9uOjQ=") {

This modified query with then get the first 5 items after the cursor point, and this query will give the endCursor that can be used for a 3rd query, and so on…

  1. Is there a reason why we need to specify “candidate” in the query with all lower case, when the field we’re searching for in the results will start with a capital letter “Candidate”?

This was just a system design preference, but could be improved. The query is designed to use the system identifier for the status level (“candidate”) and returns a human readable result (“Candidate”).

  1. How would I modify this query to pick up all data elements that are Candidate or Recorded? graphQL doesn’t like lines like “dataElements(status: “candidate”, status: “recorded”)”

For this you’ll need to multiple queries, but this can be done with GraphQL variables. Queries can accept a variable, which can be set when calling the query. For example, this query:

query ($state:String) {
  properties (status: $state) {

With this variables object, will all you to query recorded items only.

{
  "state": "recorded",
}

Then when rerunning the query, you can just change the Variables object. But how this is called will depend on the language you are using.

Thanks. Q1 was really throwing me, makes sense this is a bug.

The other things should be easy now. I thought an OR functionality would have been possible within graphQL, but knowing that there is no such thing makes it easy to get around the issue through successive graphQL calls through my own code.

All good, thanks again.

1 Like