We Ditched REST and Went With GraphQL: Right here’s Why | by Meteum Staff | Meteum | Mar, 2023
You will have heard of GraphQL, a younger and fast-growing API structure type that’s turning into more and more fashionable amongst builders. Though REST stays dominant and a few builders could also be hesitant to make use of an unfamiliar structure, GraphQL has seen huge adoption in data-heavy industries, corresponding to e-commerce.
However what precisely is GraphQL? Merely put, it’s a question language for APIs, designed to provide customers extra flexibility and effectivity when retrieving information. On this article, we’ll discover the advantages of utilizing GraphQL over REST for climate APIs and supply examples of the way to make requests to Meteum API.
Based on Postman’s State of the API 2022 report, GraphQL is without doubt one of the prime 4 architectural kinds of API, with 28% of builders utilizing it. Evaluate that to 24% in 2021, or to 22.5% in 2020: GraphQL is selecting up tempo yr by yr. We’re assured this quantity will proceed to develop, and there are quite a few causes for it.
For instance them, right here’s an instance utilizing mock parameters. We’re requesting forecasting information for New York Metropolis for 5 days. Let’s say we’re particularly fascinated by temperature and humidity and need to see each the present and the each day forecast. With REST, we’d have to specify these parameters as question parameters, which might lead to an unwieldy URL:
The REST API response can be a flat JSON object that dumps all the requested information in a single stage and in addition contains information that we didn’t explicitly request. Think about if we had been requesting information for a number of areas and a spread of a number of months — the horror!
In distinction, with GraphQL, we will request all the needed information with a single question, and we will even group the info into separate fields for the present climate circumstances and a each day forecast:
The response will probably be a neat and tidy hierarchical JSON object with nested fields that correspond to the requested information:
GraphQL makes it a lot simpler to request and handle complicated information constructions. The automated documentation and strongly-typed system of GraphQL additionally simplify work with the assorted information varieties and models of measurement generally present in climate APIs like ours.
There are numerous extra variations underneath the hood. Find out about the important thing benefits that GraphQL gives with a direct comparability in opposition to REST:
To get entry to the Meteum API, log in to the Meteum developer portal utilizing your work account (Google, Slack), or create a brand new Meteum account together with your work electronic mail.
After logging in to the portal, you can begin your free trial or select the API access plan that most accurately fits your wants. You’ll discover a new key in your account as quickly as your plan is lively. The bottom line is usable a couple of minutes after creation.
You should use your key to entry the API. For it to work, you want every API request to go this key as an HTTP request header. The header known as X-Meteum-API-Key.
To examine if the important thing works by way of the terminal command line, use this pattern question:
Sending requests to Meteum API is straightforward: every request is enclosed in curly braces and contains request parameters. For instance, to get the present temperature at a particular location, you’d name the weatherByPoint technique with the request parameter specifying the latitude and longitude of the situation.
You possibly can then specify which climate parameters you want, such because the temperature, and execute the request:
Meteum API is provided with a sandbox environment the place you’ll be able to create and execute requests utilizing autocomplete options and ideas that describe the GraphQL API schema intimately. The sandbox additionally contains ready-made JavaScript and Python code snippets in your queries.
Please check with our documentation for extra examples and a deeper look into Meteum API options, GraphQL syntax, and preliminary setup. You could find extra examples and run check queries in Meteum’s Postman workspace.
As we’ve established, GraphQL is a robust and easy-to-learn option to retrieve information from APIs. We selected GraphQL to construct a delightfully quick, useful, environment friendly, and stylish API platform. Retrieve precisely the info you want for any location on Earth — be it historic climate data, dwell observations, short-term nowcasts, or long-term forecasts.
Expertise the complete suite of options by getting a free key at meteum.ai. The trial provides you entry to all API options usually obtainable to Skilled subscription customers, together with the Meteum Rain API for deep precipitation information. Should you want one thing particular that we don’t but supply out of the field, reach out to us, and we’ll do our greatest to satisfy your necessities.