UNPKG

5.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const immutable_1 = require("immutable");
4const lodash_1 = require("lodash");
5const types_1 = require("@babel/types");
6const intermediates_1 = require("./intermediates");
7const genericTypes_1 = require("./genericTypes");
8const types_2 = require("@babel/types");
9exports.typeReference = (name) => types_1.TSTypeReference(types_1.identifier(name));
10exports.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};
21exports.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};
35exports.typeForTypename = (typename) => types_1.TSUnionType(typename.possibleTypes
36 .toArray()
37 .map(type => types_1.TSLiteralType(types_1.stringLiteral(type))));
38const 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;
42const 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};
60const propertySignatureForField = (field) => types_1.TSPropertySignature(types_1.identifier(field.name), types_1.TSTypeAnnotation(typeForOutputType(field.type)));
61const typeForFragmentReference = (type) => exports.typeReference(type.name);
62const typeForAnyObject = (object) => object.kind == "FragmentReference"
63 ? typeForFragmentReference(object)
64 : typeForInlineSelection(object);
65const emptyType = types_1.TSTypeLiteral([]);
66exports.remainingPossibleTypes = (typeConditions, possibleTypes) => possibleTypes.subtract(typeConditions.reduce((possibleTypes, type) => possibleTypes.union(type.possibleTypes), immutable_1.Set()));
67const 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};
73const 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));
87const typeForSelectionSet = (selectionSet) => typeForInlineSelection(intermediates_1.InlineSelection(selectionSet));
88const enumMemberForGraphQLEnumValue = (value) => types_1.TSEnumMember(types_1.identifier(value.name), types_1.stringLiteral(value.name));
89exports.enumDeclarationForGraphQLEnumType = (type) => types_1.TSEnumDeclaration(types_1.identifier(type.name), type.getValues().map(enumMemberForGraphQLEnumValue));
90const 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};
96const typeForGraphQLInputFieldMap = (map) => types_1.TSTypeLiteral(Object.values(map).map(propertySignatureForGraphQLInputField));
97exports.typeAliasDeclarationForGraphQLInputObjectType = (type) => types_1.TSTypeAliasDeclaration(types_1.identifier(type.name), undefined, typeForGraphQLInputFieldMap(type.getFields()));
98exports.typeAliasDeclarationForFragment = (fragment) => types_1.TSTypeAliasDeclaration(types_1.identifier(fragment.fragmentName), undefined, typeForSelectionSet(fragment.selectionSet));
99exports.typeAliasDeclarationForOperation = (operation) => types_1.TSTypeAliasDeclaration(types_1.identifier(operation.operationName), undefined, typeForSelectionSet(operation.selectionSet));
100exports.exportDeclaration = (expression) => types_1.exportNamedDeclaration(expression, []);
101exports.stringIdentifier = (fragmentOrOperationName) => types_1.identifier(lodash_1.camelCase(fragmentOrOperationName + "String"));
102//# sourceMappingURL=types.js.map
\No newline at end of file