Our #frontendatbytex series continues with its 7th episode about migrating from REST to GraphQL. If you’re learning about frontend technologies and how to best use them depending on your project, this series is a resource to help you get ahead.
Below you will find the entire video script, as well as the video itself, from Alex Cojocariu, one of the Bytex team leads.
‘Hi, welcome back to Frontend at Bytex, my name’s Alex, and today we’ll talk about Migrating from REST to GraphQL.
Now, I’m sure all of you know about REST, after all, it’s been around for a while, and it’s like the old reliable VHS of API designs.It’s simple, easy to use, and gets the job done. But just like VHS, it’s not the most efficient way to do things.
There’s a chance that lately GraphQL has been popping up in conversations at your workplace, but what exactly is graphql ?
GraphQL is a query language and runtime for APIs that was developed and open-sourced by Facebook way back in 2015.
One of the key benefits of GraphQL is its flexibility. Unlike REST, where each endpoint returns a fixed set of data, in GraphQL the client can specify exactly what data it needs in a single request. This means that the client can get all the data it needs in a single round trip to the server, reducing the number of requests and improving performance. Additionally, GraphQL allows for real-time updates, so clients can subscribe to specific data and be notified when it changes.
Another advantage of GraphQL is its strong typing system, which allows developers to validate queries at the application level rather than relying on documentation or the user to ensure that they are sending valid requests. This can help to prevent errors and make the development process more efficient.
GraphQL also offers a better developer experience than REST. With GraphQL, developers can see all the available data and operations in a single schema, making it easier to understand and navigate the API. Additionally, GraphQL has an active and growing community, which provides a wealth of resources and tools for developers to use.
You could say GraphQL is the new kid on the block. It’s like the Blu-ray of API designs. It’s got more firepower, and it’s more versatile. It’s like a one-stop-shop for all your data needs. But, just like Blu-ray, it’s not as simple as REST, you gotta know what you’re doing.
Now, I know what you’re thinking: Hey Alex, graphql sounds pretty sweet but how can I convince my team to adopt it and where do we even start ?
I’m glad you asked, from my experience the first step is to never stop talking about graphql BUT in all seriousness, while it does depend on your app, here are 4 general steps to adding GraphQL to an existing application that uses REST :
- The first step is to define the GraphQL schema, which is a blueprint of the data and operations that will be available in the GraphQL API. This schema defines the types of data, the fields that are available, and the relationships between those types.
- Once the schema is defined, the next step is to create the GraphQL server. This server is responsible for handling GraphQL requests and returning the appropriate data. There are several libraries and frameworks available for creating a GraphQL server, such as Apollo Server, Express-GraphQL, and GraphQL-Yoga.
- After the GraphQL server is set up, it needs to be connected to the existing REST API so that it can access the data and perform operations. There are several ways to do this, such as using a library like apollo-datasource-rest to connect to the REST API, or by creating a custom connector that maps the GraphQL operations to the corresponding REST endpoints.
- The final step is to update the client-side code to use the GraphQL API instead of the REST API. This will typically involve updating the client-side code to use a GraphQL client library, such as Apollo Client, to interact with the GraphQL server.
It is important to note that these steps are not exhaustive and there might be more specific steps that are required based on the architecture of the app and the specific requirements of the project. Also, it’s important to test the integration and check if everything is working as expected.
To summarize, migrating from a RESTful API to a GraphQL API can bring significant benefits in terms of flexibility, performance, and efficiency. However, it is important to weigh the benefits against the challenges and determine if it is the right choice for the specific application. A thorough evaluation of the current API and the needs of the application should be conducted before making a decision.
That being said, thank you for joining us and we hope we’ll be seeing you next time. Bye bye.’
If you enjoyed this video about migrating from REST to GraphQL, make sure to follow us on social, and browse the previous episodes in the series.