Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit f3a7518

Browse files
committed
[TS generator] Make sure the ref names are declared
In cases where we cannot import the correct ref type, declare it as any.
1 parent dcf846f commit f3a7518

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/TypeScriptGenerator.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,16 @@ function groupRefs(props: Selection[]): Selection[] {
465465
return result;
466466
}
467467

468+
function createAnyTypeAlias(name: string): ts.TypeAliasDeclaration {
469+
return ts.createTypeAliasDeclaration(
470+
undefined,
471+
undefined,
472+
ts.createIdentifier(name),
473+
undefined,
474+
ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)
475+
);
476+
}
477+
468478
function getFragmentImports(state: State) {
469479
const imports: ts.Statement[] = [];
470480
if (state.usedFragments.size > 0) {
@@ -487,6 +497,10 @@ function getFragmentImports(state: State) {
487497
relativeReference + path.join(relative, usedFragment + ".graphql")
488498
)
489499
);
500+
} else {
501+
imports.push(
502+
createAnyTypeAlias(refTypeName),
503+
);
490504
}
491505
}
492506
}

test/__snapshots__/TypeScriptGenerator-test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ fragment ConcreateTypes on Viewer {
6464
}
6565
6666
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
67+
type OtherFragment_ref = any;
68+
type PictureFragment_ref = any;
69+
type UserFrag1_ref = any;
70+
type UserFrag2_ref = any;
6771
import { FragmentReference } from "relay-runtime";
6872
export enum FragmentSpread_ref {
6973
}
@@ -81,6 +85,7 @@ export type FragmentSpread = {
8185
};
8286
8387
88+
type PageFragment_ref = any;
8489
import { FragmentReference } from "relay-runtime";
8590
export enum ConcreateTypes_ref {
8691
}
@@ -205,6 +210,7 @@ export type InlineFragmentConditionalID = {
205210
};
206211
207212
213+
type SomeFragment_ref = any;
208214
import { FragmentReference } from "relay-runtime";
209215
export enum InlineFragmentKitchenSink_ref {
210216
}
@@ -675,6 +681,7 @@ fragment AnotherRecursiveFragment on Image {
675681
}
676682
677683
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
684+
type PhotoFragment_ref = any;
678685
import { FragmentReference } from "relay-runtime";
679686
export enum UserProfile_ref {
680687
}

0 commit comments

Comments
 (0)