Status Codes in GraphQL

Status Codes in GraphQL

As you all know, REST APIs use status codes as a part of the response (200-ok, 404-not found, 500-internal server error etc.). In GraphQL it’s a bit different because in most cases you just get 200 (or 500 if something goes really bad). But without status code and status message information you won’t be able to represent the results of a client’s request.

Image for post

Photo by Erik Mclean on Unsplash

As you all know, REST APIs use status codes as a part of the response (200-ok, 404-not found, 500-internal server error etc.). In GraphQL it’s a bit different because in most cases you just get 200 (or 500 if something goes really bad). But without status code and status message information you won’t be able to represent the results of a client’s request.

For this article you’ll need at least basic GraphQL and Typescript knowledge.


Server side

On a server-side we’re going to use SDL first approach. First, let’s write type definitions for a super simple query, which will get a Customer by ID.

This query will return customer for all existing customers in you DB, but it can also return null if you try to query for a nonexistent customer or if you don’t have permission to read that customer.

This solution is problematic because it doesn't provide any additional information about the data. Solution for that is a new layer in the type definition which will wrap customer type and add more information about response like status / status code and a message.

We just introduced a new type calledCustomerResult which wraps Customer and adds status and status message. In this case status is string, but it could be anything else what suits your needs (for example you could use integer and send HTTP codes).

You can also notice that now there is exclamation mark in customer query because CustomerResult won't be null — it means there will always be at least something in the response.

That’s it from type definition side. Now we’re just missing resolver for this query. Naive solution could look like this:

After we start adding more and more resolvers we could notice that there is repetition of a pattern in all resolvers — we always need to wrap results into an object and send it. Let’s extract those pieces of code into universal utility functions.

Those utility functions will make your code clean and DRY. If you need to replace a query status you do it on one place. Our resolvers will be nice and clean.

typescript api-development programming nodejs graphql code

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

How to Restart the TypeScript NodeJS Application Fast

ts-node one of the best tool for NodeJS-Typescript development but when comes to typescript it doesn't do the best job.

A Simple Guide to API Development Tools

APIs can be as simple as 1 endpoint for use by 100s of users or as complex as the AWS APIs with 1000s of endpoints and 100s of thousands of users. Building them can mean spending a couple of hours using a low-code platform or months of work using a multitude of tools. Hosting them can be as simple as using one platform that does everything we need or as complex as setting up and managing ingress control, security, caching, failover, metrics, scaling.

Tracking a Developer’s Journey From Documentation Visit

Measuring website activity provides only half the story. See how to best track the developer's journey and what funnel stages makes sense for API-first products

How to Market to Developers with Paid Marketing

Selling to developers is hard. How to market to developers efficiently using paid advertising leveraging inbound marketing techniques.