UNPKG

2.45 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const normalization_1 = require("./normalization");
4const types_1 = require("@babel/types");
5const genericTypes_1 = require("./genericTypes");
6const types_2 = require("./types");
7const constructors_1 = require("./constructors");
8const typeForExtendedFields = (fields) => types_1.TSTypeLiteral([...fields.map(propertySignatureForExtendedField).values()]);
9const typeForExtendedSelection = (selection) => types_1.TSUnionType([...selection.fields.map(typeForExtendedFields).values()]);
10const typeForExtendedFieldType = (type) => {
11 switch (type.kind) {
12 case "InlineSelection":
13 return typeForExtendedSelection(type);
14 case "List":
15 return types_1.TSArrayType(typeForExtendedFieldType(type.ofType));
16 case "Maybe":
17 return genericTypes_1.MaybeType(typeForExtendedFieldType(type.ofType));
18 case "Enum":
19 return types_2.typeReference(type.name);
20 case "Scalar":
21 return types_2.typeForScalar(type);
22 case "Typename":
23 return types_2.typeForTypename(type);
24 }
25};
26const propertySignatureForExtendedField = (field, fieldName) => {
27 return Object.assign({}, types_1.TSPropertySignature(types_1.identifier(fieldName), types_1.TSTypeAnnotation(typeForExtendedFieldType(field.type))), { optional: field.optional });
28};
29const normalizedDeclaration = (fields, __typename) => types_1.TSTypeAliasDeclaration(types_1.identifier(`Normalized${__typename}`), undefined, typeForExtendedFields(fields));
30exports.normalizedDataDeclarations = (context) => {
31 const types = normalization_1.normalizedTypes(context);
32 return [
33 ...types.map(normalizedDeclaration).values(),
34 types_1.TSTypeAliasDeclaration(types_1.identifier("NormalizedData"), undefined, types_1.TSTypeLiteral(types
35 .keySeq()
36 .map(__typename => types_1.TSPropertySignature(types_1.identifier(__typename), types_1.TSTypeAnnotation(genericTypes_1.ByIdType(types_2.typeReference(`Normalized${__typename}`)))))
37 .toArray())),
38 constructors_1.constructorDeclaration("NormalizedData", [], types_2.typeReference("NormalizedData"), types
39 .keySeq()
40 .map(__typename => types_1.objectProperty(types_1.identifier(__typename), types_1.objectExpression([])))
41 .toArray())
42 ];
43};
44//# sourceMappingURL=normalizedDataDeclaration.js.map
\No newline at end of file