Server-side implementation of the GraphQL spec!
I have worked with REST API before for many projects and I always wondered if there was a simpler way to request specific data from the backend!
With the problem stated up, I started searching for solutions and noticed that there was an unusual hype about something called Graphql. This made me inquisitive about the buzz that surrounded it! So, I started digging in.
To begin with, what is Graphql?
GraphQL is a query language and server-side runtime for application programming interfaces (APIs). Frontend developers can construct requests that exactly pull the data requested from multiple data sources in a single API call.
What is a Graphql spec?
The language, grammar, and rules to be used when writing GraphQL documents.
Apollo Server is an open-source, spec-compliant GraphQL server that’s compatible with any GraphQL client.
How does Graphql work and why is it better than REST?
GraphQL was developed to cope with the need for more flexibility and efficiency! It solves many of the shortcomings and inefficiencies that developers experience when interacting with REST APIs.
Let’s take an example!
Frontend has requested only the name, post title, and the last 3 followers of a particular User Id on any social media platform.
With a REST API, 3 endpoints are required -
/users/<id> endpoint to fetch the initial user data that consists of name
/users/<id>/posts endpoint that returns all the posts for a user
/users/<id>/followers that returns a list of all followers per user
Thus, three endpoints are required to fetch the required data along with a lot of unnecessary data.
With GraphQL, a single query to the GraphQL server includes the concrete data requirements. Thus, all the required data is returned in a single API call.
The server then responds with a JSON object with all information requested.
Let's start building!
We need to create a Node.js project, you can learn how to do it here.
To install all dependencies, we need to run the command,
npm i express apollo-server-express --save
We need to initially edit a file called index.js to create an entry point for our application.
What is a schema?
GraphQL server uses a schema to describe the model of your available dataset. It defines a hierarchy of types with fields that are populated from your back-end data stores.
What are resolver functions?
GraphQl queries or mutations consist of a set of fields. Graphql server has one resolver function per field and the purpose of the resolver is to retrieve data for its fields.
Schema provides the information about queries and mutations available to the client.
Move into the folder where the files are created and run the command in your respective folder
Here’s what your output should look like -
Run your localhost on the browser with the route of /graphql which is —
You can also clone this project from my GitHub repository - here!
Test a Sample Query!
Type a default test query in the Operation section.
We get a Response with 200 Status.
We can now work with the server by loading our dataset and writing queries!
Our GraphQl server is working!!
Key Takeaways -
GraphQL is about seeing your data as a graph and by querying that graph, you can filter out and select only the information you need. However, a simple REST API is prefered while working with really simple API.
I highly recommend reading these documentations as they contain lots of information about further scope!