days
-3
-6
hours
-2
-2
minutes
-4
-3
seconds
0
-9
search
Why developers are embracing GraphQL

GraphQL for busy web developers

Dan O’Donnell
graphql
© Shutterstock / Allies Interactive

GraphQL has been available to the public as an open source project since 2015. Why is it growing so fast? This article explores GraphQL, how it hides database complexities, how the ecosystem supports it, and what paid and open source libraries are compatible with it. It’s never too late to begin using GraphQL and its many available services in your projects.

What is GraphQL?

GraphQL is an API Standard for building modern, data-driven applications that build on top of the widely known HTTP Protocol.

GraphQL was first created by Facebook while they were shifting their mobile app from HTML5 to native mobile app. Then, in 2015, Facebook made GraphQL open to the public as an open-source project.

With GraphQL, you define a Domain Specific Language (DSL) around your data structure, agnostically, in terms of:

  • The programming language you use. Being that it is an API standard, GraphQL is open for implementations in many languages, such as JavaScript, Java, and C#. Follow the standard with your language of choice!
  • The database engine you use. You have the freedom to implement GraphQL API Standards with almost all database engines. GraphQL cares to have the data ready rather than the source of the data, and where is it coming from.

For practionaries, GraphQL is a Query Language, that is used to query your data sources, and apply mutations on them. For this to work, GraphQL requires you to define a schema to list:

  • All the data objects in your domain model (User, Product, Customer)
  • The queries you want to expose to your API clients
  • The mutations you want to expose to your API clients

GraphQL will then collect the schema information, and build you a JSON-Like dynamic query model (without the data) that you use when writing your queries and mutations.

Given a GraphQL schema definition for data querying:

schema {
 query: Query
}

A Query object defined as:

type Query
 me: User
}

Where User object is defined as:

type User {
 id: ID
 name: String
 city: String
 state: String
 friends: [User]
}

GraphQL allows you to write a query to get all users in the database and return only their ID and name as follows:

query {
 me {
 id
 name
 }
}

And the end result of this query is a JSON response of the data as follows:

"data": {
 "me": {
 "id": 1,
 "name": "John Doe"
 }
}

This is GraphQL in brief! Let’s move on, and discover the power of GraphQL and why it’s growing so fast.

Why GraphQL is growing so fast

From the very start, GraphQL was seen as a “Savior” due to the failure of, and lack of features in the technology of that time. It has seen adoption among established technology brands and startups alike. This encouraged developers to start building new, or open-source, projects to implement GraphQL concepts, and standards, in many programming languages. This made it accessible to all developers, with varying degrees of knowledge or experience.

Cloud computing, and processing, combined with GraphQL, is the ultimate dream of any fullstack developer wanting to build apps without hindrance. Let’s look at why developers have welcomed GraphQL with open arms.

SOAP, REST, and Hell

The transition from SOAP to REST was an achievement in and of itself! A lot of the old problems were resolved by REST. It was only then that the cracks appeared when employing REST for building APIs. GraphQL addresses three of the most intense complaints associated with REST.

Endpoint explosion

REST enforces a form of limitation on defining the API Endpoints for each and every resource, defined in the application. So, you end up defining many Endpoints in your API, each with a different Request/Response format. This increases the complexity of maintaining, and improving them as you go. It’s frustrating for the developer accessing your APIs to remember the different endpoints provided by it. They have to track down the request/response payload for every endpoint in the API.

graphql

GraphQL requires defining a single endpoint. All clients will connect to this. For instance, based on the query, GraphQL will communicate with the database(s) via pre-defined resolvers to collect the data, and satisfy the query. The GraphQL endpoint gives you access to the entire DSL of your application. It’s up to you how you build your own queries and mutations, depending on the frontend application requirements.

graphql

The Goldilocks syndrome

With REST, you are always in a state of either under-fetching, or over-fetching the data. There is no middle ground! For instance, you define an API endpoint to return the User details without fetching the related Friends data. If you want to access a few fields on the User object, then your REST API will be in the over-fetching mode. On the other hand, if you want to access the User’s Friends (which were not returned by the API), then your REST API will be in the under-fetching mode. You need to do multiple round-trips to the server to get all the data you need. One option to resolve this situation is to be selective in what data to return as per the application’s requirements. However this is an extreme, and complex option to implement in REST APIs.

SEE ALSO: Revolutionizing the web development sector

GraphQL gives you the freedom to select any field or combination of fields on the DSL. You have access to all fields, but you opt-in for the fields you want. So you can define multiple queries, each with a different set of fields, without requiring any changes on the GraphQL API level.

graphql

Programming simplicity

The ease of GraphQL concepts and techniques combined with the power of GraphQL queries (selecting what you want, and returning one or more resources in the same query) makes it a first-class citizen of applications performing extremely well, even when connected to a single data source.

Moreover, GraphQL performs even better when dealing with multiple data sources in your application. You can write queries that can collect data about multiple resources, coming from multiple data sources and returned all together in a single response to the client-side.

Even if you are just starting with a single data source for your application, GraphQL is the tool for you, helping you expand and scale-up your data now and later, if need be.

GraphQL hides database complexities

It can be overwhelming, and extremely time consuming, for a full-stack developer to keep up with the backend. It’s refreshing when something is made to work for you!

