1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const immutable_1 = require("immutable");
|
4 | const lodash_1 = require("lodash");
|
5 | const types_1 = require("@babel/types");
|
6 | const intermediates_1 = require("./intermediates");
|
7 | const genericTypes_1 = require("./genericTypes");
|
8 | const types_2 = require("@babel/types");
|
9 | exports.typeReference = (name) => types_1.TSTypeReference(types_1.identifier(name));
|
10 | exports.typeForScalar = (scalar) => {
|
11 | switch (scalar.name) {
|
12 | case "Int":
|
13 | case "Float":
|
14 | return types_1.TSNumberKeyword();
|
15 | case "Boolean":
|
16 | return types_1.TSBooleanKeyword();
|
17 | default:
|
18 | return types_1.TSStringKeyword();
|
19 | }
|
20 | };
|
21 | exports.typeForInputType = (type) => {
|
22 | switch (type.kind) {
|
23 | case "Maybe":
|
24 | return genericTypes_1.OptionalType(exports.typeForInputType(type.ofType));
|
25 | case "List":
|
26 | return types_1.TSArrayType(exports.typeForInputType(type.ofType));
|
27 | case "InputObject":
|
28 | return exports.typeReference(type.name);
|
29 | case "Enum":
|
30 | return exports.typeReference(type.name);
|
31 | case "Scalar":
|
32 | return exports.typeForScalar(type);
|
33 | }
|
34 | };
|
35 | exports.typeForTypename = (typename) => types_1.TSUnionType(typename.possibleTypes
|
36 | .toArray()
|
37 | .map(type => types_1.TSLiteralType(types_1.stringLiteral(type))));
|
38 | const parenthesizedTypeIfNecessary = (type) => (types_1.isTSIntersectionType(type) && type.types.length > 1) ||
|
39 | (types_2.isTSUnionType(type) && type.types.length > 1)
|
40 | ? types_1.TSParenthesizedType(type)
|
41 | : type;
|
42 | const typeForOutputType = (type) => {
|
43 | switch (type.kind) {
|
44 | case "Maybe":
|
45 | return genericTypes_1.MaybeType(typeForOutputType(type.ofType));
|
46 | case "List":
|
47 | return types_1.TSArrayType(parenthesizedTypeIfNecessary(typeForOutputType(type.ofType)));
|
48 | case "FragmentReference":
|
49 | return exports.typeReference(type.name);
|
50 | case "InlineSelection":
|
51 | return typeForInlineSelection(type);
|
52 | case "Enum":
|
53 | return exports.typeReference(type.name);
|
54 | case "Scalar":
|
55 | return exports.typeForScalar(type);
|
56 | case "Typename":
|
57 | return exports.typeForTypename(type);
|
58 | }
|
59 | };
|
60 | const propertySignatureForField = (field) => types_1.TSPropertySignature(types_1.identifier(field.name), types_1.TSTypeAnnotation(typeForOutputType(field.type)));
|
61 | const typeForFragmentReference = (type) => exports.typeReference(type.name);
|
62 | const typeForAnyObject = (object) => object.kind == "FragmentReference"
|
63 | ? typeForFragmentReference(object)
|
64 | : typeForInlineSelection(object);
|
65 | const emptyType = types_1.TSTypeLiteral([]);
|
66 | exports.remainingPossibleTypes = (typeConditions, possibleTypes) => possibleTypes.subtract(typeConditions.reduce((possibleTypes, type) => possibleTypes.union(type.possibleTypes), immutable_1.Set()));
|
67 | const unionTypeForTypeConditions = (typeConditions, possibleTypes) => {
|
68 | const remainingTypes = exports.remainingPossibleTypes(typeConditions, possibleTypes);
|
69 | return types_1.TSUnionType(typeConditions
|
70 | .map(type => genericTypes_1.IfType(type.possibleTypes, typeForAnyObject(type)))
|
71 | .concat(remainingTypes.size > 0 ? [genericTypes_1.IfType(remainingTypes, emptyType)] : []));
|
72 | };
|
73 | const typeForInlineSelection = (type) => types_1.TSIntersectionType([
|
74 | ...type.intersections.map(typeForFragmentReference),
|
75 | type.fields.length > 0
|
76 | ? types_1.TSTypeLiteral(type.fields.map(propertySignatureForField))
|
77 | : undefined,
|
78 | type.booleanConditions.length > 0
|
79 | ? genericTypes_1.PartialType(types_1.TSIntersectionType(type.booleanConditions.map(typeForAnyObject)))
|
80 | : undefined,
|
81 | type.typeConditions.length > 0
|
82 | ? types_1.TSParenthesizedType(unionTypeForTypeConditions(type.typeConditions, type.possibleTypes))
|
83 | : undefined
|
84 | ]
|
85 | .filter(x => x)
|
86 | .map(x => x));
|
87 | const typeForSelectionSet = (selectionSet) => typeForInlineSelection(intermediates_1.InlineSelection(selectionSet));
|
88 | const enumMemberForGraphQLEnumValue = (value) => types_1.TSEnumMember(types_1.identifier(value.name), types_1.stringLiteral(value.name));
|
89 | exports.enumDeclarationForGraphQLEnumType = (type) => types_1.TSEnumDeclaration(types_1.identifier(type.name), type.getValues().map(enumMemberForGraphQLEnumValue));
|
90 | const propertySignatureForGraphQLInputField = (field) => {
|
91 | const inputType = intermediates_1.InputType(field.type);
|
92 | return Object.assign({}, types_1.TSPropertySignature(types_1.identifier(field.name), types_1.TSTypeAnnotation(inputType.kind == "Maybe"
|
93 | ? genericTypes_1.MaybeType(exports.typeForInputType(inputType.ofType))
|
94 | : exports.typeForInputType(inputType))), { optional: intermediates_1.InputType(field.type).kind == "Maybe" });
|
95 | };
|
96 | const typeForGraphQLInputFieldMap = (map) => types_1.TSTypeLiteral(Object.values(map).map(propertySignatureForGraphQLInputField));
|
97 | exports.typeAliasDeclarationForGraphQLInputObjectType = (type) => types_1.TSTypeAliasDeclaration(types_1.identifier(type.name), undefined, typeForGraphQLInputFieldMap(type.getFields()));
|
98 | exports.typeAliasDeclarationForFragment = (fragment) => types_1.TSTypeAliasDeclaration(types_1.identifier(fragment.fragmentName), undefined, typeForSelectionSet(fragment.selectionSet));
|
99 | exports.typeAliasDeclarationForOperation = (operation) => types_1.TSTypeAliasDeclaration(types_1.identifier(operation.operationName), undefined, typeForSelectionSet(operation.selectionSet));
|
100 | exports.exportDeclaration = (expression) => types_1.exportNamedDeclaration(expression, []);
|
101 | exports.stringIdentifier = (fragmentOrOperationName) => types_1.identifier(lodash_1.camelCase(fragmentOrOperationName + "String"));
|
102 |
|
\ | No newline at end of file |