UNPKG

3.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const printing_1 = require("apollo-codegen-core/lib/utilities/printing");
4const graphql_1 = require("apollo-codegen-core/lib/utilities/graphql");
5const helpers_1 = require("./helpers");
6const t = require("@babel/types");
7class TypescriptGenerator {
8 constructor(compilerOptions) {
9 this.options = compilerOptions;
10 this.typeFromGraphQLType = helpers_1.createTypeFromGraphQLTypeFunction(compilerOptions);
11 }
12 enumerationDeclaration(type) {
13 const { name, description } = type;
14 const enumMembers = graphql_1.sortEnumValues(type.getValues()).map(({ value }) => {
15 return t.TSEnumMember(t.identifier(value), t.stringLiteral(value));
16 });
17 const typeAlias = t.exportNamedDeclaration(t.TSEnumDeclaration(t.identifier(name), enumMembers), []);
18 if (description) {
19 typeAlias.leadingComments = [{
20 type: 'CommentBlock',
21 value: printing_1.commentBlockContent(description)
22 }];
23 }
24 return typeAlias;
25 }
26 inputObjectDeclaration(inputObjectType) {
27 const { name, description } = inputObjectType;
28 const fieldMap = inputObjectType.getFields();
29 const fields = Object.keys(inputObjectType.getFields())
30 .map((fieldName) => {
31 const field = fieldMap[fieldName];
32 return {
33 name: fieldName,
34 type: this.typeFromGraphQLType(field.type)
35 };
36 });
37 const inputType = t.exportNamedDeclaration(this.interface(name, fields, {
38 keyInheritsNullability: true
39 }), []);
40 if (description) {
41 inputType.leadingComments = [{
42 type: 'CommentBlock',
43 value: printing_1.commentBlockContent(description)
44 }];
45 }
46 return inputType;
47 }
48 typesForProperties(fields, { keyInheritsNullability = false } = {}) {
49 return fields.map(({ name, description, type }) => {
50 const propertySignatureType = t.TSPropertySignature(t.identifier(name), t.TSTypeAnnotation(type));
51 propertySignatureType.optional = keyInheritsNullability && this.isNullableType(type);
52 if (description) {
53 propertySignatureType.leadingComments = [{
54 type: 'CommentBlock',
55 value: printing_1.commentBlockContent(description)
56 }];
57 }
58 return propertySignatureType;
59 });
60 }
61 interface(name, fields, { keyInheritsNullability = false } = {}) {
62 return t.TSInterfaceDeclaration(t.identifier(name), undefined, undefined, t.TSInterfaceBody(this.typesForProperties(fields, {
63 keyInheritsNullability
64 })));
65 }
66 typeAliasGenericUnion(name, members) {
67 return t.TSTypeAliasDeclaration(t.identifier(name), undefined, t.TSUnionType(members));
68 }
69 exportDeclaration(declaration) {
70 return t.exportNamedDeclaration(declaration, []);
71 }
72 nameFromScopeStack(scope) {
73 return scope.join('_');
74 }
75 makeNullableType(type) {
76 return t.TSUnionType([
77 type,
78 t.TSNullKeyword()
79 ]);
80 }
81 isNullableType(type) {
82 return t.isTSUnionType(type) && type.types.some(type => t.isTSNullKeyword(type));
83 }
84 import(types, source) {
85 return t.importDeclaration(types.map((type) => t.importSpecifier(t.identifier(type.toString()), t.identifier(type.toString()))), t.stringLiteral(source));
86 }
87}
88exports.default = TypescriptGenerator;
89//# sourceMappingURL=language.js.map
\No newline at end of file