The current databases ecosystem is rich with Object Relational Mapping (ORM). An ORM allows you to connect to a variety of databases, query, and mutate the data, without having to be an expert in database query languages. Of course, the more you know the better, but this is one less headache to deal with.

Now, combine GraphQL with ORMs, and you can simplify your database engine! After all, GraphQL requires building Resolvers. Resolvers are at the core of GraphQL Execution that allows GraphQL to fetch, or mutate data in the database. Therefore, with the use of ORMs in building your resolvers, you can maintain a “query language knowledge” distance between you and those databases. You can leave all the complexity of building up queries to the ORMS, yet benefit from the ease of writing ORM queries to satisfy GraphQL.

Once the resolvers are built, that’s it! You can start querying, and mutating, your data without writing a single line of database queries.

That’s one of the major reasons behind the fast growth of GraphQL. With GraphQL, you don’t have to learn and write complex SQL queries to retrieve your data. With the help of resolvers, GraphQL helps you split the complexity of a database query into smaller pieces. Each of these pieces, or resolvers, have a specific goal. It’s up to GraphQL to connect the dots, like a puzzle, and join multiple resolvers together, to generate the final outcome of a single GraphQL query.

This is great for both the performance, and scalability of the application, and for the developer. Developers don’t have to learn Database queries, and their complex syntax. All that is required is to write resolvers in their preferred programming language, and GraphQL will kick in to generate the final result.

GraphQL makes stateful apps simple. It’s no wonder developers are drawn to it.

How the ecosystem is supporting GraphQL

The support that the ecosystem is providing GraphQL can be summarized in three main areas:

Community efforts

Well-entrenched founding members of the GraphQL movement, especially those that pay attention to research and development, started adopting GraphQL early on. They understood the importance of advancement to help build, and provide better services, and solutions. Majority of these developments can be tracked on graphql.org. The GraphQL foundation was considered an endorsement, and helped GraphQL’s standing.

Paid and Open-Source products

More libraries are now built to be compatible with GraphQL, such as:

These products/libraries help you build GraphQL servers, and clients, from the ground up. They offer support to a wide spectrum of technology, making the possibility to adopt GraphQL simple.

On the server-side, they let you define your GraphQL types and schemas. On the client-side, they provide you with ready-made APIs to connect to the backend GraphQL server. They wrap all the communication details for you in a nice, and easy to use API.

GraphQL services

GraphQL Services in the cloud might be the smartest move to get started when using it. A GraphQL as-a-service gives you worry-free migration of your old database systems, building the GraphQL server, and serving your client apps.

You can either migrate or upload your existing databases, and those services will automatically build the GraphQL server on top of your database by inferring the types, and schema, required for the server.

On the other hand, you can start from scratch, and build your database structure in the cloud. This is useful, especially if you are starting a new project with GraphQL.

For instance, FaunaDB is a serverless cloud database with native GraphQL endpoints. It is globally distributed for ubiquitous low-latency access, which is strongly consistent, yet limitless in scale. FaunaDB GraphQL API makes it easy to perform queries, and mutations against your schema inside FaunaDB. FaunaDB offers generous free tiers, that let you get started without risk.

FaunaDB embraces GraphQL Standards, and gives you a more intuitive experience building a GraphQL-based application. For instance, with FaunaDB you don’t even have to define resolvers for GraphQL to work! FaunaDB does the bulk, giving you a smooth experience. Let’s see how!

To start using GraphQL on FaunaDB follow these steps to get started:

Signup and Login to FaunaDB Dashboard

Create a new GraphQL Database by clicking the NEW DATABASE button. You are prompted to enter a database name. That’s it!

graphql

Create a file called schema.gql containing the schema of your GraphQL database. Here’s a downloadable schema file to start you off.

Upload and import the GraphQL schema by navigating to the GRAPHQL menu item, and upload the schema file to FaunaDB servers. Once the schema is imported, you are redirected to a GraphQL Playground to start issuing GraphQL queries and mutations.

graphql

Start by creating a new record as shown below:

graphql

Now that the record is created successfully, let’s query for this data:

graphql

That’s the experience FaunaDB gives when working with GraphQL, making it easy to integrate GraphQL into our existing or new applications.

SEE ALSO: (Agile && metrics) ? Can agile metrics help developers and teams improve?

In general, these cloud-based services accelerate the use of GraphQL in your projects by either migrating existing ones, or starting new ones. They offer services for you to consume in your applications without having to go through the hassle of managing your databases, or writing your database queries. You get access to a service, and consume it like any other service in your application.

Conclusion

It’s never too late to start using GraphQL in your projects. More services are popping up to make the experience of using GraphQL easy and affordable. Many open-source, and paid products, are emerging help you build GraphQL servers, and write optimized queries, and mutations.

Only when you start using GraphQL, will you truly admire the experience, and uncover the powers hidden inside it.

As with any other technology, always keep an eye open on three main things when building GraphQL:

  • Authorize and secure GraphQL endpoint
  • Make use of data pagination (as it is already supported by GraphQL) not to bloat your servers, and bring them down
  • Server caching is a key in building performant and accessible applications.
Author

Dan O’Donnell

Dan O’Donnell is the Manager of Front-end Engineering at Fauna. Originally Australian, he has also lived and worked in the UK and the United States. Previously working at Fortune 500 companies and governmental agencies, he loves the culture, drive and tech stack that a vibrant startup like Fauna provides. He is passionate about finding technical solutions that work for people, not the other way around.


Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of