Skip to content

Commit

Permalink
Skip importing export types in TS (#4754)
Browse files Browse the repository at this point in the history
Summary:
A fix for a typescript generation bug discovered from the work in #4753, with efforts to enable checking for all TS generated files #4745

This fixes a class of Typescript errors in generated typescript files with TS checking enabled:
```
/src/__generated__/mutation.graphql.ts(11,27): error TS2305: Module '"relay-runtime"' has no exported member 'Mutation'.
/src/__generated__/mutation.graphql.ts(11,27): error TS6133: 'Mutation' is declared but its value is never read.
```

The root cause is the exported type is both not exported by the TS types, and not used due to typescript using the `export default` syntax.
Where flow would generate the following export:
```
module.exports = ((node/*: any*/)/*: Mutation<
  validateMutationTest9ChangeNameIncludeMutation$variables,
  validateMutationTest9ChangeNameIncludeMutation$data,
>*/);
```
Typescript would generate the following, not using the `Mutation` type:
```
export default node;
```

This fix removes the extraneous import type when the language is set as typescript.

Pull Request resolved: #4754

Differential Revision: D60604034

fbshipit-source-id: f38fc80bba3b549a22ebbca544dca1f9e940cce0
  • Loading branch information
drewatk authored and facebook-github-bot committed Aug 7, 2024
1 parent e1668bb commit 0f49c2e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
14 changes: 11 additions & 3 deletions compiler/crates/relay-compiler/src/artifact_content/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,11 @@ pub fn generate_updatable_query(

// -- Begin Types Section --
let mut section = GenericSection::default();
let generated_types =
ArtifactGeneratedTypes::from_updatable_query(typegen_operation, skip_types);
let generated_types = ArtifactGeneratedTypes::from_updatable_query(
typegen_operation,
skip_types,
project_config.typegen_config.language,
);

if project_config.typegen_config.language == TypegenLanguage::Flow {
writeln!(section, "/*::")?;
Expand Down Expand Up @@ -358,6 +361,7 @@ pub fn generate_operation(
typegen_operation,
skip_types,
request_parameters.is_client_request(),
project_config.typegen_config.language,
);

if project_config.typegen_config.language == TypegenLanguage::Flow {
Expand Down Expand Up @@ -687,7 +691,11 @@ fn generate_read_only_fragment(

// -- Begin Types Section --
let mut section = GenericSection::default();
let generated_types = ArtifactGeneratedTypes::from_fragment(typegen_fragment, skip_types);
let generated_types = ArtifactGeneratedTypes::from_fragment(
typegen_fragment,
skip_types,
project_config.typegen_config.language,
);

if project_config.typegen_config.language == TypegenLanguage::Flow {
writeln!(section, "/*::")?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use common::NamedItem;
use graphql_ir::FragmentDefinition;
use graphql_ir::OperationDefinition;
use graphql_syntax::OperationKind;
use relay_config::TypegenLanguage;
use relay_transforms::RefetchableMetadata;
use relay_transforms::INLINE_DIRECTIVE_NAME;
use relay_transforms::UPDATABLE_DIRECTIVE;
Expand All @@ -27,8 +28,9 @@ impl ArtifactGeneratedTypes {
operation: &OperationDefinition,
skip_types: bool,
is_client_only: bool,
language: TypegenLanguage,
) -> Self {
if skip_types {
if skip_types || language == TypegenLanguage::TypeScript {
Self {
imported_types: "ConcreteRequest",
ast_type: "ConcreteRequest",
Expand Down Expand Up @@ -80,8 +82,12 @@ impl ArtifactGeneratedTypes {
}
}

pub fn from_updatable_query(typegen_operation: &OperationDefinition, skip_types: bool) -> Self {
if skip_types {
pub fn from_updatable_query(
typegen_operation: &OperationDefinition,
skip_types: bool,
language: TypegenLanguage,
) -> Self {
if skip_types || language == TypegenLanguage::TypeScript {
Self {
imported_types: "ConcreteUpdatableQuery",
ast_type: "ConcreteUpdatableQuery",
Expand All @@ -100,11 +106,15 @@ impl ArtifactGeneratedTypes {
}
}

pub fn from_fragment(fragment: &FragmentDefinition, skip_types: bool) -> Self {
pub fn from_fragment(
fragment: &FragmentDefinition,
skip_types: bool,
language: TypegenLanguage,
) -> Self {
let is_inline_data_fragment = fragment.directives.named(*INLINE_DIRECTIVE_NAME).is_some();
let is_updatable_fragment = fragment.directives.named(*UPDATABLE_DIRECTIVE).is_some();

if skip_types {
if skip_types || language == TypegenLanguage::TypeScript {
if is_inline_data_fragment {
Self {
imported_types: "ReaderInlineDataFragment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Post {
==================================== OUTPUT ===================================
//- __generated__/foo.graphql.ts
/**
* <auto-generated> SignedSource<<0e503f5c200310a5580a2b4853a499ce>>
* <auto-generated> SignedSource<<fbfb0da0e84763fffee1d191d9a7f6c4>>
* @lightSyntaxTransform
* @nogrep
*/
Expand All @@ -48,7 +48,7 @@ type Post {
/* eslint-disable */
// @ts-nocheck

import { Fragment, ReaderFragment } from 'relay-runtime';
import { ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type foo$data = {
readonly posts: ReadonlyArray<{
Expand Down Expand Up @@ -108,7 +108,7 @@ export default node;

//- __generated__/fooQuery.graphql.ts
/**
* <auto-generated> SignedSource<<20043d1d00f946a33727d27907f7f3f2>>
* <auto-generated> SignedSource<<119739ea84b9bdf74d937309d38abba4>>
* @lightSyntaxTransform
* @nogrep
*/
Expand All @@ -117,7 +117,7 @@ export default node;
/* eslint-disable */
// @ts-nocheck

import { ConcreteRequest, Query } from 'relay-runtime';
import { ConcreteRequest } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type fooQuery$variables = Record<PropertyKey, never>;
export type fooQuery$data = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default node;

//- __generated__/fooQuery.graphql.ts
/**
* <auto-generated> SignedSource<<461989c03655ebfecf253b741f65ef91>>
* <auto-generated> SignedSource<<fb8fb1474f3e44f8818e0d2fb6490eae>>
* @relayHash ae6874c86ce5db2df8d6b253a6a0ec13
* @lightSyntaxTransform
* @nogrep
Expand All @@ -65,7 +65,7 @@ export default node;

// @relayRequestID ae6874c86ce5db2df8d6b253a6a0ec13

import { ConcreteRequest, Query } from 'relay-runtime';
import { ConcreteRequest } from 'relay-runtime';
export type fooQuery$variables = Record<PropertyKey, never>;
export type fooQuery$data = {
readonly userName: string | null | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type User { name: String }
==================================== OUTPUT ===================================
//- __generated__/foo.graphql.ts
/**
* <auto-generated> SignedSource<<b8a79927acf6eb285be2d05e7d209ab8>>
* <auto-generated> SignedSource<<d2b6a2fbf057fd52d8aa0869bcccbe8e>>
* @lightSyntaxTransform
* @nogrep
*/
Expand All @@ -26,7 +26,7 @@ type User { name: String }
/* eslint-disable */
// @ts-nocheck

import { Fragment, ReaderFragment } from 'relay-runtime';
import { ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type foo$data = {
readonly name: string | null | undefined;
Expand Down
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<<6c58dff465555ee63fe212edde3a3e01>>
* <auto-generated> SignedSource<<f592d795251b0c88f6e5456bd47a8d10>>
* @lightSyntaxTransform
* @nogrep
*/
Expand All @@ -34,7 +34,7 @@ type User { name: String }
/* eslint-disable */
// @ts-nocheck

import { Fragment, ReaderFragment } from 'relay-runtime';
import { ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
import { foo as userFooResolverType } from "../foo";
export type barFragment$data = {
Expand Down

0 comments on commit 0f49c2e

Please sign in to comment.