-
Notifications
You must be signed in to change notification settings - Fork 9
/
graphql-types-and-factories.ts
479 lines (405 loc) · 14.4 KB
/
graphql-types-and-factories.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: { input: string; output: string; }
String: { input: string; output: string; }
Boolean: { input: boolean; output: boolean; }
Int: { input: number; output: number; }
Float: { input: number; output: number; }
Date: { input: any; output: any; }
};
export type Author = Named & {
__typename?: 'Author';
anotherId: Scalars['ID']['output'];
birthday?: Maybe<Scalars['Date']['output']>;
bookPopularities: Array<PopularityDetail>;
books: Array<Book>;
id: Scalars['ID']['output'];
name: Scalars['String']['output'];
popularity: PopularityDetail;
summary: AuthorSummary;
working?: Maybe<Working>;
workingDetail: Array<WorkingDetail>;
};
export type AuthorInput = {
name?: InputMaybe<Scalars['String']['input']>;
};
export type AuthorSummary = {
__typename?: 'AuthorSummary';
amountOfSales?: Maybe<Scalars['Float']['output']>;
author: Author;
id: Scalars['ID']['output'];
numberOfBooks: Scalars['Int']['output'];
};
export type Book = Named & {
__typename?: 'Book';
coauthor?: Maybe<Author>;
name: Scalars['String']['output'];
popularity?: Maybe<PopularityDetail>;
reviews?: Maybe<Array<Maybe<BookReview>>>;
status: BookStatus;
};
export type BookReview = {
__typename?: 'BookReview';
rating: Scalars['Int']['output'];
};
export enum BookStatus {
InProgress = 'IN_PROGRESS',
NotStarted = 'NOT_STARTED',
OnHold = 'ON_HOLD'
}
export type CalendarInterval = {
__typename?: 'CalendarInterval';
end: Scalars['Date']['output'];
start: Scalars['Date']['output'];
};
export type Child = Named & {
__typename?: 'Child';
name: Scalars['String']['output'];
parent: Named;
};
export type Mutation = {
__typename?: 'Mutation';
saveAuthor: SaveAuthorResult;
};
export type MutationSaveAuthorArgs = {
input: AuthorInput;
};
export type Named = {
name: Scalars['String']['output'];
};
export type Parent = Named & {
__typename?: 'Parent';
children: Array<Named>;
name: Scalars['String']['output'];
};
export enum Popularity {
High = 'High',
Low = 'Low'
}
export type PopularityDetail = Named & {
__typename?: 'PopularityDetail';
code: Popularity;
name: Scalars['String']['output'];
};
export type Query = {
__typename?: 'Query';
authorSummaries: Array<AuthorSummary>;
authors: Array<Author>;
search: Array<SearchResult>;
};
export type QueryAuthorsArgs = {
id?: InputMaybe<Scalars['ID']['input']>;
};
export type QuerySearchArgs = {
query: Scalars['String']['input'];
};
export type SaveAuthorResult = {
__typename?: 'SaveAuthorResult';
author: Author;
};
export type SearchResult = Author | Book;
export type SearchResults = {
__typename?: 'SearchResults';
result1?: Maybe<SearchResult>;
result2?: Maybe<Named>;
result3?: Maybe<Author>;
};
export enum Working {
No = 'NO',
Yes = 'YES'
}
export type WorkingDetail = {
__typename?: 'WorkingDetail';
code: Working;
extraField: Scalars['Int']['output'];
name: Scalars['String']['output'];
};
import { newDate } from "./testData";
const factories: Record<string, Function> = {};
export interface AuthorOptions {
__typename?: "Author";
anotherId?: Author["anotherId"];
birthday?: Author["birthday"];
bookPopularities?: Array<PopularityDetailOptions>;
books?: Array<BookOptions>;
id?: Author["id"];
name?: Author["name"];
popularity?: PopularityDetailOptions | Popularity;
summary?: AuthorSummaryOptions;
working?: Author["working"];
workingDetail?: Array<WorkingDetailOptions>;
}
export function newAuthor(options: AuthorOptions = {}, cache: Record<string, any> = {}): Author {
const o = (options.__typename ? options : cache["Author"] = {}) as Author;
(cache.all ??= new Set()).add(o);
o.__typename = "Author";
o.anotherId = options.anotherId ?? "anotherId";
o.birthday = options.birthday ?? null;
o.bookPopularities = (options.bookPopularities ?? []).map((i) => enumOrDetailOfPopularity(i));
o.books = (options.books ?? []).map((i) => maybeNew("Book", i, cache, options.hasOwnProperty("books")));
o.id = options.id ?? nextFactoryId("Author");
o.name = options.name ?? "name";
o.popularity = enumOrDetailOfPopularity(options.popularity);
o.summary = maybeNew("AuthorSummary", options.summary, cache, options.hasOwnProperty("summary"));
o.working = options.working ?? null;
o.workingDetail = (options.workingDetail ?? []).map((i) => enumOrDetailOfWorking(i));
return o;
}
factories["Author"] = newAuthor;
export interface AuthorSummaryOptions {
__typename?: "AuthorSummary";
amountOfSales?: AuthorSummary["amountOfSales"];
author?: AuthorOptions;
id?: AuthorSummary["id"];
numberOfBooks?: AuthorSummary["numberOfBooks"];
}
export function newAuthorSummary(options: AuthorSummaryOptions = {}, cache: Record<string, any> = {}): AuthorSummary {
const o = (options.__typename ? options : cache["AuthorSummary"] = {}) as AuthorSummary;
(cache.all ??= new Set()).add(o);
o.__typename = "AuthorSummary";
o.amountOfSales = options.amountOfSales ?? null;
o.author = maybeNew("Author", options.author, cache, options.hasOwnProperty("author"));
o.id = options.id ?? nextFactoryId("AuthorSummary");
o.numberOfBooks = options.numberOfBooks ?? 0;
return o;
}
factories["AuthorSummary"] = newAuthorSummary;
export interface BookOptions {
__typename?: "Book";
coauthor?: AuthorOptions | null;
name?: Book["name"];
popularity?: PopularityDetailOptions | Popularity | null;
reviews?: Array<Maybe<BookReviewOptions>> | null;
status?: Book["status"];
}
export function newBook(options: BookOptions = {}, cache: Record<string, any> = {}): Book {
const o = (options.__typename ? options : cache["Book"] = {}) as Book;
(cache.all ??= new Set()).add(o);
o.__typename = "Book";
o.coauthor = maybeNewOrNull("Author", options.coauthor, cache);
o.name = options.name ?? "name";
o.popularity = enumOrDetailOrNullOfPopularity(options.popularity);
o.reviews = (options.reviews ?? []).map((i) => maybeNewOrNull("BookReview", i, cache));
o.status = options.status ?? BookStatus.InProgress;
return o;
}
factories["Book"] = newBook;
export interface BookReviewOptions {
__typename?: "BookReview";
rating?: BookReview["rating"];
}
export function newBookReview(options: BookReviewOptions = {}, cache: Record<string, any> = {}): BookReview {
const o = (options.__typename ? options : cache["BookReview"] = {}) as BookReview;
(cache.all ??= new Set()).add(o);
o.__typename = "BookReview";
o.rating = options.rating ?? 0;
return o;
}
factories["BookReview"] = newBookReview;
export interface CalendarIntervalOptions {
__typename?: "CalendarInterval";
end?: CalendarInterval["end"];
start?: CalendarInterval["start"];
}
export function newCalendarInterval(
options: CalendarIntervalOptions = {},
cache: Record<string, any> = {},
): CalendarInterval {
const o = (options.__typename ? options : cache["CalendarInterval"] = {}) as CalendarInterval;
(cache.all ??= new Set()).add(o);
o.__typename = "CalendarInterval";
o.end = options.end ?? newDate();
o.start = options.start ?? newDate();
return o;
}
factories["CalendarInterval"] = newCalendarInterval;
export interface ChildOptions {
__typename?: "Child";
name?: Child["name"];
parent?: NamedOptions;
}
export function newChild(options: ChildOptions = {}, cache: Record<string, any> = {}): Child {
const o = (options.__typename ? options : cache["Child"] = {}) as Child;
(cache.all ??= new Set()).add(o);
o.__typename = "Child";
o.name = options.name ?? "name";
o.parent = maybeNew("Author", options.parent, cache, options.hasOwnProperty("parent"));
return o;
}
factories["Child"] = newChild;
export interface ParentOptions {
__typename?: "Parent";
children?: Array<NamedOptions>;
name?: Parent["name"];
}
export function newParent(options: ParentOptions = {}, cache: Record<string, any> = {}): Parent {
const o = (options.__typename ? options : cache["Parent"] = {}) as Parent;
(cache.all ??= new Set()).add(o);
o.__typename = "Parent";
o.children = (options.children ?? []).map((i) => maybeNew("Named", i, cache, options.hasOwnProperty("children")));
o.name = options.name ?? "name";
return o;
}
factories["Parent"] = newParent;
export interface PopularityDetailOptions {
__typename?: "PopularityDetail";
code?: PopularityDetail["code"];
name?: PopularityDetail["name"];
}
export function newPopularityDetail(
options: PopularityDetailOptions = {},
cache: Record<string, any> = {},
): PopularityDetail {
const o = (options.__typename ? options : cache["PopularityDetail"] = {}) as PopularityDetail;
(cache.all ??= new Set()).add(o);
o.__typename = "PopularityDetail";
o.code = options.code ?? Popularity.High;
o.name = options.name ?? "High";
return o;
}
factories["PopularityDetail"] = newPopularityDetail;
export interface SaveAuthorResultOptions {
__typename?: "SaveAuthorResult";
author?: AuthorOptions;
}
export function newSaveAuthorResult(
options: SaveAuthorResultOptions = {},
cache: Record<string, any> = {},
): SaveAuthorResult {
const o = (options.__typename ? options : cache["SaveAuthorResult"] = {}) as SaveAuthorResult;
(cache.all ??= new Set()).add(o);
o.__typename = "SaveAuthorResult";
o.author = maybeNew("Author", options.author, cache, options.hasOwnProperty("author"));
return o;
}
factories["SaveAuthorResult"] = newSaveAuthorResult;
export interface SearchResultsOptions {
__typename?: "SearchResults";
result1?: SearchResults["result1"];
result2?: NamedOptions | null;
result3?: AuthorOptions | null;
}
export function newSearchResults(options: SearchResultsOptions = {}, cache: Record<string, any> = {}): SearchResults {
const o = (options.__typename ? options : cache["SearchResults"] = {}) as SearchResults;
(cache.all ??= new Set()).add(o);
o.__typename = "SearchResults";
o.result1 = options.result1 ?? null;
o.result2 = maybeNewOrNull("Named", options.result2, cache);
o.result3 = maybeNewOrNull("Author", options.result3, cache);
return o;
}
factories["SearchResults"] = newSearchResults;
export interface WorkingDetailOptions {
__typename?: "WorkingDetail";
code?: WorkingDetail["code"];
extraField?: WorkingDetail["extraField"];
name?: WorkingDetail["name"];
}
export function newWorkingDetail(options: WorkingDetailOptions = {}, cache: Record<string, any> = {}): WorkingDetail {
const o = (options.__typename ? options : cache["WorkingDetail"] = {}) as WorkingDetail;
(cache.all ??= new Set()).add(o);
o.__typename = "WorkingDetail";
o.code = options.code ?? Working.No;
o.extraField = options.extraField ?? 0;
o.name = options.name ?? "No";
return o;
}
factories["WorkingDetail"] = newWorkingDetail;
export type NamedOptions = AuthorOptions | BookOptions | ChildOptions | ParentOptions | PopularityDetailOptions;
export type NamedType = Author | Book | Child | Parent | PopularityDetail;
export type NamedTypeName = "Author" | "Book" | "Child" | "Parent" | "PopularityDetail";
factories["Named"] = newAuthor;
const enumDetailNameOfPopularity = { High: "High", Low: "Low" };
function enumOrDetailOfPopularity(enumOrDetail: PopularityDetailOptions | Popularity | undefined): PopularityDetail {
if (enumOrDetail === undefined) {
return newPopularityDetail();
} else if (typeof enumOrDetail === "object" && "code" in enumOrDetail) {
return {
__typename: "PopularityDetail",
code: enumOrDetail.code!,
name: enumDetailNameOfPopularity[enumOrDetail.code!],
...enumOrDetail,
} as PopularityDetail;
} else {
return newPopularityDetail({
code: enumOrDetail as Popularity,
name: enumDetailNameOfPopularity[enumOrDetail as Popularity],
});
}
}
function enumOrDetailOrNullOfPopularity(
enumOrDetail: PopularityDetailOptions | Popularity | undefined | null,
): PopularityDetail | null {
if (enumOrDetail === null) {
return null;
}
return enumOrDetailOfPopularity(enumOrDetail);
}
const enumDetailNameOfWorking = { NO: "No", YES: "Yes" };
function enumOrDetailOfWorking(enumOrDetail: WorkingDetailOptions | Working | undefined): WorkingDetail {
if (enumOrDetail === undefined) {
return newWorkingDetail();
} else if (typeof enumOrDetail === "object" && "code" in enumOrDetail) {
return {
__typename: "WorkingDetail",
code: enumOrDetail.code!,
name: enumDetailNameOfWorking[enumOrDetail.code!],
...enumOrDetail,
} as WorkingDetail;
} else {
return newWorkingDetail({ code: enumOrDetail as Working, name: enumDetailNameOfWorking[enumOrDetail as Working] });
}
}
function enumOrDetailOrNullOfWorking(
enumOrDetail: WorkingDetailOptions | Working | undefined | null,
): WorkingDetail | null {
if (enumOrDetail === null) {
return null;
}
return enumOrDetailOfWorking(enumOrDetail);
}
const taggedIds: Record<string, string> = { "AuthorSummary": "summary" };
let nextFactoryIds: Record<string, number> = {};
export function resetFactoryIds() {
nextFactoryIds = {};
}
function nextFactoryId(objectName: string): string {
const nextId = nextFactoryIds[objectName] || 1;
nextFactoryIds[objectName] = nextId + 1;
const tag = taggedIds[objectName] ?? objectName.replace(/[a-z]/g, "").toLowerCase();
return tag + ":" + nextId;
}
function maybeNew(
type: string,
value: { __typename?: string } | object | undefined,
cache: Record<string, any>,
isSet: boolean = false,
): any {
if (value === undefined) {
return isSet ? undefined : cache[type] || factories[type]({}, cache);
} else if ("__typename" in value && value.__typename) {
return cache.all?.has(value) ? value : factories[value.__typename](value, cache);
} else {
return factories[type](value, cache);
}
}
function maybeNewOrNull(
type: string,
value: { __typename?: string } | object | undefined | null,
cache: Record<string, any>,
): any {
if (!value) {
return null;
} else if ("__typename" in value && value.__typename) {
return cache.all?.has(value) ? value : factories[value.__typename](value, cache);
} else {
return factories[type](value, cache);
}
}