/plushcap/analysis/hasura/graphql-nulls-cheatsheet

Nulls in GraphQL: Cheatsheet

What's this blog post about?

This article discusses the concept of nullability in GraphQL and its implications. In GraphQL, all types are nullable by default, which is a departure from other typed languages where non-null types are the norm. The server returns a root field called errors for error handling purposes, while any fields with errors have the value null. Nulls become relevant when considering how to handle errors in GraphQL responses. The article provides a cheatsheet on using nulls in queries and responses, as well as discussing the pros and cons of nullable vs non-null types. While some developers prefer nullable types for their ability to provide partial data even in case of errors, others find them cumbersome and prefer non-null types for guaranteed values. Different GraphQL clients like Apollo and Relay have different approaches to handling nulls and errors. Apollo allows users to choose from three error policies: none (default), ignore, and all. On the other hand, Relay ignores GraphQL errors unless they affect the top-level data field or are explicitly thrown by the fetch function provided to the Relay Network. The article concludes with a recommendation to make everything nullable except when it doesn't make sense, but ultimately leaves the decision up to individual developers based on their specific needs and preferences.

Company
Hasura

Date published
May 15, 2020

Author(s)
Sezgi Ulucam

Word count
1801

Hacker News points
None found.

Language
English


By Matt Makai. 2021-2024.