GraphQL is an open source, declarative language that you can use to source data from your APIs. In a backend landscape with disparate APIs that all need to be tied together, GraphQL is like a middle layer interface between them.
Josh Oppenheim, a software engineer and one of our guests today, adds to this definition. “GraphQL allows you to declaratively say, ‘Hey, I want this data to look like this,’ and expect what you get back. So every time that call is made, it's always going to come back as that structure. You can also choose and add fields onto that.”
GraphQL has a wealth of features that can make developer’s lives easier, including:
Choosing GraphQL or REST
GraphQL and REST both fetch data, but there are differences between the two. “REST is a bit different from GraphQL,” Josh explains, “REST allows you to grab one resource by ID, but you don't get to decide what fields they are going to be, to put them together, or describe your data.”
So how do you know when to use REST vs. GraphQL? Josh says,“Generally speaking, I think they both have their place.” He goes on to describe the differences in how each works. “REST has an over-fetching problem, right? So GraphQL, I make one call, I describe my data, I get it back. REST, I make X amount of calls, I get my data, I structure it myself, I put it where it needs to go, and I discard the rest. So using REST in this example means wasted data, time, and developer effort.”
Our other guest, Developer Advocate Lucas Santos, says that REST and GraphQL are complementary, not at odds. He often uses GraphQL for a very specific use case. “In my opinion, logs are the best use case for GraphQL.”
Learn Graph QL
While you might be tempted to jump in and implement GraphQL right away, our guests advise a different approach. GraphQL has many online “playgrounds” where you can experience GraphQL, with no consequences. Josh says, “Learn the queries first before you try to implement it.”
Try GraphQL on Platform.sh with Gatsby or Strapi
Platform.sh is a robust, reliable hosting platform that gives development teams the tools to build and scale applications efficiently. Whether you run one or one thousand websites, you can focus on creating features and functionality with your favorite tech stack.