Skip to content

Commit 6ee4aa2

Browse files
committed
update example
1 parent 603e59b commit 6ee4aa2

File tree

2 files changed

+85
-10
lines changed

2 files changed

+85
-10
lines changed

examples/sdk-only/countries/countries.ts

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,8 @@ type SelectionFnParent =
16041604
isRootType?: "Query" | "Mutation" | "Subscription";
16051605
onTypeFragment?: string;
16061606
isFragment?: string;
1607+
1608+
tnp?: string;
16071609
}
16081610
| undefined;
16091611

@@ -2032,8 +2034,8 @@ const selectAll = <
20322034
>(
20332035
selection: Record<string, V>,
20342036
typeNamePure: TNP,
2035-
opts: selectAllOpts<S>,
2036-
collector?: { parents: string[]; path?: string },
2037+
opts: selectAllOpts<S> & { parent?: string },
2038+
collector?: { parents: string[]; path?: string; typeOnType?: string[] },
20372039
) => {
20382040
// let's not make the type too complicated, it's basically a
20392041
// nested map of string to either SLW or again
@@ -2044,8 +2046,26 @@ const selectAll = <
20442046
const tk = collector?.path
20452047
? `${collector.path}.${k}`
20462048
: `${typeNamePure}.${k}`;
2049+
2050+
let typeOnType = (collector?.typeOnType ?? []).at(-1);
2051+
typeOnType = typeOnType?.includes(".")
2052+
? typeOnType.split(".").at(-1)
2053+
: typeOnType;
2054+
const tnpNoArray = typeNamePure.replaceAll("[]", "");
2055+
const tot = typeOnType
2056+
? `${typeOnType}.${tnpNoArray}`
2057+
: opts?.parent
2058+
? `${opts?.parent}.${tnpNoArray}`
2059+
: tnpNoArray;
2060+
20472061
let excludePaths = opts?.exclude ?? ([] as string[]);
2048-
if ("cyclic" in opts) {
2062+
const excludeAllCyclic = "cyclic" in opts && opts.cyclic === "exclude";
2063+
2064+
if (
2065+
"cyclic" in opts &&
2066+
typeof opts.cyclic === "object" &&
2067+
Object.keys(opts.cyclic).length > 0
2068+
) {
20492069
const exclude = Object.entries(
20502070
opts.cyclic as Record<string, string>,
20512071
)
@@ -2077,10 +2097,19 @@ const selectAll = <
20772097
if (excludePaths.includes(tk as any)) continue;
20782098

20792099
if (typeof v === "function") {
2100+
if (collector?.typeOnType && collector?.typeOnType.includes(tot)) {
2101+
if (!excludeAllCyclic) {
2102+
throw new Error(
2103+
`Circular dependency: ${collector?.typeOnType.join(" -> ")}`,
2104+
);
2105+
}
2106+
continue;
2107+
}
2108+
20802109
if (v.name.startsWith("bound ")) {
2081-
// if (collector?.parents?.includes(tk)) continue;
20822110
const col = {
20832111
parents: [...(collector?.parents ?? []), tk],
2112+
typeOnType: [...(collector?.typeOnType ?? []), tot],
20842113
path: tk,
20852114
};
20862115
s[k] = v(
@@ -2145,7 +2174,10 @@ const makeSLFN = <
21452174
const s =
21462175
_s ??
21472176
((selection: FF) =>
2148-
(selection as any)["$all"]({ cyclic: "exclude" }) as TT);
2177+
(selection as any)["$all"]({
2178+
cyclic: "exclude",
2179+
parent: parent?.tnp,
2180+
}) as TT);
21492181

21502182
const selection: FF = makeSLFNInput.bind(this)() as any;
21512183
const r = s(selection);
@@ -2410,6 +2442,7 @@ export function makeCountryNotNullArrayNotNullSelectionInput(
24102442
this: any,
24112443
): ReturnTypeFromCountryNotNullArrayNotNullSelection {
24122444
const that = this;
2445+
const tnp = "Country";
24132446
return {
24142447
get awsRegion() {
24152448
return new SelectionWrapper(
@@ -2437,6 +2470,7 @@ export function makeCountryNotNullArrayNotNullSelectionInput(
24372470
continent: ContinentNotNullSelection.bind({
24382471
collector: that,
24392472
fieldName: "continent",
2473+
tnp,
24402474
}) as any,
24412475
get currencies() {
24422476
return new SelectionWrapper(
@@ -2481,6 +2515,7 @@ export function makeCountryNotNullArrayNotNullSelectionInput(
24812515
languages: LanguageNotNullArrayNotNullSelection.bind({
24822516
collector: that,
24832517
fieldName: "languages",
2518+
tnp,
24842519
}) as any,
24852520
get name() {
24862521
return (args: CountryNameArgs) =>
@@ -2516,15 +2551,24 @@ export function makeCountryNotNullArrayNotNullSelectionInput(
25162551
);
25172552
},
25182553
get phones() {
2519-
return new SelectionWrapper("phones", "String!", 1, {}, that);
2554+
return new SelectionWrapper(
2555+
"phones",
2556+
"String!",
2557+
1,
2558+
{},
2559+
that,
2560+
undefined,
2561+
);
25202562
},
25212563
states: StateNotNullArrayNotNullSelection.bind({
25222564
collector: that,
25232565
fieldName: "states",
2566+
tnp,
25242567
}) as any,
25252568
subdivisions: SubdivisionNotNullArrayNotNullSelection.bind({
25262569
collector: that,
25272570
fieldName: "subdivisions",
2571+
tnp,
25282572
}) as any,
25292573

25302574
$fragment: <F extends (this: any, ...args: any[]) => any>(f: F) =>
@@ -2594,13 +2638,15 @@ export function makeContinentNotNullSelectionInput(
25942638
this: any,
25952639
): ReturnTypeFromContinentNotNullSelection {
25962640
const that = this;
2641+
const tnp = "Continent";
25972642
return {
25982643
get code() {
25992644
return new SelectionWrapper("code", "ID!", 0, {}, that, undefined);
26002645
},
26012646
countries: CountryNotNullArrayNotNullSelection.bind({
26022647
collector: that,
26032648
fieldName: "countries",
2649+
tnp,
26042650
}) as any,
26052651
get name() {
26062652
return new SelectionWrapper(
@@ -2684,13 +2730,15 @@ export function makeLanguageNotNullArrayNotNullSelectionInput(
26842730
this: any,
26852731
): ReturnTypeFromLanguageNotNullArrayNotNullSelection {
26862732
const that = this;
2733+
const tnp = "Language";
26872734
return {
26882735
get code() {
26892736
return new SelectionWrapper("code", "ID!", 0, {}, that, undefined);
26902737
},
26912738
countries: CountryNotNullArrayNotNullSelection.bind({
26922739
collector: that,
26932740
fieldName: "countries",
2741+
tnp,
26942742
}) as any,
26952743
get name() {
26962744
return new SelectionWrapper(
@@ -2790,6 +2838,7 @@ export function makeStateNotNullArrayNotNullSelectionInput(
27902838
this: any,
27912839
): ReturnTypeFromStateNotNullArrayNotNullSelection {
27922840
const that = this;
2841+
const tnp = "State";
27932842
return {
27942843
get code() {
27952844
return new SelectionWrapper(
@@ -2804,6 +2853,7 @@ export function makeStateNotNullArrayNotNullSelectionInput(
28042853
country: CountryNotNullSelection.bind({
28052854
collector: that,
28062855
fieldName: "country",
2856+
tnp,
28072857
}) as any,
28082858
get name() {
28092859
return new SelectionWrapper(
@@ -2931,6 +2981,7 @@ export function makeCountryNotNullSelectionInput(
29312981
this: any,
29322982
): ReturnTypeFromCountryNotNullSelection {
29332983
const that = this;
2984+
const tnp = "Country";
29342985
return {
29352986
get awsRegion() {
29362987
return new SelectionWrapper(
@@ -2958,6 +3009,7 @@ export function makeCountryNotNullSelectionInput(
29583009
continent: ContinentNotNullSelection.bind({
29593010
collector: that,
29603011
fieldName: "continent",
3012+
tnp,
29613013
}) as any,
29623014
get currencies() {
29633015
return new SelectionWrapper(
@@ -3002,6 +3054,7 @@ export function makeCountryNotNullSelectionInput(
30023054
languages: LanguageNotNullArrayNotNullSelection.bind({
30033055
collector: that,
30043056
fieldName: "languages",
3057+
tnp,
30053058
}) as any,
30063059
get name() {
30073060
return (args: CountryNameArgs) =>
@@ -3049,10 +3102,12 @@ export function makeCountryNotNullSelectionInput(
30493102
states: StateNotNullArrayNotNullSelection.bind({
30503103
collector: that,
30513104
fieldName: "states",
3105+
tnp,
30523106
}) as any,
30533107
subdivisions: SubdivisionNotNullArrayNotNullSelection.bind({
30543108
collector: that,
30553109
fieldName: "subdivisions",
3110+
tnp,
30563111
}) as any,
30573112

30583113
$fragment: <F extends (this: any, ...args: any[]) => any>(f: F) =>
@@ -3117,6 +3172,7 @@ export function makeSubdivisionNotNullArrayNotNullSelectionInput(
31173172
this: any,
31183173
): ReturnTypeFromSubdivisionNotNullArrayNotNullSelection {
31193174
const that = this;
3175+
const tnp = "Subdivision";
31203176
return {
31213177
get code() {
31223178
return new SelectionWrapper("code", "ID!", 0, {}, that, undefined);
@@ -3211,13 +3267,15 @@ export function makeContinentSelectionInput(
32113267
this: any,
32123268
): ReturnTypeFromContinentSelection {
32133269
const that = this;
3270+
const tnp = "Continent";
32143271
return {
32153272
get code() {
32163273
return new SelectionWrapper("code", "ID!", 0, {}, that, undefined);
32173274
},
32183275
countries: CountryNotNullArrayNotNullSelection.bind({
32193276
collector: that,
32203277
fieldName: "countries",
3278+
tnp,
32213279
}) as any,
32223280
get name() {
32233281
return new SelectionWrapper(
@@ -3297,13 +3355,15 @@ export function makeContinentNotNullArrayNotNullSelectionInput(
32973355
this: any,
32983356
): ReturnTypeFromContinentNotNullArrayNotNullSelection {
32993357
const that = this;
3358+
const tnp = "Continent";
33003359
return {
33013360
get code() {
33023361
return new SelectionWrapper("code", "ID!", 0, {}, that, undefined);
33033362
},
33043363
countries: CountryNotNullArrayNotNullSelection.bind({
33053364
collector: that,
33063365
fieldName: "countries",
3366+
tnp,
33073367
}) as any,
33083368
get name() {
33093369
return new SelectionWrapper(
@@ -3435,6 +3495,7 @@ export function makeCountrySelectionInput(
34353495
this: any,
34363496
): ReturnTypeFromCountrySelection {
34373497
const that = this;
3498+
const tnp = "Country";
34383499
return {
34393500
get awsRegion() {
34403501
return new SelectionWrapper(
@@ -3462,6 +3523,7 @@ export function makeCountrySelectionInput(
34623523
continent: ContinentNotNullSelection.bind({
34633524
collector: that,
34643525
fieldName: "continent",
3526+
tnp,
34653527
}) as any,
34663528
get currencies() {
34673529
return new SelectionWrapper(
@@ -3506,6 +3568,7 @@ export function makeCountrySelectionInput(
35063568
languages: LanguageNotNullArrayNotNullSelection.bind({
35073569
collector: that,
35083570
fieldName: "languages",
3571+
tnp,
35093572
}) as any,
35103573
get name() {
35113574
return (args: CountryNameArgs) =>
@@ -3553,10 +3616,12 @@ export function makeCountrySelectionInput(
35533616
states: StateNotNullArrayNotNullSelection.bind({
35543617
collector: that,
35553618
fieldName: "states",
3619+
tnp,
35563620
}) as any,
35573621
subdivisions: SubdivisionNotNullArrayNotNullSelection.bind({
35583622
collector: that,
35593623
fieldName: "subdivisions",
3624+
tnp,
35603625
}) as any,
35613626

35623627
$fragment: <F extends (this: any, ...args: any[]) => any>(f: F) =>
@@ -3628,13 +3693,15 @@ export function makeLanguageSelectionInput(
36283693
this: any,
36293694
): ReturnTypeFromLanguageSelection {
36303695
const that = this;
3696+
const tnp = "Language";
36313697
return {
36323698
get code() {
36333699
return new SelectionWrapper("code", "ID!", 0, {}, that, undefined);
36343700
},
36353701
countries: CountryNotNullArrayNotNullSelection.bind({
36363702
collector: that,
36373703
fieldName: "countries",
3704+
tnp,
36383705
}) as any,
36393706
get name() {
36403707
return new SelectionWrapper(
@@ -3816,48 +3883,55 @@ export function makeQuerySelectionInput(
38163883
this: any,
38173884
): ReturnTypeFromQuerySelection {
38183885
const that = this;
3886+
const tnp = "Query";
38193887
return {
38203888
continent: (args: QueryContinentArgs) =>
38213889
ContinentSelection.bind({
38223890
collector: that,
38233891
fieldName: "continent",
38243892
args,
38253893
argsMeta: QueryContinentArgsMeta,
3894+
tnp,
38263895
}) as any,
38273896
continents: (args: QueryContinentsArgs) =>
38283897
ContinentNotNullArrayNotNullSelection.bind({
38293898
collector: that,
38303899
fieldName: "continents",
38313900
args,
38323901
argsMeta: QueryContinentsArgsMeta,
3902+
tnp,
38333903
}) as any,
38343904
countries: (args: QueryCountriesArgs) =>
38353905
CountryNotNullArrayNotNullSelection.bind({
38363906
collector: that,
38373907
fieldName: "countries",
38383908
args,
38393909
argsMeta: QueryCountriesArgsMeta,
3910+
tnp,
38403911
}) as any,
38413912
country: (args: QueryCountryArgs) =>
38423913
CountrySelection.bind({
38433914
collector: that,
38443915
fieldName: "country",
38453916
args,
38463917
argsMeta: QueryCountryArgsMeta,
3918+
tnp,
38473919
}) as any,
38483920
language: (args: QueryLanguageArgs) =>
38493921
LanguageSelection.bind({
38503922
collector: that,
38513923
fieldName: "language",
38523924
args,
38533925
argsMeta: QueryLanguageArgsMeta,
3926+
tnp,
38543927
}) as any,
38553928
languages: (args: QueryLanguagesArgs) =>
38563929
LanguageNotNullArrayNotNullSelection.bind({
38573930
collector: that,
38583931
fieldName: "languages",
38593932
args,
38603933
argsMeta: QueryLanguagesArgsMeta,
3934+
tnp,
38613935
}) as any,
38623936

38633937
$fragment: <F extends (this: any, ...args: any[]) => any>(f: F) =>
@@ -3918,6 +3992,7 @@ export function makeStateSelectionInput(
39183992
this: any,
39193993
): ReturnTypeFromStateSelection {
39203994
const that = this;
3995+
const tnp = "State";
39213996
return {
39223997
get code() {
39233998
return new SelectionWrapper(
@@ -3932,6 +4007,7 @@ export function makeStateSelectionInput(
39324007
country: CountryNotNullSelection.bind({
39334008
collector: that,
39344009
fieldName: "country",
4010+
tnp,
39354011
}) as any,
39364012
get name() {
39374013
return new SelectionWrapper(
@@ -3999,6 +4075,7 @@ export function makeSubdivisionSelectionInput(
39994075
this: any,
40004076
): ReturnTypeFromSubdivisionSelection {
40014077
const that = this;
4078+
const tnp = "Subdivision";
40024079
return {
40034080
get code() {
40044081
return new SelectionWrapper("code", "ID!", 0, {}, that, undefined);

0 commit comments

Comments
 (0)