DEV Community

kycodee
kycodee

Posted on

Intro to GraphQL

What is GraphQL?

GraphQl is a query language and a server-side runtime. It was initially released by Facebook back in 2015. Facebook created GraphQL in order to give it's developers access to a data-fetching API that was simple, but powerful enough to describe all of Facebook's enormous log of data. Unlike any of the other querying languages you may have used, GraphQL can query data from many different sources and does not require a specific data store, such as a postgres database. Since it's release back in 2015, it has become fairly popular and some would argue that it is the future of web APIs. I'm not a fortune teller so I can't confirm nor deny that statement, but what I can do is give you some insight on graphQL and it's abilities. Continue reading, as I will explain to you how graphQL can be used in web applications and how it's schemas work.

graphQL logo

Using GraphQL in Web Applications

As we all know, when building a web application we must have a way to retrieve data to be displayed on the user-interface. Luckily for us, graphQL was created just for this and is perfect for the task. In order to retrieve this data using graphQL, we must take the following steps. First, we must define one or more schemas that'll describe the type or types of data we can query from our database. This will allow us to grab data based off of the specific type that we used or created. After defining our schemas, we can add in resolvers. Resolvers, just like their name, are functions that resolve the data that is sent back in a response from the query. Resolvers also take up the responsibility of retrieving data from the database and sending it back in the way the query asked for it. Next, after adding in our resolvers, we must configure a graphQL server such as Express GraphQL, in order to interact with the schemas and resolvers we made. Lastly, we must set up a graphQL client that allows users to interact with an interface and send request to the database and that will be displayed on the user-interface or client.

graphql in web applications

GraphQL Schemas

Now it's time to go a little more in depth about those schemas we just talked about. In contrast to other querying languages, graphQL has it's own type system that is used to describe a schema. This type system's syntax is called Schema Definition Language or SDL for short. The schemas in graphQL are used to describe a collection of different types and how they relate to one another. In fact, there are some very important types that are already built into the graphQL system. These types are queries, mutations, and subscriptions. The action of the query type is fetching data from the server. The mutation type's job is to allow for requesting of modifications of the stored data. And last, but not least, is the subscription type that is used for alerting the client about changes to the data in the database.

GraphQL vs. REST

The most common comparison of graphQL is between it and REST APIs. They are both very popular architectural patterns used to build APIs. The biggest difference between these two architectural patterns is that REST APIs use HTTP requests and multiple endpoints to interact with data, while GraphQL allows clients to query the exact data they need through a single endpoint, which prevents the over-fetching of data. Even though after hearing this information you may be lead to believe that graphQL is more efficient, which would make you assume it is more popular, that is just not the case. REST APIs have been around a lot longer than graphQL and seems like it will continue to be the standard of data-fetching for a while.

graph vs rest api

Conclusion

In conclusion, graphQL seems to be an awesome option for developers looking to make the querying of data a bit easier on the themselves. If you are a professional developer or just a hobbyist, I'd recommend giving graphQL a try to see you if you prefer it rather than traditional querying languages. Thanks for reading my article and leave a comment on how you feel about the article and your experiences using graphQL.

Top comments (0)