The REST architectural style has been the standard for designing APIs for quite some time. One main critique of REST is that it can be inflexible at times. GraphQL enters the situation as an alternative to the mainstream and it even offers a more flexible and efficient solution than REST for fetching your data.

Let’s say for example we were attempting to fetch some data for a user. In some cases we’d find ourselves using multiple endpoints to gather the users information such as its posts and followers. Whereas with GraphQL you would simply send one Query and you’d get all the data you would need. Below is a sample GraphQL query:

query {
User(id: "dkaljker3") {
username:
posts {
caption
}
followers(last: 5) {
username
}
}
}

With the above query we are able to fetch the exact data required. Notice that we only fetch the User’s last 5 followers and only the posts caption, this helps prevent what is called Overfetching. Generally when an app makes a call to a REST API endpoint we download an array of JSON data. In this case if we wanted to display a list of the last 5 followers we’d end up downloading the entire follower list and then write out the logic to filter out the rest after it’s been downloaded. Now with GraphQL we can efficiently and quickly gather just the right amount of data required for our app.

Schema & Types

GraphQL has a strong type system. All the types that would be presented in the API are written out in the schema using the GraphQL Schema Definition Language (SDL). Now pair this with Typescript, on your next react project and you’ll have an extremely testable and efficient process during your development period. While the frontend team works to bring the UI to life they can easily test their components with mock data that is identical to what the completed API will provide. This allows for a seamless transition once the backend team completes their work.

AWS Amplify

For those interested in building out their first project using GraphQL I highly recommend using AWS Amplify . Their documentation is well done and their services such as their authentication are a pleasure to use. Look back to the sample query I had written earlier to fetch a User’s data. With amplify’s CLI you’ll be able to simply write out a couple of lines and AWS will do the rest to help you write out your queries for full CRUD functionality. Their authentication service also comes with a prebuilt login/sign up components that allows you to build prototypes rapidly for testing. It’s also used by a large amount of startups and larger companies, so learning amplify could be beneficial to those looking for a job.

type User @model {
id: ID!
name: String!
imageUri: String
status: String
}

The above lines of code written in your schema.graphql file will allow AWS Amplify to automate over 100 lines of code for your Queries. Make sure to read their documentation and start building out your own API to see for yourself. AWS’s free tier is more than enough for a simple personal project. With that being said, welcome to the world of GraphQL!

“Think in graphs, not endpoints.” — Lee Byron, Co-Creator of GraphQL

Recent graduate actively seeking full time opportunities. https://www.linkedin.com/in/vakas-akhtar-a4bb30170/