Learning GraphQL

5beberson
3 min readJun 14, 2021

--

I started my job search about a month ago after graduating from Flatiron School on April 2021. Looking at the different job postings, I noticed that a lot of companies had graphQl in their requirements so I decided to start learning it.

What is graphQL?

It is a query language for reading and mutating data in apis.

As a back-end developer, graphQL provides a type system where you can describe a schema for your data and in turn, this gives front-end consumers of the apis the power to explore and request the exact data they need.

GraphQL vs Rest

Traditionally, web developers have consumed apis using rest where data entities live on a bunch of urls on a server. When a request is received, the api responds with the full data payload of that entity. In this case, there are two potential drawbacks:

  • we may need multiple entities at one time, in which case, each request is under fetching the actual data we want.
  • we may only want a small subset of a data entity, in which case we need to over fetch from the api which is bad for the environment.

Instead of multiple urls, a graphQL api has a single entry point. Data is queried or fetched by describing it with a syntax that mirrors its return shape in json. The front-end developers describe the data they want while the back-end developer writes code to resolve the request. And it all happens in a syntax that can work with any programming language.

Defining a schema

We can start defining a schema with our own custom objects using the type keyword. A type can have multiple fields like a unique id which we make required with a bang (!). We can also give it integer and string values then create a relationship with another type. A creator can have many videos which we represent by wrapping the type in brackets. On the other side, a video belongs to a creator as shown below

Example of a youtuber having videos

Every graphQL api has a query type which is the main entry point for a consumer of the api. We can query a list of videos or an individual user based on their id. That’s how a consumer reads data, but they may also want you to mutate data, in which case, we implement a mutation type that defines how data can be modified on the api.

query type and mutation type

From there, we can define code to resolve this data in any programming language. Once deployed, any developer consuming this api will be able to explore it with a complete understanding of all possible queries and data entities, which means the tooling can autocomplete the query as you type it out in the editor.

Who’s using GraphQL?

Facebook’s mobile apps have been powered by GraphQL since 2012. A GraphQL spec was open sourced in 2015 and is now available in many environments and used by teams of all sizes.

Why using GraphQL?

GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

--

--

5beberson

Made in France, built in USA. Living the American dream