UNPKG

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