Skip to content
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

Add satisfies type assertion for typescript #4797

Closed
wants to merge 4 commits into from
Closed
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 @@ -25,7 +25,7 @@ type User { name: String }
==================================== OUTPUT ===================================
//- __generated__/barFragment.graphql.ts
/**
* <auto-generated> SignedSource<<f592d795251b0c88f6e5456bd47a8d10>>
* <auto-generated> SignedSource<<0f778584db7b20b3491a3ed42c61cdc1>>
* @lightSyntaxTransform
* @nogrep
*/
Expand All @@ -37,6 +37,9 @@ type User { name: String }
import { ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
import { foo as userFooResolverType } from "../foo";
// Type assertion validating that `userFooResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(userFooResolverType satisfies () => unknown | null | undefined);
export type barFragment$data = {
readonly foo: ReturnType<typeof userFooResolverType> | null | undefined;
readonly " $fragmentType": "barFragment";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type User { name: String }
==================================== OUTPUT ===================================
//- __generated__/barFragment.graphql.ts
/**
* <auto-generated> SignedSource<<5dff27df4f02d14a44779d79d3f98fbb>>
* <auto-generated> SignedSource<<7c2c6939d2e5610ed47a154e9cab2dfd>>
* @lightSyntaxTransform
* @nogrep
*/
Expand All @@ -38,9 +38,15 @@ type User { name: String }
// @ts-nocheck

import { ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
import { LiveState, FragmentRefs } from "relay-runtime";
import { foo as userFooResolverType } from "../foo";
import { ITestResolverContextType } from "@test/package";
// Type assertion validating that `userFooResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(userFooResolverType satisfies (
args: undefined,
context: ITestResolverContextType,
) => LiveState<unknown | null | undefined>);
export type barFragment$data = {
readonly foo: ReturnType<ReturnType<typeof userFooResolverType>["read"]> | null | undefined;
readonly " $fragmentType": "barFragment";
Expand Down
47 changes: 42 additions & 5 deletions compiler/crates/relay-typegen/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use intern::intern;
use itertools::Itertools;
use relay_config::TypegenConfig;

use crate::writer::FunctionTypeAssertion;
use crate::writer::KeyValuePairProp;
use crate::writer::Prop;
use crate::writer::SortedASTList;
use crate::writer::SortedStringKeyList;
Expand Down Expand Up @@ -77,11 +79,11 @@ impl Writer for TypeScriptPrinter {
AST::ReturnTypeOfMethodCall(object, method_name) => {
self.write_return_type_of_method_call(object, *method_name)
}
AST::AssertFunctionType(_) => {
// TODO: Add proper support for Resolver type generation in
// typescript: https://github.com/facebook/relay/issues/4772
Ok(())
}
AST::AssertFunctionType(FunctionTypeAssertion {
function_name,
arguments,
return_type,
}) => self.write_assert_function_type(*function_name, arguments, return_type),
AST::GenericType { outer, inner } => self.write_generic_type(*outer, inner),
AST::PropertyType {
type_,
Expand Down Expand Up @@ -364,6 +366,41 @@ impl TypeScriptPrinter {
}
write!(&mut self.result, ">")
}

fn write_assert_function_type(
&mut self,
function_name: StringKey,
arguments: &[KeyValuePairProp],
return_type: &AST,
) -> FmtResult {
writeln!(
&mut self.result,
"// Type assertion validating that `{}` resolver is correctly implemented.",
function_name
)?;
writeln!(
&mut self.result,
"// A type error here indicates that the type signature of the resolver module is incorrect."
)?;
if arguments.is_empty() {
write!(&mut self.result, "({} satisfies (", function_name)?;
} else {
writeln!(&mut self.result, "({} satisfies (", function_name)?;
self.indentation += 1;
for argument in arguments.iter() {
self.write_indentation()?;
write!(&mut self.result, "{}: ", argument.key)?;
self.write(&argument.value)?;
writeln!(&mut self.result, ",")?;
}
self.indentation -= 1;
}
write!(&mut self.result, ") => ")?;
self.write(return_type)?;
writeln!(&mut self.result, ");")?;

Ok(())
}
}

#[cfg(test)]
Expand Down
8 changes: 1 addition & 7 deletions compiler/crates/relay-typegen/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,7 @@ fn generate_resolver_type(

let ast = transform_type_reference_into_ast(&schema_field_type, |_| inner_ast);

let return_type = if matches!(
typegen_context.project_config.typegen_config.language,
TypegenLanguage::TypeScript
) {
// TODO: Add proper support for Resolver type generation in typescript: https://github.com/facebook/relay/issues/4772
AST::Any
} else if resolver_metadata.live {
let return_type = if resolver_metadata.live {
runtime_imports.resolver_live_state_type = true;
AST::GenericType {
outer: *LIVE_STATE_TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export type User__pop_star_name$normalization = {
-------------------------------------------------------------------------------
import type { FragmentRefs } from "relay-runtime";
import userPopStarNameResolverType from "PopStarNameResolver";
// Type assertion validating that `userPopStarNameResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(userPopStarNameResolverType satisfies (
rootKey: PopStarNameResolverFragment_name$key,
) => User__pop_star_name$normalization | null | undefined);
export type Foo_user$data = {
readonly poppy: {
readonly name: string | null | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export type User__pop_star_name$normalization = {
-------------------------------------------------------------------------------
import type { FragmentRefs } from "relay-runtime";
import userPopStarNameResolverType from "PopStarNameResolver";
// Type assertion validating that `userPopStarNameResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(userPopStarNameResolverType satisfies (
rootKey: PopStarNameResolverFragment_name$key,
) => User__pop_star_name$normalization | null | undefined);
export type Foo_user$data = {
readonly poppy: {
readonly name: string | null | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ extend type User {
==================================== OUTPUT ===================================
import { FragmentRefs } from "relay-runtime";
import userPopStarNameResolverType from "PopStarNameResolver";
// Type assertion validating that `userPopStarNameResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(userPopStarNameResolverType satisfies (
rootKey: PopStarNameResolverFragment_name$key,
) => unknown | null | undefined);
export type Foo_user$data = {
readonly poppy: NonNullable<ReturnType<typeof userPopStarNameResolverType>>;
readonly " $fragmentType": "Foo_user";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ extend type User {
==================================== OUTPUT ===================================
import { FragmentRefs } from "relay-runtime";
import userPopStarNameResolverType from "PopStarNameResolver";
// Type assertion validating that `userPopStarNameResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(userPopStarNameResolverType satisfies (
rootKey: PopStarNameResolverFragment_name$key,
) => unknown | null | undefined);
export type Foo_user$data = {
readonly poppy: ReturnType<typeof userPopStarNameResolverType> | null | undefined;
readonly " $fragmentType": "Foo_user";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export type ClientEdgeQuery_MyFragment_best_friend = {
-------------------------------------------------------------------------------
import { FragmentRefs, DataID } from "relay-runtime";
import clientUserBestFriendResolverType from "bar";
// Type assertion validating that `clientUserBestFriendResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(clientUserBestFriendResolverType satisfies () => {
readonly id: DataID;
});
export type MyFragment$data = {
readonly best_friend: {
readonly name: string | null | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export type ClientUser__blob$normalization = {
-------------------------------------------------------------------------------
import { FragmentRefs } from "relay-runtime";
import clientUserBlobResolverType from "bar";
// Type assertion validating that `clientUserBlobResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(clientUserBlobResolverType satisfies () => ClientUser__blob$normalization);
export type MyFragment$data = {
readonly blob: {
readonly data: string | null | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ type ClientUser {
==================================== OUTPUT ===================================
import { FragmentRefs } from "relay-runtime";
import clientUserNameResolverType from "bar";
// Type assertion validating that `clientUserNameResolverType` resolver is correctly implemented.
// A type error here indicates that the type signature of the resolver module is incorrect.
(clientUserNameResolverType satisfies () => unknown);
export type MyFragment$data = {
readonly name: NonNullable<ReturnType<typeof clientUserNameResolverType>>;
readonly " $fragmentType": "MyFragment";
Expand Down
Loading