Since the last few years GraphQL has been growing in popularity among developers and a lot of companies started adopting this “Query Language” to build their APIs, and became something very important to learn it and use it, and don’t worry https://www.howtographql.com/ is your favorite place to get started with it.

What is GraphQl ?

As a short answer: it’s a query language for APIs. Think of it like SQL for databases.

SQL is a query language for database that allows you to get exactly what you want from the database (columns, rows count, etc…). You get what you want and how much of it.

Now GraphQL in the other hands behaves the same way, you simply send a query to a single endpoint (keep that in mind “single endpoint”) of the server and ask him the data you want to get and the amount of it.

Unlike REST where you have to write and prepare an endpoint (URL) for each request you want, example:

let’s say you have a page where you want to display a specific author with ID 1 and his 5 last blogs

Using REST you have 2 options:

  1. You write an API (/api/author/1/with-last-5-blogs) to get the author and his 5 last blogs together and this API will be used only for this particular page (which is a very bad solution)

  2. You run two requests, the first one to get the author (/api/authors/1) and the second one to get his blogs (/api/authohrs/1/blogs)

Now using GraphQL you can be flexible as much as you want and in this case you send a query that tells the server to return the author with ID 1 and his last 5 blogs like so:

{
  author(id: "1"){
    books(last: 5){
      edges{
        node{
          title
        }
      }
    }
  }
}

and that’s it, like a boss! 😎

as you can see we get the author with his last 5 blogs, and from the blogs we get only the title of each one.

Now let’s see how to add GraphQL to Symfony and Angular and see how they work together shall we ? 😁.

#angular #graphql #symfony #api #web-development

GraphQL between Angular and Symfony
4.70 GEEK