@preprio/toolkit 0.6.0-alpha.6 → 0.6.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -52
- package/dist/astro/{chunk-MWRA3732.js → chunk-3EXK2LHI.js} +1 -1
- package/dist/astro/graphql.cjs +58 -10
- package/dist/astro/graphql.d.cts +33 -38
- package/dist/astro/graphql.d.ts +33 -38
- package/dist/astro/graphql.js +59 -11
- package/dist/astro/index.cjs +1 -1
- package/dist/astro/index.js +1 -1
- package/dist/{chunk-OQGEL4TV.js → chunk-3QGWCYTL.js} +1 -1
- package/dist/{chunk-W6AVX6FI.js → chunk-PFJO4TYL.js} +52 -5
- package/dist/{chunk-Y7DWVOOY.js → chunk-U7TRFVK7.js} +1 -1
- package/dist/{chunk-NSQOGI5K.js → chunk-VQIPTUN4.js} +1 -1
- package/dist/graphql/codegen.cjs +51 -4
- package/dist/graphql/codegen.js +3 -3
- package/dist/graphql.cjs +89 -10
- package/dist/graphql.d.cts +69 -39
- package/dist/graphql.d.ts +69 -39
- package/dist/graphql.js +38 -9
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/dist/nextjs/{chunk-PIQF5ZI5.js → chunk-CZK7SMBD.js} +58 -53
- package/dist/nextjs/components.cjs +175 -242
- package/dist/nextjs/components.d.cts +25 -21
- package/dist/nextjs/components.d.ts +25 -21
- package/dist/nextjs/components.js +167 -236
- package/dist/nextjs/graphql.cjs +372 -509
- package/dist/nextjs/graphql.d.cts +131 -135
- package/dist/nextjs/graphql.d.ts +131 -135
- package/dist/nextjs/graphql.js +307 -456
- package/dist/nextjs/index.cjs +114 -54
- package/dist/nextjs/index.d.cts +57 -33
- package/dist/nextjs/index.d.ts +57 -33
- package/dist/nextjs/index.js +108 -5
- package/dist/nuxt/{chunk-PF6GVWHV.js → chunk-JL33ENCM.js} +1 -1
- package/dist/nuxt/graphql.cjs +58 -10
- package/dist/nuxt/graphql.d.cts +33 -38
- package/dist/nuxt/graphql.d.ts +33 -38
- package/dist/nuxt/graphql.js +59 -11
- package/dist/nuxt/index.cjs +1 -1
- package/dist/nuxt/index.js +1 -1
- package/dist/react/components.cjs +1 -1
- package/dist/react/components.js +1 -1
- package/dist/sveltekit/{chunk-5K7HWMIF.js → chunk-S7S276V7.js} +1 -1
- package/dist/sveltekit/graphql.cjs +58 -10
- package/dist/sveltekit/graphql.d.cts +33 -38
- package/dist/sveltekit/graphql.d.ts +33 -38
- package/dist/sveltekit/graphql.js +59 -11
- package/dist/sveltekit/index.cjs +1 -1
- package/dist/sveltekit/index.js +1 -1
- package/package.json +1 -1
package/dist/astro/graphql.d.ts
CHANGED
|
@@ -95,39 +95,6 @@ interface PreprVariantProps {
|
|
|
95
95
|
*/
|
|
96
96
|
declare function preprVariantProps(item: unknown): PreprVariantProps;
|
|
97
97
|
|
|
98
|
-
/**
|
|
99
|
-
* The schema facts the query rewrites need, from `loadPreprSchema`. Pass it
|
|
100
|
-
* along; its shape is not part of the public API.
|
|
101
|
-
*/
|
|
102
|
-
type PreprSchema = PreprSchemaInfo;
|
|
103
|
-
/**
|
|
104
|
-
* Your query, asking for every personalization and A/B variant: what
|
|
105
|
-
* `query(..., { variants: true })` sends, as a string for your own GraphQL
|
|
106
|
-
* client. The result is the same for every visitor, so cache it like any
|
|
107
|
-
* other content query, and render it with `resolveVariants` or `PreprStack`.
|
|
108
|
-
*
|
|
109
|
-
* A query without personalizable fields comes back unchanged (minified).
|
|
110
|
-
*/
|
|
111
|
-
declare function toVariantsQuery(document: PreprDocument<unknown, unknown> | string, schema: PreprSchema): string;
|
|
112
|
-
/**
|
|
113
|
-
* The per-visitor decision query for the same query: send it with the
|
|
114
|
-
* visitor's Prepr headers, never cached, and pass the response's `data` to
|
|
115
|
-
* `decisionsFrom`.
|
|
116
|
-
*
|
|
117
|
-
* Pass the variants result as `from` whenever you have it: the query then
|
|
118
|
-
* asks only about the lists that hold segment groups on this page, which is
|
|
119
|
-
* a few hundred bytes instead of the whole page and markedly faster for a
|
|
120
|
-
* visitor Prepr has not seen before. Without `from` it asks about every
|
|
121
|
-
* personalizable list. `topLevelOnly` stops at the first personalizable list
|
|
122
|
-
* on each path, for middleware that decides only the top of a stack.
|
|
123
|
-
*
|
|
124
|
-
* Null when there is nothing to decide: send no request then.
|
|
125
|
-
*/
|
|
126
|
-
declare function toDecisionQuery(document: PreprDocument<unknown, unknown> | string, schema: PreprSchema, options?: {
|
|
127
|
-
from?: unknown;
|
|
128
|
-
topLevelOnly?: boolean;
|
|
129
|
-
}): string | null;
|
|
130
|
-
|
|
131
98
|
/**
|
|
132
99
|
* A GraphQL document accepted by `query`: a plain string, graphql-codegen's
|
|
133
100
|
* `TypedDocumentString` (`documentMode: 'string'`), or any `DocumentNode`,
|
|
@@ -221,11 +188,6 @@ interface PreprDecideOptions extends Omit<PreprQueryOptions, 'variants'> {
|
|
|
221
188
|
* none, e.g. on pages with only A/B tests or no personalization.
|
|
222
189
|
*/
|
|
223
190
|
from?: unknown;
|
|
224
|
-
/**
|
|
225
|
-
* @internal Ask only about the first personalizable list on each path,
|
|
226
|
-
* without `from`: what middleware needs for groups at the top of a stack.
|
|
227
|
-
*/
|
|
228
|
-
topLevelOnly?: boolean;
|
|
229
191
|
}
|
|
230
192
|
interface PreprQueryResult<TResult> {
|
|
231
193
|
data: TResult;
|
|
@@ -236,6 +198,39 @@ interface PreprQueryResult<TResult> {
|
|
|
236
198
|
personalized: boolean;
|
|
237
199
|
}
|
|
238
200
|
|
|
201
|
+
/**
|
|
202
|
+
* The schema facts the query rewrites need, from `loadPreprSchema`. Pass it
|
|
203
|
+
* along; its shape is not part of the public API.
|
|
204
|
+
*/
|
|
205
|
+
type PreprSchema = PreprSchemaInfo;
|
|
206
|
+
/**
|
|
207
|
+
* Your query, asking for every personalization and A/B variant: what
|
|
208
|
+
* `query(..., { variants: true })` sends, as a string for your own GraphQL
|
|
209
|
+
* client. The result is the same for every visitor, so cache it like any
|
|
210
|
+
* other content query, and render it with `resolveVariants` or `PreprStack`.
|
|
211
|
+
*
|
|
212
|
+
* A query without personalizable fields comes back unchanged (minified).
|
|
213
|
+
*/
|
|
214
|
+
declare function toVariantsQuery(document: PreprDocument<unknown, unknown> | string, schema: PreprSchema): string;
|
|
215
|
+
/**
|
|
216
|
+
* The per-visitor decision query for the same query: send it with the
|
|
217
|
+
* visitor's Prepr headers, never cached, and pass the response's `data` to
|
|
218
|
+
* `decisionsFrom`.
|
|
219
|
+
*
|
|
220
|
+
* Pass the variants result as `from` whenever you have it: the query then
|
|
221
|
+
* asks only about the lists that hold segment groups on this page, which is
|
|
222
|
+
* a few hundred bytes instead of the whole page and markedly faster for a
|
|
223
|
+
* visitor Prepr has not seen before. Without `from` it asks about every
|
|
224
|
+
* personalizable list. `topLevelOnly` stops at the first personalizable list
|
|
225
|
+
* on each path, for middleware that decides only the top of a stack.
|
|
226
|
+
*
|
|
227
|
+
* Null when there is nothing to decide: send no request then.
|
|
228
|
+
*/
|
|
229
|
+
declare function toDecisionQuery(document: PreprDocument<unknown, unknown> | string, schema: PreprSchema, options?: {
|
|
230
|
+
from?: unknown;
|
|
231
|
+
topLevelOnly?: boolean;
|
|
232
|
+
}): string | null;
|
|
233
|
+
|
|
239
234
|
/**
|
|
240
235
|
* Query options for frameworks without an implicit request scope.
|
|
241
236
|
* `personalize: true` requires the request `event`; the compiler enforces it.
|
package/dist/astro/graphql.js
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
getPreprHeadersFromHeaders,
|
|
4
4
|
isPersonalizedResponse,
|
|
5
5
|
validatePreprToken
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-3EXK2LHI.js";
|
|
7
7
|
|
|
8
8
|
// src/core/variants/constants.ts
|
|
9
9
|
var PREPR_CONTEXT_ALIAS = "_preprContext";
|
|
@@ -92,7 +92,10 @@ function decisionSelectionSet(set, typeName, path, ctx) {
|
|
|
92
92
|
path,
|
|
93
93
|
ctx
|
|
94
94
|
);
|
|
95
|
-
if (pruned)
|
|
95
|
+
if (!pruned) continue;
|
|
96
|
+
if (fragment.typeCondition.name.value === typeName) {
|
|
97
|
+
selections.push(...pruned.selections);
|
|
98
|
+
} else {
|
|
96
99
|
selections.push(
|
|
97
100
|
inlineFragment(fragment.typeCondition.name.value, [
|
|
98
101
|
...pruned.selections
|
|
@@ -102,13 +105,19 @@ function decisionSelectionSet(set, typeName, path, ctx) {
|
|
|
102
105
|
continue;
|
|
103
106
|
}
|
|
104
107
|
if (selection.kind === "InlineFragment") {
|
|
108
|
+
const condition = selection.typeCondition?.name.value ?? typeName;
|
|
105
109
|
const pruned = decisionSelectionSet(
|
|
106
110
|
selection.selectionSet,
|
|
107
|
-
|
|
111
|
+
condition,
|
|
108
112
|
path,
|
|
109
113
|
ctx
|
|
110
114
|
);
|
|
111
|
-
if (pruned)
|
|
115
|
+
if (!pruned) continue;
|
|
116
|
+
if (condition === typeName && !selection.directives?.length) {
|
|
117
|
+
selections.push(...pruned.selections);
|
|
118
|
+
} else {
|
|
119
|
+
selections.push({ ...selection, selectionSet: pruned });
|
|
120
|
+
}
|
|
112
121
|
continue;
|
|
113
122
|
}
|
|
114
123
|
if (!selection.selectionSet) continue;
|
|
@@ -182,20 +191,58 @@ function toDecisionDocument(document2, schema, groupPaths, options) {
|
|
|
182
191
|
const used = /* @__PURE__ */ new Set();
|
|
183
192
|
collectVariables(pruned, used);
|
|
184
193
|
collectVariables(operation.directives, used);
|
|
194
|
+
const shared = sharedContextFragment(schema);
|
|
185
195
|
return {
|
|
186
196
|
kind: "Document",
|
|
187
197
|
definitions: [
|
|
198
|
+
...shared ? [shared.definition] : [],
|
|
188
199
|
{
|
|
189
200
|
...operation,
|
|
190
201
|
name: operation.name && name(`${operation.name.value}Decision`),
|
|
191
202
|
variableDefinitions: (operation.variableDefinitions ?? []).filter(
|
|
192
203
|
(definition) => used.has(definition.variable.name.value)
|
|
193
204
|
),
|
|
194
|
-
selectionSet: pruned
|
|
205
|
+
selectionSet: shared ? shared.apply(pruned) : pruned
|
|
195
206
|
}
|
|
196
207
|
]
|
|
197
208
|
};
|
|
198
209
|
}
|
|
210
|
+
function sharedContextFragment(schema) {
|
|
211
|
+
const contextTypes = new Set(
|
|
212
|
+
Object.values(schema.types).flatMap(
|
|
213
|
+
(type) => type.hasContext ? [type.fields._context.type] : []
|
|
214
|
+
)
|
|
215
|
+
);
|
|
216
|
+
if (contextTypes.size !== 1) return null;
|
|
217
|
+
const [contextType] = contextTypes;
|
|
218
|
+
const fragmentName = "c";
|
|
219
|
+
const definition = {
|
|
220
|
+
kind: "FragmentDefinition",
|
|
221
|
+
name: name(fragmentName),
|
|
222
|
+
typeCondition: { kind: "NamedType", name: name(contextType) },
|
|
223
|
+
selectionSet: selectionSet(
|
|
224
|
+
["kind", "group_id", "variant_id"].map(
|
|
225
|
+
(field) => ({ kind: "Field", name: name(field) })
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
};
|
|
229
|
+
const spread = {
|
|
230
|
+
kind: "FragmentSpread",
|
|
231
|
+
name: name(fragmentName)
|
|
232
|
+
};
|
|
233
|
+
const apply = (set) => selectionSet(
|
|
234
|
+
set.selections.map((selection) => {
|
|
235
|
+
if (selection.kind === "Field" && selection.alias?.value === PREPR_CONTEXT_ALIAS) {
|
|
236
|
+
return { ...selection, selectionSet: selectionSet([spread]) };
|
|
237
|
+
}
|
|
238
|
+
return "selectionSet" in selection && selection.selectionSet ? {
|
|
239
|
+
...selection,
|
|
240
|
+
selectionSet: apply(selection.selectionSet)
|
|
241
|
+
} : selection;
|
|
242
|
+
})
|
|
243
|
+
);
|
|
244
|
+
return { definition, apply };
|
|
245
|
+
}
|
|
199
246
|
|
|
200
247
|
// src/core/variants/probe-document.ts
|
|
201
248
|
function probeSelectionSet(set, fragments) {
|
|
@@ -769,6 +816,7 @@ function createVaryProbe(run, options) {
|
|
|
769
816
|
}
|
|
770
817
|
|
|
771
818
|
// src/core/graphql/client.ts
|
|
819
|
+
var clientSchemas = /* @__PURE__ */ new WeakMap();
|
|
772
820
|
function isParseableUrl(url) {
|
|
773
821
|
try {
|
|
774
822
|
new URL(url);
|
|
@@ -805,7 +853,7 @@ function createPreprClient(options) {
|
|
|
805
853
|
));
|
|
806
854
|
const variantsDocuments = /* @__PURE__ */ new Map();
|
|
807
855
|
const decisionDocuments = /* @__PURE__ */ new Map();
|
|
808
|
-
|
|
856
|
+
const client = {
|
|
809
857
|
async query(document2, variables, queryOptions = {}) {
|
|
810
858
|
const { variants, ...rest } = queryOptions;
|
|
811
859
|
if (!variants) return run(document2, variables, rest);
|
|
@@ -817,10 +865,10 @@ function createPreprClient(options) {
|
|
|
817
865
|
return run(derived, variables, rest);
|
|
818
866
|
},
|
|
819
867
|
async decide(document2, variables, queryOptions = {}) {
|
|
820
|
-
const { from,
|
|
868
|
+
const { from, ...rest } = queryOptions;
|
|
821
869
|
const paths = from === void 0 ? void 0 : personalizationPaths(from);
|
|
822
870
|
if (paths && paths.size === 0) return /* @__PURE__ */ new Map();
|
|
823
|
-
const signature = paths ? [...paths].map(([path, types]) => `${path}:${[...types].sort().join(",")}`).sort().join("|") :
|
|
871
|
+
const signature = paths ? [...paths].map(([path, types]) => `${path}:${[...types].sort().join(",")}`).sort().join("|") : "*";
|
|
824
872
|
let byPaths = decisionDocuments.get(document2);
|
|
825
873
|
if (!byPaths) {
|
|
826
874
|
byPaths = /* @__PURE__ */ new Map();
|
|
@@ -828,9 +876,7 @@ function createPreprClient(options) {
|
|
|
828
876
|
}
|
|
829
877
|
let derived = byPaths.get(signature);
|
|
830
878
|
if (derived === void 0) {
|
|
831
|
-
derived = toDecisionDocument(document2, await loadSchema(), paths
|
|
832
|
-
topLevelOnly: !paths && topLevelOnly
|
|
833
|
-
});
|
|
879
|
+
derived = toDecisionDocument(document2, await loadSchema(), paths);
|
|
834
880
|
byPaths.set(signature, derived);
|
|
835
881
|
}
|
|
836
882
|
if (!derived) return /* @__PURE__ */ new Map();
|
|
@@ -844,6 +890,8 @@ function createPreprClient(options) {
|
|
|
844
890
|
(vary) => vary === null || SEGMENTS_VARY.test(vary)
|
|
845
891
|
)
|
|
846
892
|
};
|
|
893
|
+
clientSchemas.set(client, loadSchema);
|
|
894
|
+
return client;
|
|
847
895
|
}
|
|
848
896
|
|
|
849
897
|
// src/core/graphql/event-client.ts
|
package/dist/astro/index.cjs
CHANGED
package/dist/astro/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
PreprError
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-3QGWCYTL.js";
|
|
4
4
|
|
|
5
5
|
// src/core/variants/constants.ts
|
|
6
6
|
var PREPR_CONTEXT_ALIAS = "_preprContext";
|
|
@@ -244,7 +244,10 @@ function decisionSelectionSet(set, typeName, path, ctx) {
|
|
|
244
244
|
path,
|
|
245
245
|
ctx
|
|
246
246
|
);
|
|
247
|
-
if (pruned)
|
|
247
|
+
if (!pruned) continue;
|
|
248
|
+
if (fragment.typeCondition.name.value === typeName) {
|
|
249
|
+
selections.push(...pruned.selections);
|
|
250
|
+
} else {
|
|
248
251
|
selections.push(
|
|
249
252
|
inlineFragment(fragment.typeCondition.name.value, [
|
|
250
253
|
...pruned.selections
|
|
@@ -254,13 +257,19 @@ function decisionSelectionSet(set, typeName, path, ctx) {
|
|
|
254
257
|
continue;
|
|
255
258
|
}
|
|
256
259
|
if (selection.kind === "InlineFragment") {
|
|
260
|
+
const condition = selection.typeCondition?.name.value ?? typeName;
|
|
257
261
|
const pruned = decisionSelectionSet(
|
|
258
262
|
selection.selectionSet,
|
|
259
|
-
|
|
263
|
+
condition,
|
|
260
264
|
path,
|
|
261
265
|
ctx
|
|
262
266
|
);
|
|
263
|
-
if (pruned)
|
|
267
|
+
if (!pruned) continue;
|
|
268
|
+
if (condition === typeName && !selection.directives?.length) {
|
|
269
|
+
selections.push(...pruned.selections);
|
|
270
|
+
} else {
|
|
271
|
+
selections.push({ ...selection, selectionSet: pruned });
|
|
272
|
+
}
|
|
264
273
|
continue;
|
|
265
274
|
}
|
|
266
275
|
if (!selection.selectionSet) continue;
|
|
@@ -334,20 +343,58 @@ function toDecisionDocument(document, schema, groupPaths, options) {
|
|
|
334
343
|
const used = /* @__PURE__ */ new Set();
|
|
335
344
|
collectVariables(pruned, used);
|
|
336
345
|
collectVariables(operation.directives, used);
|
|
346
|
+
const shared = sharedContextFragment(schema);
|
|
337
347
|
return {
|
|
338
348
|
kind: "Document",
|
|
339
349
|
definitions: [
|
|
350
|
+
...shared ? [shared.definition] : [],
|
|
340
351
|
{
|
|
341
352
|
...operation,
|
|
342
353
|
name: operation.name && name(`${operation.name.value}Decision`),
|
|
343
354
|
variableDefinitions: (operation.variableDefinitions ?? []).filter(
|
|
344
355
|
(definition) => used.has(definition.variable.name.value)
|
|
345
356
|
),
|
|
346
|
-
selectionSet: pruned
|
|
357
|
+
selectionSet: shared ? shared.apply(pruned) : pruned
|
|
347
358
|
}
|
|
348
359
|
]
|
|
349
360
|
};
|
|
350
361
|
}
|
|
362
|
+
function sharedContextFragment(schema) {
|
|
363
|
+
const contextTypes = new Set(
|
|
364
|
+
Object.values(schema.types).flatMap(
|
|
365
|
+
(type) => type.hasContext ? [type.fields._context.type] : []
|
|
366
|
+
)
|
|
367
|
+
);
|
|
368
|
+
if (contextTypes.size !== 1) return null;
|
|
369
|
+
const [contextType] = contextTypes;
|
|
370
|
+
const fragmentName = "c";
|
|
371
|
+
const definition = {
|
|
372
|
+
kind: "FragmentDefinition",
|
|
373
|
+
name: name(fragmentName),
|
|
374
|
+
typeCondition: { kind: "NamedType", name: name(contextType) },
|
|
375
|
+
selectionSet: selectionSet(
|
|
376
|
+
["kind", "group_id", "variant_id"].map(
|
|
377
|
+
(field) => ({ kind: "Field", name: name(field) })
|
|
378
|
+
)
|
|
379
|
+
)
|
|
380
|
+
};
|
|
381
|
+
const spread = {
|
|
382
|
+
kind: "FragmentSpread",
|
|
383
|
+
name: name(fragmentName)
|
|
384
|
+
};
|
|
385
|
+
const apply = (set) => selectionSet(
|
|
386
|
+
set.selections.map((selection) => {
|
|
387
|
+
if (selection.kind === "Field" && selection.alias?.value === PREPR_CONTEXT_ALIAS) {
|
|
388
|
+
return { ...selection, selectionSet: selectionSet([spread]) };
|
|
389
|
+
}
|
|
390
|
+
return "selectionSet" in selection && selection.selectionSet ? {
|
|
391
|
+
...selection,
|
|
392
|
+
selectionSet: apply(selection.selectionSet)
|
|
393
|
+
} : selection;
|
|
394
|
+
})
|
|
395
|
+
);
|
|
396
|
+
return { definition, apply };
|
|
397
|
+
}
|
|
351
398
|
|
|
352
399
|
// src/core/variants/variants-document.ts
|
|
353
400
|
function variantsSelectionSet(set, typeName, schema) {
|
package/dist/graphql/codegen.cjs
CHANGED
|
@@ -169,7 +169,10 @@ function decisionSelectionSet(set, typeName, path, ctx) {
|
|
|
169
169
|
path,
|
|
170
170
|
ctx
|
|
171
171
|
);
|
|
172
|
-
if (pruned)
|
|
172
|
+
if (!pruned) continue;
|
|
173
|
+
if (fragment.typeCondition.name.value === typeName) {
|
|
174
|
+
selections.push(...pruned.selections);
|
|
175
|
+
} else {
|
|
173
176
|
selections.push(
|
|
174
177
|
inlineFragment(fragment.typeCondition.name.value, [
|
|
175
178
|
...pruned.selections
|
|
@@ -179,13 +182,19 @@ function decisionSelectionSet(set, typeName, path, ctx) {
|
|
|
179
182
|
continue;
|
|
180
183
|
}
|
|
181
184
|
if (selection.kind === "InlineFragment") {
|
|
185
|
+
const condition = selection.typeCondition?.name.value ?? typeName;
|
|
182
186
|
const pruned = decisionSelectionSet(
|
|
183
187
|
selection.selectionSet,
|
|
184
|
-
|
|
188
|
+
condition,
|
|
185
189
|
path,
|
|
186
190
|
ctx
|
|
187
191
|
);
|
|
188
|
-
if (pruned)
|
|
192
|
+
if (!pruned) continue;
|
|
193
|
+
if (condition === typeName && !selection.directives?.length) {
|
|
194
|
+
selections.push(...pruned.selections);
|
|
195
|
+
} else {
|
|
196
|
+
selections.push({ ...selection, selectionSet: pruned });
|
|
197
|
+
}
|
|
189
198
|
continue;
|
|
190
199
|
}
|
|
191
200
|
if (!selection.selectionSet) continue;
|
|
@@ -259,20 +268,58 @@ function toDecisionDocument(document, schema, groupPaths, options) {
|
|
|
259
268
|
const used = /* @__PURE__ */ new Set();
|
|
260
269
|
collectVariables(pruned, used);
|
|
261
270
|
collectVariables(operation.directives, used);
|
|
271
|
+
const shared = sharedContextFragment(schema);
|
|
262
272
|
return {
|
|
263
273
|
kind: "Document",
|
|
264
274
|
definitions: [
|
|
275
|
+
...shared ? [shared.definition] : [],
|
|
265
276
|
{
|
|
266
277
|
...operation,
|
|
267
278
|
name: operation.name && name(`${operation.name.value}Decision`),
|
|
268
279
|
variableDefinitions: (operation.variableDefinitions ?? []).filter(
|
|
269
280
|
(definition) => used.has(definition.variable.name.value)
|
|
270
281
|
),
|
|
271
|
-
selectionSet: pruned
|
|
282
|
+
selectionSet: shared ? shared.apply(pruned) : pruned
|
|
272
283
|
}
|
|
273
284
|
]
|
|
274
285
|
};
|
|
275
286
|
}
|
|
287
|
+
function sharedContextFragment(schema) {
|
|
288
|
+
const contextTypes = new Set(
|
|
289
|
+
Object.values(schema.types).flatMap(
|
|
290
|
+
(type) => type.hasContext ? [type.fields._context.type] : []
|
|
291
|
+
)
|
|
292
|
+
);
|
|
293
|
+
if (contextTypes.size !== 1) return null;
|
|
294
|
+
const [contextType] = contextTypes;
|
|
295
|
+
const fragmentName = "c";
|
|
296
|
+
const definition = {
|
|
297
|
+
kind: "FragmentDefinition",
|
|
298
|
+
name: name(fragmentName),
|
|
299
|
+
typeCondition: { kind: "NamedType", name: name(contextType) },
|
|
300
|
+
selectionSet: selectionSet(
|
|
301
|
+
["kind", "group_id", "variant_id"].map(
|
|
302
|
+
(field) => ({ kind: "Field", name: name(field) })
|
|
303
|
+
)
|
|
304
|
+
)
|
|
305
|
+
};
|
|
306
|
+
const spread = {
|
|
307
|
+
kind: "FragmentSpread",
|
|
308
|
+
name: name(fragmentName)
|
|
309
|
+
};
|
|
310
|
+
const apply = (set) => selectionSet(
|
|
311
|
+
set.selections.map((selection) => {
|
|
312
|
+
if (selection.kind === "Field" && selection.alias?.value === PREPR_CONTEXT_ALIAS) {
|
|
313
|
+
return { ...selection, selectionSet: selectionSet([spread]) };
|
|
314
|
+
}
|
|
315
|
+
return "selectionSet" in selection && selection.selectionSet ? {
|
|
316
|
+
...selection,
|
|
317
|
+
selectionSet: apply(selection.selectionSet)
|
|
318
|
+
} : selection;
|
|
319
|
+
})
|
|
320
|
+
);
|
|
321
|
+
return { definition, apply };
|
|
322
|
+
}
|
|
276
323
|
|
|
277
324
|
// src/core/variants/resolve.ts
|
|
278
325
|
function personalizationPaths(data) {
|
package/dist/graphql/codegen.js
CHANGED
|
@@ -2,9 +2,9 @@ import {
|
|
|
2
2
|
toDecisionQuery,
|
|
3
3
|
toSchemaInfo,
|
|
4
4
|
toVariantsQuery
|
|
5
|
-
} from "../chunk-
|
|
6
|
-
import "../chunk-
|
|
7
|
-
import "../chunk-
|
|
5
|
+
} from "../chunk-PFJO4TYL.js";
|
|
6
|
+
import "../chunk-3QGWCYTL.js";
|
|
7
|
+
import "../chunk-VQIPTUN4.js";
|
|
8
8
|
|
|
9
9
|
// src/codegen/index.ts
|
|
10
10
|
function typeRef(type) {
|