1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
5 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6 | desc = { enumerable: true, get: function() { return m[k]; } };
|
7 | }
|
8 | Object.defineProperty(o, k2, desc);
|
9 | }) : (function(o, m, k, k2) {
|
10 | if (k2 === undefined) k2 = k;
|
11 | o[k2] = m[k];
|
12 | }));
|
13 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15 | }) : function(o, v) {
|
16 | o["default"] = v;
|
17 | });
|
18 | var __importStar = (this && this.__importStar) || function (mod) {
|
19 | if (mod && mod.__esModule) return mod;
|
20 | var result = {};
|
21 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22 | __setModuleDefault(result, mod);
|
23 | return result;
|
24 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | exports.Helpers = void 0;
|
27 | const graphql_1 = require("graphql");
|
28 | const change_case_1 = require("change-case");
|
29 | const Inflector = __importStar(require("inflected"));
|
30 | const printing_1 = require("apollo-codegen-core/lib/utilities/printing");
|
31 | const language_1 = require("./language");
|
32 | const graphql_2 = require("apollo-codegen-core/lib/utilities/graphql");
|
33 | const collectAndMergeFields_1 = require("apollo-codegen-core/lib/compiler/visitors/collectAndMergeFields");
|
34 | const builtInScalarMap = {
|
35 | [graphql_1.GraphQLString.name]: "String",
|
36 | [graphql_1.GraphQLInt.name]: "Int",
|
37 | [graphql_1.GraphQLFloat.name]: "Double",
|
38 | [graphql_1.GraphQLBoolean.name]: "Bool",
|
39 | [graphql_1.GraphQLID.name]: "GraphQLID",
|
40 | };
|
41 | class Helpers {
|
42 | constructor(options) {
|
43 | this.options = options;
|
44 | }
|
45 | typeNameFromGraphQLType(type, unmodifiedTypeName, isOptional) {
|
46 | if ((0, graphql_1.isNonNullType)(type)) {
|
47 | return this.typeNameFromGraphQLType(type.ofType, unmodifiedTypeName, false);
|
48 | }
|
49 | else if (isOptional === undefined) {
|
50 | isOptional = true;
|
51 | }
|
52 | let typeName;
|
53 | if ((0, graphql_1.isListType)(type)) {
|
54 | typeName =
|
55 | "[" +
|
56 | this.typeNameFromGraphQLType(type.ofType, unmodifiedTypeName) +
|
57 | "]";
|
58 | }
|
59 | else if ((0, graphql_1.isScalarType)(type)) {
|
60 | typeName = this.typeNameForScalarType(type);
|
61 | }
|
62 | else {
|
63 | typeName = unmodifiedTypeName || type.name;
|
64 | }
|
65 | return isOptional ? typeName + "?" : typeName;
|
66 | }
|
67 | typeNameForScalarType(type) {
|
68 | return (builtInScalarMap[type.name] ||
|
69 | (this.options.passthroughCustomScalars
|
70 | ? this.options.customScalarsPrefix + type.name
|
71 | : graphql_1.GraphQLString.name));
|
72 | }
|
73 | fieldTypeEnum(type, structName) {
|
74 | if ((0, graphql_1.isNonNullType)(type)) {
|
75 | return (0, language_1.swift) `.nonNull(${this.fieldTypeEnum(type.ofType, structName)})`;
|
76 | }
|
77 | else if ((0, graphql_1.isListType)(type)) {
|
78 | return (0, language_1.swift) `.list(${this.fieldTypeEnum(type.ofType, structName)})`;
|
79 | }
|
80 | else if ((0, graphql_1.isScalarType)(type)) {
|
81 | return (0, language_1.swift) `.scalar(${this.typeNameForScalarType(type)}.self)`;
|
82 | }
|
83 | else if ((0, graphql_1.isEnumType)(type)) {
|
84 | return (0, language_1.swift) `.scalar(${type.name}.self)`;
|
85 | }
|
86 | else if ((0, graphql_1.isCompositeType)(type)) {
|
87 | return (0, language_1.swift) `.object(${structName}.selections)`;
|
88 | }
|
89 | else {
|
90 | throw new Error(`Unknown field type: ${type}`);
|
91 | }
|
92 | }
|
93 | enumCaseName(name) {
|
94 | return camelCase(name);
|
95 | }
|
96 | enumDotCaseName(name) {
|
97 | return (0, language_1.swift) `.${language_1.SwiftSource.memberName(camelCase(name))}`;
|
98 | }
|
99 | operationClassName(name) {
|
100 | return pascalCase(name);
|
101 | }
|
102 | structNameForPropertyName(propertyName) {
|
103 | return pascalCase(Inflector.singularize(propertyName));
|
104 | }
|
105 | structNameForFragmentName(fragmentName) {
|
106 | return pascalCase(fragmentName);
|
107 | }
|
108 | structNameForVariant(variant) {
|
109 | return ("As" +
|
110 | variant.possibleTypes.map((type) => pascalCase(type.name)).join("Or"));
|
111 | }
|
112 | internalParameterName(propertyName, properties) {
|
113 | return language_1.SwiftSource.isValidParameterName(propertyName)
|
114 | ? propertyName
|
115 | : makeUniqueName(`_${propertyName}`, properties);
|
116 | }
|
117 | propertyFromField(field, namespace) {
|
118 | const { responseKey, isConditional } = field;
|
119 | const propertyName = (0, graphql_2.isMetaFieldName)(responseKey)
|
120 | ? responseKey
|
121 | : camelCase(responseKey);
|
122 | const structName = (0, printing_1.join)([namespace, this.structNameForPropertyName(responseKey)], ".");
|
123 | let type = field.type;
|
124 | if (isConditional && (0, graphql_1.isNonNullType)(type)) {
|
125 | type = type.ofType;
|
126 | }
|
127 | const isOptional = !(0, graphql_1.isNonNullType)(type);
|
128 | const unmodifiedType = (0, graphql_1.getNamedType)(field.type);
|
129 | const unmodifiedTypeName = (0, graphql_1.isCompositeType)(unmodifiedType)
|
130 | ? structName
|
131 | : unmodifiedType.name;
|
132 | const typeName = this.typeNameFromGraphQLType(type, unmodifiedTypeName);
|
133 | return Object.assign({}, field, {
|
134 | responseKey,
|
135 | propertyName,
|
136 | typeName,
|
137 | structName,
|
138 | isOptional,
|
139 | });
|
140 | }
|
141 | propertyFromVariant(variant) {
|
142 | const structName = this.structNameForVariant(variant);
|
143 | return Object.assign(variant, {
|
144 | propertyName: camelCase(structName),
|
145 | typeName: structName + "?",
|
146 | structName,
|
147 | });
|
148 | }
|
149 | propertyFromFragmentSpread(fragmentSpread, isConditional) {
|
150 | const structName = this.structNameForFragmentName(fragmentSpread.fragmentName);
|
151 | return Object.assign({}, fragmentSpread, {
|
152 | propertyName: camelCase(fragmentSpread.fragmentName),
|
153 | typeName: isConditional ? structName + "?" : structName,
|
154 | structName,
|
155 | isConditional,
|
156 | });
|
157 | }
|
158 | propertyFromInputField(field) {
|
159 | return Object.assign({}, field, {
|
160 | propertyName: camelCase(field.name),
|
161 | typeName: this.typeNameFromGraphQLType(field.type),
|
162 | isOptional: !(0, graphql_1.isNonNullType)(field.type),
|
163 | });
|
164 | }
|
165 | propertiesForSelectionSet(selectionSet, namespace) {
|
166 | const properties = (0, collectAndMergeFields_1.collectAndMergeFields)(selectionSet, true)
|
167 | .filter((field) => field.name !== "__typename")
|
168 | .map((field) => this.propertyFromField(field, namespace));
|
169 | if (selectionSet.selections.some((selection) => selection.kind === "FragmentSpread") &&
|
170 | properties.some((property) => (0, graphql_1.isCompositeType)((0, graphql_1.getNamedType)(property.type)))) {
|
171 | return undefined;
|
172 | }
|
173 | return properties;
|
174 | }
|
175 | dictionaryLiteralForFieldArguments(args) {
|
176 | function expressionFromValue(value) {
|
177 | if (value === null) {
|
178 | return (0, language_1.swift) `nil`;
|
179 | }
|
180 | else if (value.kind === "Variable") {
|
181 | return (0, language_1.swift) `GraphQLVariable(${language_1.SwiftSource.string(value.variableName)})`;
|
182 | }
|
183 | else if (Array.isArray(value)) {
|
184 | return (language_1.SwiftSource.wrap((0, language_1.swift) `[`, language_1.SwiftSource.join(value.map(expressionFromValue), ", "), (0, language_1.swift) `]`) || (0, language_1.swift) `[]`);
|
185 | }
|
186 | else if (typeof value === "object") {
|
187 | return (language_1.SwiftSource.wrap((0, language_1.swift) `[`, language_1.SwiftSource.join(Object.entries(value).map(([key, value]) => {
|
188 | return (0, language_1.swift) `${language_1.SwiftSource.string(key)}: ${expressionFromValue(value)}`;
|
189 | }), ", "), (0, language_1.swift) `]`) || (0, language_1.swift) `[:]`);
|
190 | }
|
191 | else if (typeof value === "string") {
|
192 | return language_1.SwiftSource.string(value);
|
193 | }
|
194 | else {
|
195 | return new language_1.SwiftSource(JSON.stringify(value));
|
196 | }
|
197 | }
|
198 | return (language_1.SwiftSource.wrap((0, language_1.swift) `[`, language_1.SwiftSource.join(args.map((arg) => {
|
199 | return (0, language_1.swift) `${language_1.SwiftSource.string(arg.name)}: ${expressionFromValue(arg.value)}`;
|
200 | }), ", "), (0, language_1.swift) `]`) || (0, language_1.swift) `[:]`);
|
201 | }
|
202 | mapExpressionForType(type, isConditional = false, makeExpression, expression, inputTypeName, outputTypeName) {
|
203 | let isOptional;
|
204 | if ((0, graphql_1.isNonNullType)(type)) {
|
205 | isOptional = !!isConditional;
|
206 | type = type.ofType;
|
207 | }
|
208 | else {
|
209 | isOptional = true;
|
210 | }
|
211 | if ((0, graphql_1.isListType)(type)) {
|
212 | const elementType = type.ofType;
|
213 | if (isOptional) {
|
214 | return (0, language_1.swift) `${expression}.flatMap { ${makeClosureSignature(this.typeNameFromGraphQLType(type, inputTypeName, false), this.typeNameFromGraphQLType(type, outputTypeName, false))} value.map { ${makeClosureSignature(this.typeNameFromGraphQLType(elementType, inputTypeName), this.typeNameFromGraphQLType(elementType, outputTypeName))} ${this.mapExpressionForType(elementType, undefined, makeExpression, (0, language_1.swift) `value`, inputTypeName, outputTypeName)} } }`;
|
215 | }
|
216 | else {
|
217 | return (0, language_1.swift) `${expression}.map { ${makeClosureSignature(this.typeNameFromGraphQLType(elementType, inputTypeName), this.typeNameFromGraphQLType(elementType, outputTypeName))} ${this.mapExpressionForType(elementType, undefined, makeExpression, (0, language_1.swift) `value`, inputTypeName, outputTypeName)} }`;
|
218 | }
|
219 | }
|
220 | else if (isOptional) {
|
221 | return (0, language_1.swift) `${expression}.flatMap { ${makeClosureSignature(this.typeNameFromGraphQLType(type, inputTypeName, false), this.typeNameFromGraphQLType(type, outputTypeName, false))} ${makeExpression((0, language_1.swift) `value`)} }`;
|
222 | }
|
223 | else {
|
224 | return makeExpression(expression);
|
225 | }
|
226 | }
|
227 | }
|
228 | exports.Helpers = Helpers;
|
229 | function makeClosureSignature(parameterTypeName, returnTypeName) {
|
230 | let closureSignature = (0, language_1.swift) `(value: ${parameterTypeName})`;
|
231 | if (returnTypeName) {
|
232 | closureSignature.append((0, language_1.swift) ` -> ${returnTypeName}`);
|
233 | }
|
234 | closureSignature.append((0, language_1.swift) ` in`);
|
235 | return closureSignature;
|
236 | }
|
237 | function makeUniqueName(proposedName, properties) {
|
238 | for (let name = proposedName;; name += "_") {
|
239 | if (properties.every((prop) => prop.propertyName != name)) {
|
240 | return name;
|
241 | }
|
242 | }
|
243 | }
|
244 | function camelCase(value) {
|
245 | const [_, prefix, middle, suffix] = value.match(/^(_*)(.*?)(_*)$/) || [
|
246 | "",
|
247 | "",
|
248 | value,
|
249 | "",
|
250 | ];
|
251 | return `${prefix}${(0, change_case_1.camelCase)(middle)}${suffix}`;
|
252 | }
|
253 | function pascalCase(value) {
|
254 | const [_, prefix, middle, suffix] = value.match(/^(_*)(.*?)(_*)$/) || [
|
255 | "",
|
256 | "",
|
257 | value,
|
258 | "",
|
259 | ];
|
260 | return `${prefix}${(0, change_case_1.pascalCase)(middle)}${suffix}`;
|
261 | }
|
262 |
|
\ | No newline at end of file |