A Free GraphQL API for Johns Hopkins University COVID-19 Dataset
Rate this tutorial
You can retrieve an access token using the API like so:
It's a great dataset for education purposes and for pet projects. The MongoDB Atlas cluster is freely accessible using the user
readonlyand the password
readonlyusing the connection string:
In this blog post, I will first show you how to access our GraphQL endpoint securely. Then we will have a look at the documentation together to build out a variety of GraphQL queries to access all sorts of information in our dataset - all against a single API endpoint. Learning how to to use filters and request only specific fields in our data will help optimize the performance of your applications - by bringing you exactly the data you want - nothing more, nothing less.
APP_IDinto the URL gives you:
If you execute this query in your favorite shell, you will receive a JSON answer that looks like this one:
Five collections are available in this GraphQL API, and you can learn more about each one of them . Each have a "singular" query which can be used to retrieve a single document and a "plural" one to retrieve a list of documents. As an exception, the metadata collection contains only a single document so it offers no "plural" query.
|Collection||Query (singular / plural)||Fields available|
Here is the GraphQL query:
Now let's build an HTTP query with it. Don't forget to replace the
ACCESS_TOKENin the query with your own valid token.
Now let's refine our data query even further. We want to see how France is trending for the last week, so we will use a query filter:
Plus we will sort the dates in descending order with the most recent dates first. Remember with GraphQL, we can request as many or as few data fields as we want for the client. In this example, we'll only ask for the number of confirmed cases, deaths, and recoveries, along with the date. The final query with this filter and those specific fields is:
Or with cURL:
Which gives me:
Finally, let's find the three counties in the USA with the greatest number of confirmed cases:
Or with cURL:
MongoDB made setting up a GraphQL API really easy. And this GraphQL API made querying our Covid19 dataset to gain insight even easier. We update this data every hour, and we hope you will enjoy using this data to explore and learn.
I truly hope you will be able to build something amazing with this GraphQL API. Even if it won't save the world from the COVID-19 pandemic, I hope it will be a great source of motivation and training for your next pet project.