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

Commit

Permalink
[plugin] Type fragment refs as any when not using a single artifact dir.
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
alloy committed Aug 25, 2018
1 parent 97e8d18 commit 9ebdda2
Show file tree
Hide file tree
Showing 3 changed files with 814 additions and 34 deletions.
50 changes: 30 additions & 20 deletions src/TypeScriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,26 +353,37 @@ function createVisitor(options: TypeGeneratorOptions) {
});
state.generatedFragments.add(node.name);
const refTypeName = getRefTypeName(node.name);
const _refTypeName = `_${refTypeName}`;
const _refType = ts.createVariableStatement(
[ts.createToken(ts.SyntaxKind.DeclareKeyword)],
ts.createVariableDeclarationList(
[
ts.createVariableDeclaration(
_refTypeName,
ts.createTypeOperatorNode(
ts.SyntaxKind.UniqueKeyword,
ts.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword)
const refTypeNodes: ts.Node[] = []
if (options.useSingleArtifactDirectory) {
const _refTypeName = `_${refTypeName}`;
const _refType = ts.createVariableStatement(
[ts.createToken(ts.SyntaxKind.DeclareKeyword)],
ts.createVariableDeclarationList(
[
ts.createVariableDeclaration(
_refTypeName,
ts.createTypeOperatorNode(
ts.SyntaxKind.UniqueKeyword,
ts.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword)
)
)
)
],
ts.NodeFlags.Const
],
ts.NodeFlags.Const
)
);
const refType = exportType(
refTypeName,
ts.createTypeQueryNode(ts.createIdentifier(_refTypeName))
);
refTypeNodes.push(_refType)
refTypeNodes.push(refType)
} else {
const refType = exportType(
refTypeName,
ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)
)
);
const refType = exportType(
refTypeName,
ts.createTypeQueryNode(ts.createIdentifier(_refTypeName))
);
refTypeNodes.push(refType)
}
const baseType = selectionsToAST(selections, state, refTypeName);
const type = isPlural(node)
? ts.createTypeReferenceNode(ts.createIdentifier("ReadonlyArray"), [
Expand All @@ -382,8 +393,7 @@ function createVisitor(options: TypeGeneratorOptions) {
return [
...getFragmentImports(state),
...getEnumDefinitions(state),
_refType,
refType,
...refTypeNodes,
exportType(node.name, type)
];
},
Expand Down
27 changes: 26 additions & 1 deletion test/TypeScriptGenerator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,32 @@ import {GraphQLCompilerContext, IRTransforms, transformASTSchema} from 'relay-co

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

describe('TypeScriptGenerator', () => {
describe('TypeScriptGenerator with a single artifact directory', () => {
generateTestsFromFixtures(`${__dirname}/fixtures/type-generator`, text => {
const schema = transformASTSchema(RelayTestSchema, [
IRTransforms.schemaExtensions[1], // RelayRelayDirectiveTransform.SCHEMA_EXTENSION,
]);
const {definitions} = parseGraphQLText(schema, text);
return new GraphQLCompilerContext(RelayTestSchema, schema)
.addAll(definitions)
.applyTransforms(TypeScriptGenerator.transforms)
.documents()
.map(doc =>
TypeScriptGenerator.generate(doc, {
customScalars: {},
enumsHasteModule: null,
existingFragmentNames: new Set(['PhotoFragment']),
inputFieldWhiteList: [],
relayRuntimeModule: 'relay-runtime',
useHaste: false,
useSingleArtifactDirectory: true,
}),
)
.join('\n\n');
});
});

describe('TypeScriptGenerator without a single artifact directory', () => {
generateTestsFromFixtures(`${__dirname}/fixtures/type-generator`, text => {
const schema = transformASTSchema(RelayTestSchema, [
IRTransforms.schemaExtensions[1], // RelayRelayDirectiveTransform.SCHEMA_EXTENSION,
Expand Down
Loading

0 comments on commit 9ebdda2

Please sign in to comment.