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

Commit c0feeae

Browse files
committed
some tweaks on Node visitor and started with refetchable fragments
1 parent b009100 commit c0feeae

File tree

2 files changed

+61
-363
lines changed

2 files changed

+61
-363
lines changed

src/TypeScriptGenerator.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Fragment,
55
IRVisitor,
66
LinkedField,
7+
Metadata,
78
Root,
89
ScalarField,
910
Schema,
@@ -363,8 +364,8 @@ function createVisitor(
363364
customScalars: options.customScalars,
364365
enumsHasteModule: options.enumsHasteModule,
365366
existingFragmentNames: options.existingFragmentNames,
366-
generatedInputObjectTypes: {},
367367
generatedFragments: new Set(),
368+
generatedInputObjectTypes: {},
368369
hasConnectionResolver: false,
369370
optionalInputFields: options.optionalInputFields,
370371
usedEnums: {},
@@ -384,9 +385,7 @@ function createVisitor(
384385
node,
385386
state
386387
);
387-
388388
const inputObjectTypes = generateInputObjectTypes(state);
389-
390389
const responseType = exportType(
391390
`${node.name}Response`,
392391
selectionsToAST(
@@ -411,9 +410,7 @@ function createVisitor(
411410

412411
// Generate raw response type
413412
let rawResponseType;
414-
415413
const { normalizationIR } = options;
416-
417414
if (
418415
normalizationIR &&
419416
node.directives.some(d => d.name === DIRECTIVE_NAME)
@@ -423,14 +420,32 @@ function createVisitor(
423420
createRawResponseTypeVisitor(schema, state)
424421
);
425422
}
426-
427-
const nodes = [
423+
const refetchableFragmentName = getRefetchableQueryParentFragmentName(
424+
state,
425+
node.metadata
426+
);
427+
if (state.hasConnectionResolver) {
428+
state.runtimeImports.add("ConnectionReference");
429+
}
430+
if (refetchableFragmentName !== null) {
431+
state.runtimeImports.add("FragmentReference");
432+
}
433+
const nodes = [];
434+
if (state.runtimeImports.size) {
435+
nodes.push(
436+
importTypes(
437+
Array.from(state.runtimeImports).sort(),
438+
"relay-runtime"
439+
)
440+
);
441+
}
442+
nodes.push(
428443
...getFragmentRefsTypeImport(state),
429444
...getEnumDefinitions(schema, state),
430445
...inputObjectTypes,
431446
inputVariablesType,
432447
responseType
433-
];
448+
);
434449

435450
if (rawResponseType) {
436451
for (const [key, ast] of state.matchFields) {
@@ -1065,6 +1080,25 @@ function getEnumDefinitions(
10651080
});
10661081
}
10671082

1083+
// If it's a @refetchable fragment, we generate the $fragmentRef in generated
1084+
// query, and import it in the fragment to avoid circular dependencies
1085+
function getRefetchableQueryParentFragmentName(
1086+
state: State,
1087+
metadata: Metadata
1088+
): string | null | undefined {
1089+
if (
1090+
(metadata && !metadata.isRefetchableQuery) ||
1091+
(!state.useHaste && !state.useSingleArtifactDirectory)
1092+
) {
1093+
return null;
1094+
}
1095+
const derivedFrom = metadata && metadata.derivedFrom;
1096+
if (derivedFrom !== null && typeof derivedFrom === "string") {
1097+
return derivedFrom;
1098+
}
1099+
return null;
1100+
}
1101+
10681102
function stringLiteralTypeAnnotation(name: string): ts.TypeNode {
10691103
return ts.createLiteralTypeNode(ts.createLiteral(name));
10701104
}

0 commit comments

Comments
 (0)