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

Commit 8e05753

Browse files
committed
feat: match upstream behavior for custom scalar type generator
1 parent 9642e3b commit 8e05753

File tree

4 files changed

+989
-472
lines changed

4 files changed

+989
-472
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@
7777
"@artsy/auto-config": "^1.0.0",
7878
"@types/graphql": "^14.2.3",
7979
"@types/invariant": "2.2.30",
80-
"@types/jest": "^22.0.1",
80+
"@types/jest": "^24.0.17",
8181
"@types/node": "8.10.51",
8282
"@types/relay-runtime": "^5.0.3",
8383
"graphql": "^14.4.2",
8484
"husky": "^3.0.2",
85-
"jest": "^23.6.0",
86-
"jest-cli": "^23.6.0",
85+
"jest": "^24.8.0",
86+
"jest-cli": "^24.8.0",
8787
"lint-staged": "^9.2.1",
8888
"prettier": "^1.18.2",
8989
"relay-compiler": "^4.0.0",

src/TypeScriptTypeTransformers.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as ts from "typescript";
2-
31
import {
42
GraphQLEnumType,
53
GraphQLInputObjectType,
@@ -12,8 +10,8 @@ import {
1210
GraphQLType,
1311
GraphQLUnionType
1412
} from "graphql";
15-
1613
import { TypeGeneratorOptions } from "relay-compiler";
14+
import * as ts from "typescript";
1715

1816
export type ScalarTypeMapping = {
1917
[type: string]: string;
@@ -75,6 +73,7 @@ function transformGraphQLScalarType(
7573
type: GraphQLScalarType,
7674
state: State
7775
): ts.TypeNode {
76+
const customType = state.customScalars[type.name];
7877
switch (state.customScalars[type.name] || type.name) {
7978
case "ID":
8079
case "String":
@@ -85,8 +84,11 @@ function transformGraphQLScalarType(
8584
return ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
8685
case "Boolean":
8786
return ts.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword);
87+
8888
default:
89-
return ts.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
89+
return customType
90+
? ts.createTypeReferenceNode(customType, undefined)
91+
: ts.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
9092
}
9193
}
9294

test/TypeScriptGenerator-test.ts

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import {generateTestsFromFixtures} from 'relay-test-utils/lib/RelayModernTestUtils'
1+
import {
2+
GraphQLCompilerContext,
3+
IRTransforms,
4+
transformASTSchema,
5+
} from 'relay-compiler'
6+
import { generateTestsFromFixtures } from 'relay-test-utils/lib/RelayModernTestUtils'
27
import * as RelayTestSchema from 'relay-test-utils/lib/RelayTestSchema'
38
import * as parseGraphQLText from 'relay-test-utils/lib/parseGraphQLText'
4-
5-
import {GraphQLCompilerContext, IRTransforms, transformASTSchema} from 'relay-compiler'
6-
79
import * as TypeScriptGenerator from '../src/TypeScriptGenerator'
810

911
function generate(text, options) {
@@ -37,17 +39,19 @@ function generate(text, options) {
3739
username: String
3840
}
3941
`,
40-
]);
41-
const {definitions} = parseGraphQLText(schema, text);
42+
])
43+
const { definitions } = parseGraphQLText(schema, text)
4244
return new GraphQLCompilerContext(RelayTestSchema, schema)
4345
.addAll(definitions)
4446
.applyTransforms(TypeScriptGenerator.transforms)
4547
.documents()
46-
.map(doc => TypeScriptGenerator.generate(doc, {
47-
...options,
48-
schema,
49-
}))
50-
.join('\n\n');
48+
.map(doc =>
49+
TypeScriptGenerator.generate(doc, {
50+
...options,
51+
schema,
52+
})
53+
)
54+
.join('\n\n')
5155
}
5256

5357
describe('TypeScriptGenerator with a single artifact directory', () => {
@@ -60,9 +64,9 @@ describe('TypeScriptGenerator with a single artifact directory', () => {
6064
relayRuntimeModule: 'relay-runtime',
6165
useHaste: false,
6266
useSingleArtifactDirectory: true,
63-
}),
64-
);
65-
});
67+
})
68+
)
69+
})
6670

6771
describe('TypeScriptGenerator without a single artifact directory', () => {
6872
generateTestsFromFixtures(`${__dirname}/fixtures/type-generator`, text =>
@@ -74,16 +78,16 @@ describe('TypeScriptGenerator without a single artifact directory', () => {
7478
relayRuntimeModule: 'relay-runtime',
7579
useHaste: false,
7680
useSingleArtifactDirectory: false,
77-
}),
78-
);
79-
});
81+
})
82+
)
83+
})
8084

8185
describe('Does not add `%future added values` when the noFutureProofEnums option is set', () => {
8286
const text = `
8387
fragment ScalarField on User {
8488
traits
8589
}
86-
`;
90+
`
8791
const types = generate(text, {
8892
customScalars: {},
8993
enumsHasteModule: null,
@@ -92,10 +96,40 @@ describe('Does not add `%future added values` when the noFutureProofEnums option
9296
relayRuntimeModule: 'relay-runtime',
9397
useHaste: false,
9498
noFutureProofEnums: true,
95-
});
99+
})
96100

97101
// Without the option, PersonalityTraits would be `"CHEERFUL" | ... | "%future added value");`
98102
expect(types).toContain(
99-
'export type PersonalityTraits = "CHEERFUL" | "DERISIVE" | "HELPFUL" | "SNARKY";',
100-
);
101-
});
103+
'export type PersonalityTraits = "CHEERFUL" | "DERISIVE" | "HELPFUL" | "SNARKY";'
104+
)
105+
})
106+
107+
describe.each`
108+
mapping | type
109+
${'String'} | ${'string'}
110+
${'Url'} | ${'string'}
111+
${'ID'} | ${'string'}
112+
${'Int'} | ${'number'}
113+
${'Color'} | ${'Color'}
114+
${'{}'} | ${'{}'}
115+
${'[]'} | ${'[]'}
116+
`('Custom scalar mapping $mapping to $type', ({ mapping, type }) => {
117+
const text = `
118+
fragment Test on User {
119+
color
120+
}
121+
`
122+
const types = generate(text, {
123+
customScalars: {
124+
Color: mapping,
125+
},
126+
enumsHasteModule: null,
127+
existingFragmentNames: new Set(['PhotoFragment']),
128+
optionalInputFields: [],
129+
relayRuntimeModule: 'relay-runtime',
130+
useHaste: false,
131+
useSingleArtifactDirectory: true,
132+
})
133+
134+
expect(types).toContain(`color: ${type} | null`)
135+
})

0 commit comments

Comments
 (0)