question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make null value configurable, to allow undefined

See original GitHub issue

undefined is practical in JS for destructuring default, so it would be great to allow graphql to avoid converting undefined to null

I see some return null or return Promise.resovle(null) frequently in the sources, how about using a NULL configuration variable, defaulting to null

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

30reactions
leebyroncommented, Mar 29, 2018

Thanks for this extra info. This makes way more sense now.

Unfortunately this isn’t something GraphQL.js would consider adding since it breaks some of the core constraints of the spec - which is that you will get a field in the result for every field you ask for in the query. Because of how JSON serializes data, undefined values imply removal of fields, which breaks this core constraint.

JavaScript is also a bit strange in this respect, since most languages do not have a concept of a runtime value for undefined. Since GraphQL.js seeks to be a reference implementation for other GraphQL libraries built in other languages, it’s important to remain close to the spec.

In this way, null and undefined actually have special meaning to GraphQL results outside of typical results. null means that the field requested is not available (either due to being non-existent or from an error) while undefined (or absent from response) means that the field was not requested.

To preserve the differences between null and undefined values as typical values rather than special meaning - perhaps because either you intend to translate to a transport mechanism different from JSON where undefined values are preserved (is there such a thing?) or you don’t intend to send the results over the network at all - then I would suggest taking a look at some of the libraries that offer resolver middleware to replace undefined with a different placeholder value.

12reactions
caubcommented, Dec 8, 2018

undefined (from server-side) also reduces JSON responses size

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fun JavaScript: Safely dealing with undefined/null values ...
We have “lifted” the number into a container, in this case, an array, we have excluded the value if it is undefined/null (by...
Read more >
null - JavaScript - MDN Web Docs - Mozilla
The null value represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy...
Read more >
Can I set null as the default value for a @Value in Spring?
This puts an empty String into the variable if the attribute is not present. I would like to have null as the default...
Read more >
Documentation - TypeScript 2.0
Null - and undefined-aware types. TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively.
Read more >
Understanding the difference between null and undefined in ...
A property with a null value must have it explicitly assigned. Properties not defined in an item have an undefined value. In the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found