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

Commit 6006bb1

Browse files
authored
Merge pull request #61 from relay-tools/fix-typings-when-not-using-single-artefact-dir
[plugin] Type fragment refs as any when not using a single artifact dir.
2 parents 97e8d18 + 9ebdda2 commit 6006bb1

File tree

3 files changed

+814
-34
lines changed

3 files changed

+814
-34
lines changed

src/TypeScriptGenerator.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -353,26 +353,37 @@ function createVisitor(options: TypeGeneratorOptions) {
353353
});
354354
state.generatedFragments.add(node.name);
355355
const refTypeName = getRefTypeName(node.name);
356-
const _refTypeName = `_${refTypeName}`;
357-
const _refType = ts.createVariableStatement(
358-
[ts.createToken(ts.SyntaxKind.DeclareKeyword)],
359-
ts.createVariableDeclarationList(
360-
[
361-
ts.createVariableDeclaration(
362-
_refTypeName,
363-
ts.createTypeOperatorNode(
364-
ts.SyntaxKind.UniqueKeyword,
365-
ts.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword)
356+
const refTypeNodes: ts.Node[] = []
357+
if (options.useSingleArtifactDirectory) {
358+
const _refTypeName = `_${refTypeName}`;
359+
const _refType = ts.createVariableStatement(
360+
[ts.createToken(ts.SyntaxKind.DeclareKeyword)],
361+
ts.createVariableDeclarationList(
362+
[
363+
ts.createVariableDeclaration(
364+
_refTypeName,
365+
ts.createTypeOperatorNode(
366+
ts.SyntaxKind.UniqueKeyword,
367+
ts.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword)
368+
)
366369
)
367-
)
368-
],
369-
ts.NodeFlags.Const
370+
],
371+
ts.NodeFlags.Const
372+
)
373+
);
374+
const refType = exportType(
375+
refTypeName,
376+
ts.createTypeQueryNode(ts.createIdentifier(_refTypeName))
377+
);
378+
refTypeNodes.push(_refType)
379+
refTypeNodes.push(refType)
380+
} else {
381+
const refType = exportType(
382+
refTypeName,
383+
ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)
370384
)
371-
);
372-
const refType = exportType(
373-
refTypeName,
374-
ts.createTypeQueryNode(ts.createIdentifier(_refTypeName))
375-
);
385+
refTypeNodes.push(refType)
386+
}
376387
const baseType = selectionsToAST(selections, state, refTypeName);
377388
const type = isPlural(node)
378389
? ts.createTypeReferenceNode(ts.createIdentifier("ReadonlyArray"), [
@@ -382,8 +393,7 @@ function createVisitor(options: TypeGeneratorOptions) {
382393
return [
383394
...getFragmentImports(state),
384395
...getEnumDefinitions(state),
385-
_refType,
386-
refType,
396+
...refTypeNodes,
387397
exportType(node.name, type)
388398
];
389399
},

test/TypeScriptGenerator-test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,32 @@ import {GraphQLCompilerContext, IRTransforms, transformASTSchema} from 'relay-co
66

77
import * as TypeScriptGenerator from '../src/TypeScriptGenerator'
88

9-
describe('TypeScriptGenerator', () => {
9+
describe('TypeScriptGenerator with a single artifact directory', () => {
10+
generateTestsFromFixtures(`${__dirname}/fixtures/type-generator`, text => {
11+
const schema = transformASTSchema(RelayTestSchema, [
12+
IRTransforms.schemaExtensions[1], // RelayRelayDirectiveTransform.SCHEMA_EXTENSION,
13+
]);
14+
const {definitions} = parseGraphQLText(schema, text);
15+
return new GraphQLCompilerContext(RelayTestSchema, schema)
16+
.addAll(definitions)
17+
.applyTransforms(TypeScriptGenerator.transforms)
18+
.documents()
19+
.map(doc =>
20+
TypeScriptGenerator.generate(doc, {
21+
customScalars: {},
22+
enumsHasteModule: null,
23+
existingFragmentNames: new Set(['PhotoFragment']),
24+
inputFieldWhiteList: [],
25+
relayRuntimeModule: 'relay-runtime',
26+
useHaste: false,
27+
useSingleArtifactDirectory: true,
28+
}),
29+
)
30+
.join('\n\n');
31+
});
32+
});
33+
34+
describe('TypeScriptGenerator without a single artifact directory', () => {
1035
generateTestsFromFixtures(`${__dirname}/fixtures/type-generator`, text => {
1136
const schema = transformASTSchema(RelayTestSchema, [
1237
IRTransforms.schemaExtensions[1], // RelayRelayDirectiveTransform.SCHEMA_EXTENSION,

0 commit comments

Comments
 (0)