Skip to content

Do not output undefined for nullable fields within @throwOnFieldError and @catch #4974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type User { name: String }
==================================== OUTPUT ===================================
//- __generated__/foo.graphql.js
/**
* <auto-generated> SignedSource<<aa8a9fd716975ca122b77708a6fca791>>
* <auto-generated> SignedSource<<70a7a244e5796d4e6e9f2fbcd76c50f0>>
* @flow
* @lightSyntaxTransform
* @nogrep
Expand All @@ -32,7 +32,7 @@ import type { Fragment, ReaderFragment } from 'relay-runtime';
import type { FragmentType } from "relay-runtime";
declare export opaque type foo$fragmentType: FragmentType;
export type foo$data = {|
+name: ?string,
+name: string | null,
+$fragmentType: foo$fragmentType,
|};
export type foo$key = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type User { name: String }
==================================== OUTPUT ===================================
//- __generated__/fooQuery.graphql.js
/**
* <auto-generated> SignedSource<<9b5cf0134e02913bc0d65df99f7ff0c3>>
* <auto-generated> SignedSource<<2850659094fb71d3762177cc38cd028d>>
* @flow
* @lightSyntaxTransform
* @nogrep
Expand All @@ -33,9 +33,9 @@ type User { name: String }
import type { ConcreteRequest, Query } from 'relay-runtime';
export type fooQuery$variables = {||};
export type fooQuery$data = {|
+me: ?{|
+name: ?string,
|},
+me: {|
+name: string | null,
|} | null,
|};
export type fooQuery = {|
response: fooQuery$data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default ((node/*: any*/)/*: Fragment<

//- __generated__/fooQuery.graphql.js
/**
* <auto-generated> SignedSource<<3313eddbfdd22f715c9f61877c3934fc>>
* <auto-generated> SignedSource<<993d76639831b1e563cb29101f10f1c9>>
* @flow
* @lightSyntaxTransform
* @nogrep
Expand All @@ -185,9 +185,9 @@ import {clientUser as queryClientUserResolverType} from "foo";
|});
export type fooQuery$variables = {||};
export type fooQuery$data = {|
+clientUser: ?{|
+clientUser: {|
+id: string,
|},
|} | null,
|};
export type fooQuery = {|
response: fooQuery$data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Person {
==================================== OUTPUT ===================================
//- __generated__/PersonComponentFragment.graphql.js
/**
* <auto-generated> SignedSource<<dd96467e5b9c67cd6e9fef4590fc9adb>>
* <auto-generated> SignedSource<<0b312efbb631920c43da94a7f1c6bcff>>
* @flow
* @lightSyntaxTransform
* @nogrep
Expand All @@ -39,9 +39,9 @@ import type { Fragment, ReaderFragment } from 'relay-runtime';
import type { FragmentType } from "relay-runtime";
declare export opaque type PersonComponentFragment$fragmentType: FragmentType;
export type PersonComponentFragment$data = {|
+some_person: ?{|
+some_person: {|
+name: string,
|},
|} | null,
+$fragmentType: PersonComponentFragment$fragmentType,
|};
export type PersonComponentFragment$key = {
Expand Down
37 changes: 33 additions & 4 deletions compiler/crates/relay-typegen/src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::fmt::Result as FmtResult;
use std::fmt::Write;

use ::intern::string_key::StringKey;
use intern::intern;
use itertools::Itertools;

use crate::writer::AST;
Expand Down Expand Up @@ -48,6 +49,7 @@ impl Writer for FlowPrinter {
AST::Union(members) => self.write_union(members),
AST::ReadOnlyArray(of_type) => self.write_read_only_array(of_type),
AST::Nullable(of_type) => self.write_nullable(of_type),
AST::Optional(of_type) => self.write_optional(of_type),
AST::NonNullable(of_type) => self.write_non_nullable(of_type),
AST::ExactObject(props) => self.write_object(props, true),
AST::InexactObject(props) => self.write_object(props, false),
Expand Down Expand Up @@ -212,6 +214,20 @@ impl FlowPrinter {
}

fn write_nullable(&mut self, of_type: &AST) -> FmtResult {
let null_type = AST::RawType(intern!("null"));
if let AST::Union(members) = of_type {
let mut new_members = Vec::with_capacity(members.len() + 1);
new_members.extend_from_slice(members);
new_members.push(null_type);
self.write_union(&new_members)?;
} else {
let members = vec![of_type.clone(), null_type];
self.write_union(&members)?;
}
Ok(())
}

fn write_optional(&mut self, of_type: &AST) -> FmtResult {
write!(&mut self.result, "?")?;
match of_type {
AST::Union(members) if members.len() > 1 => {
Expand Down Expand Up @@ -386,8 +402,6 @@ impl FlowPrinter {

#[cfg(test)]
mod tests {
use intern::intern;

use super::*;
use crate::writer::ExactObject;
use crate::writer::InexactObject;
Expand Down Expand Up @@ -426,18 +440,33 @@ mod tests {
);
}

#[test]
fn optional_type() {
assert_eq!(
print_type(&AST::Optional(Box::new(AST::String))),
"?string".to_string()
);

assert_eq!(
print_type(&AST::Optional(Box::new(AST::Union(SortedASTList::new(
vec![AST::String, AST::Number,],
))))),
"?(string | number)"
)
}

#[test]
fn nullable_type() {
assert_eq!(
print_type(&AST::Nullable(Box::new(AST::String))),
"?string".to_string()
"string | null".to_string()
);

assert_eq!(
print_type(&AST::Nullable(Box::new(AST::Union(SortedASTList::new(
vec![AST::String, AST::Number,],
))))),
"?(string | number)"
"string | number | null"
)
}

Expand Down
1 change: 1 addition & 0 deletions compiler/crates/relay-typegen/src/type_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub(crate) struct TypeSelectionLinkedField {
pub(crate) conditional: bool,
pub(crate) concrete_type: Option<Type>,
pub(crate) is_result_type: bool,
pub(crate) emit_semantic_type: bool,
}

#[derive(Debug, Clone)]
Expand Down
34 changes: 32 additions & 2 deletions compiler/crates/relay-typegen/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl Writer for TypeScriptPrinter {
AST::Union(members) => self.write_union(members),
AST::ReadOnlyArray(of_type) => self.write_read_only_array(of_type),
AST::Nullable(of_type) => self.write_nullable(of_type),
AST::Optional(of_type) => self.write_optional(of_type),
AST::NonNullable(of_type) => self.write_non_nullable(of_type),
AST::ExactObject(object) => self.write_object(object),
AST::InexactObject(object) => self.write_object(object),
Expand Down Expand Up @@ -217,6 +218,20 @@ impl TypeScriptPrinter {
}

fn write_nullable(&mut self, of_type: &AST) -> FmtResult {
let null_type = AST::RawType(intern!("null"));
if let AST::Union(members) = of_type {
let mut new_members = Vec::with_capacity(members.len() + 1);
new_members.extend_from_slice(members);
new_members.push(null_type);
self.write_union(&new_members)?;
} else {
let union_members = vec![of_type.clone(), null_type];
self.write_union(&union_members)?;
}
Ok(())
}

fn write_optional(&mut self, of_type: &AST) -> FmtResult {
let null_type = AST::RawType(intern!("null"));
let undefined_type = AST::RawType(intern!("undefined"));
if let AST::Union(members) = of_type {
Expand Down Expand Up @@ -447,18 +462,33 @@ mod tests {
);
}

#[test]
fn optional_type() {
assert_eq!(
print_type(&AST::Optional(Box::new(AST::String))),
"string | null | undefined".to_string()
);

assert_eq!(
print_type(&AST::Optional(Box::new(AST::Union(SortedASTList::new(
vec![AST::String, AST::Number],
))))),
"string | number | null | undefined"
)
}

#[test]
fn nullable_type() {
assert_eq!(
print_type(&AST::Nullable(Box::new(AST::String))),
"string | null | undefined".to_string()
"string | null".to_string()
);

assert_eq!(
print_type(&AST::Nullable(Box::new(AST::Union(SortedASTList::new(
vec![AST::String, AST::Number],
))))),
"string | number | null | undefined"
"string | number | null"
)
}

Expand Down
Loading
Loading