-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathhelpers.ts
More file actions
79 lines (72 loc) · 2.16 KB
/
helpers.ts
File metadata and controls
79 lines (72 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLInt,
GraphQLID,
GraphQLString,
GraphQLType,
isListType,
isNonNullType,
isScalarType
} from "graphql";
import * as t from "@babel/types";
import { FlowCompilerOptions } from "./language";
const builtInScalarMap = {
[GraphQLString.name]: t.stringTypeAnnotation(),
[GraphQLInt.name]: t.numberTypeAnnotation(),
[GraphQLFloat.name]: t.numberTypeAnnotation(),
[GraphQLBoolean.name]: t.booleanTypeAnnotation(),
[GraphQLID.name]: t.stringTypeAnnotation()
};
export function createTypeAnnotationFromGraphQLTypeFunction(
compilerOptions: FlowCompilerOptions
): Function {
const arrayType = compilerOptions.useReadOnlyTypes
? "$ReadOnlyArray"
: "Array";
function nonNullableTypeAnnotationFromGraphQLType(
type: GraphQLType,
typeName?: string
): t.FlowTypeAnnotation {
if (isListType(type)) {
return t.genericTypeAnnotation(
t.identifier(arrayType),
t.typeParameterInstantiation([
typeAnnotationFromGraphQLType(type.ofType, typeName)
])
);
} else if (isScalarType(type)) {
const builtIn = builtInScalarMap[typeName || type.name];
if (builtIn != null) {
return builtIn;
} else if (compilerOptions.passthroughCustomScalars) {
return t.genericTypeAnnotation(
t.identifier(
(compilerOptions.customScalarsPrefix || "") +
(typeName || type.name)
)
);
} else {
return t.anyTypeAnnotation();
}
} else if (isNonNullType(type)) {
// This won't happen; but for TypeScript completeness:
return typeAnnotationFromGraphQLType(type.ofType, typeName);
} else {
return t.genericTypeAnnotation(t.identifier(typeName || type.name));
}
}
function typeAnnotationFromGraphQLType(
type: GraphQLType,
typeName?: string
): t.FlowTypeAnnotation {
if (isNonNullType(type)) {
return nonNullableTypeAnnotationFromGraphQLType(type.ofType, typeName);
} else {
return t.nullableTypeAnnotation(
nonNullableTypeAnnotationFromGraphQLType(type, typeName)
);
}
}
return typeAnnotationFromGraphQLType;
}