1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const normalization_1 = require("./normalization");
|
4 | const types_1 = require("@babel/types");
|
5 | const genericTypes_1 = require("./genericTypes");
|
6 | const types_2 = require("./types");
|
7 | const constructors_1 = require("./constructors");
|
8 | const typeForExtendedFields = (fields) => types_1.TSTypeLiteral([...fields.map(propertySignatureForExtendedField).values()]);
|
9 | const typeForExtendedSelection = (selection) => types_1.TSUnionType([...selection.fields.map(typeForExtendedFields).values()]);
|
10 | const 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 | };
|
26 | const propertySignatureForExtendedField = (field, fieldName) => {
|
27 | return Object.assign({}, types_1.TSPropertySignature(types_1.identifier(fieldName), types_1.TSTypeAnnotation(typeForExtendedFieldType(field.type))), { optional: field.optional });
|
28 | };
|
29 | const normalizedDeclaration = (fields, __typename) => types_1.TSTypeAliasDeclaration(types_1.identifier(`Normalized${__typename}`), undefined, typeForExtendedFields(fields));
|
30 | exports.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 |
|
\ | No newline at end of file |