1 | import { normalizedTypes } from "./normalization";
|
2 | import { CompilerContext } from "apollo-codegen-core/lib/compiler";
|
3 | import {
|
4 | identifier,
|
5 | TSTypeAliasDeclaration,
|
6 | TSPropertySignature,
|
7 | TSTypeAnnotation,
|
8 | TSTypeLiteral,
|
9 | TSType,
|
10 | TSArrayType,
|
11 | TSUnionType,
|
12 | objectProperty,
|
13 | objectExpression
|
14 | } from "@babel/types";
|
15 | import {
|
16 | ExtendedFields,
|
17 | ExtendedField,
|
18 | ExtendedFieldType,
|
19 | ExtendedSelection
|
20 | } from "./extendedIntermediates";
|
21 | import { MaybeType, ByIdType } from "./genericTypes";
|
22 | import { typeReference, typeForScalar, typeForTypename } from "./types";
|
23 | import { constructorDeclaration } from "./constructors";
|
24 |
|
25 | const typeForExtendedFields = (fields: ExtendedFields): TSType =>
|
26 | TSTypeLiteral([...fields.map(propertySignatureForExtendedField).values()]);
|
27 |
|
28 | const typeForExtendedSelection = (selection: ExtendedSelection): TSType =>
|
29 | TSUnionType([...selection.fields.map(typeForExtendedFields).values()]);
|
30 |
|
31 | const typeForExtendedFieldType = (type: ExtendedFieldType): TSType => {
|
32 | switch (type.kind) {
|
33 | case "InlineSelection":
|
34 | return typeForExtendedSelection(type);
|
35 | case "List":
|
36 | return TSArrayType(typeForExtendedFieldType(type.ofType));
|
37 | case "Maybe":
|
38 | return MaybeType(typeForExtendedFieldType(type.ofType));
|
39 | case "Enum":
|
40 | return typeReference(type.name);
|
41 | case "Scalar":
|
42 | return typeForScalar(type);
|
43 | case "Typename":
|
44 | return typeForTypename(type);
|
45 | }
|
46 | };
|
47 |
|
48 | const propertySignatureForExtendedField = (
|
49 | field: ExtendedField,
|
50 | fieldName: string
|
51 | ): TSPropertySignature => {
|
52 | return {
|
53 | ...TSPropertySignature(
|
54 | identifier(fieldName),
|
55 | TSTypeAnnotation(typeForExtendedFieldType(field.type))
|
56 | ),
|
57 | optional: field.optional
|
58 | };
|
59 | };
|
60 |
|
61 | const normalizedDeclaration = (fields: ExtendedFields, __typename: String) =>
|
62 | TSTypeAliasDeclaration(
|
63 | identifier(`Normalized${__typename}`),
|
64 | undefined,
|
65 | typeForExtendedFields(fields)
|
66 | );
|
67 |
|
68 | export const normalizedDataDeclarations = (context: CompilerContext) => {
|
69 | const types = normalizedTypes(context);
|
70 | return [
|
71 | ...types.map(normalizedDeclaration).values(),
|
72 | TSTypeAliasDeclaration(
|
73 | identifier("NormalizedData"),
|
74 | undefined,
|
75 | TSTypeLiteral(
|
76 | types
|
77 | .keySeq()
|
78 | .map(__typename =>
|
79 | TSPropertySignature(
|
80 | identifier(__typename),
|
81 | TSTypeAnnotation(
|
82 | ByIdType(typeReference(`Normalized${__typename}`))
|
83 | )
|
84 | )
|
85 | )
|
86 | .toArray()
|
87 | )
|
88 | ),
|
89 | constructorDeclaration(
|
90 | "NormalizedData",
|
91 | [],
|
92 | typeReference("NormalizedData"),
|
93 | types
|
94 | .keySeq()
|
95 | .map(__typename =>
|
96 | objectProperty(identifier(__typename), objectExpression([]))
|
97 | )
|
98 | .toArray()
|
99 | )
|
100 | ];
|
101 | };
|