@preprio/toolkit 0.5.0-rc.2 → 0.6.0-alpha.1
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 +415 -27
- package/dist/astro/chunk-O7N6GPAH.js +173 -0
- package/dist/astro/graphql.cjs +1030 -0
- package/dist/astro/graphql.d.cts +305 -0
- package/dist/astro/graphql.d.ts +305 -0
- package/dist/astro/graphql.js +936 -0
- package/dist/astro/index-XT_z-RYT.d.cts +130 -0
- package/dist/astro/index-XT_z-RYT.d.ts +130 -0
- package/dist/astro/index.cjs +14 -1
- package/dist/astro/index.d.cts +1 -120
- package/dist/astro/index.d.ts +1 -120
- package/dist/astro/index.js +15 -153
- package/dist/chunk-3JLWZQ37.js +179 -0
- package/dist/chunk-3NCEVJJ3.js +10 -0
- package/dist/chunk-S5RWVMDJ.js +148 -0
- package/dist/chunk-TBKSBXGH.js +452 -0
- package/dist/{chunk-FLPNN3CV.js → chunk-VI6QZTBM.js} +73 -276
- package/dist/chunk-YWIKKWBR.js +26 -0
- package/dist/graphql/codegen.cjs +510 -0
- package/dist/graphql/codegen.d.cts +23 -0
- package/dist/graphql/codegen.d.ts +23 -0
- package/dist/graphql/codegen.js +99 -0
- package/dist/graphql.cjs +941 -0
- package/dist/graphql.d.cts +305 -0
- package/dist/graphql.d.ts +305 -0
- package/dist/graphql.js +419 -0
- package/dist/index.cjs +76 -119
- package/dist/index.d.cts +12 -64
- package/dist/index.d.ts +12 -64
- package/dist/index.js +27 -167
- package/dist/nextjs/chunk-BQ6IGSH6.js +231 -0
- package/dist/nextjs/components.cjs +190 -122
- package/dist/nextjs/components.d.cts +33 -2
- package/dist/nextjs/components.d.ts +33 -2
- package/dist/nextjs/components.js +133 -4
- package/dist/nextjs/graphql.cjs +1362 -0
- package/dist/nextjs/graphql.d.cts +435 -0
- package/dist/nextjs/graphql.d.ts +435 -0
- package/dist/nextjs/graphql.js +1261 -0
- package/dist/nextjs/index.cjs +126 -6
- package/dist/nextjs/index.d.cts +53 -147
- package/dist/nextjs/index.d.ts +53 -147
- package/dist/nextjs/index.js +113 -203
- package/dist/nextjs/server-CjpELYvq.d.cts +156 -0
- package/dist/nextjs/server-CjpELYvq.d.ts +156 -0
- package/dist/nuxt/chunk-IQDSBPDN.js +417 -0
- package/dist/nuxt/graphql.cjs +1037 -0
- package/dist/nuxt/graphql.d.cts +302 -0
- package/dist/nuxt/graphql.d.ts +302 -0
- package/dist/nuxt/graphql.js +937 -0
- package/dist/nuxt/index.cjs +14 -1
- package/dist/nuxt/index.d.cts +3 -148
- package/dist/nuxt/index.d.ts +3 -148
- package/dist/nuxt/index.js +15 -391
- package/dist/nuxt/server-8v6OHbsw.d.cts +158 -0
- package/dist/nuxt/server-8v6OHbsw.d.ts +158 -0
- package/dist/pixel-DwW7cXJm.d.cts +49 -0
- package/dist/pixel-DwW7cXJm.d.ts +49 -0
- package/dist/react/components.cjs +88 -120
- package/dist/react/components.d.cts +2 -8
- package/dist/react/components.d.ts +2 -8
- package/dist/react/components.js +88 -120
- package/dist/server-Brr1UgtW.d.ts +59 -0
- package/dist/server-DJuS3YU0.d.cts +59 -0
- package/dist/sveltekit/chunk-OKYZ6HPH.js +413 -0
- package/dist/sveltekit/graphql.cjs +1043 -0
- package/dist/sveltekit/graphql.d.cts +307 -0
- package/dist/sveltekit/graphql.d.ts +307 -0
- package/dist/sveltekit/graphql.js +943 -0
- package/dist/sveltekit/index.cjs +14 -1
- package/dist/sveltekit/index.d.cts +3 -88
- package/dist/sveltekit/index.d.ts +3 -88
- package/dist/sveltekit/index.js +15 -387
- package/dist/sveltekit/server-CO847mEV.d.cts +98 -0
- package/dist/sveltekit/server-CO847mEV.d.ts +98 -0
- package/dist/{pixel-B3oQNbZ3.d.cts → types-BYH9XqmN.d.cts} +4 -58
- package/dist/{pixel-B3oQNbZ3.d.ts → types-BYH9XqmN.d.ts} +4 -58
- package/package.json +36 -1
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name2 in all)
|
|
8
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/codegen/index.ts
|
|
21
|
+
var codegen_exports = {};
|
|
22
|
+
__export(codegen_exports, {
|
|
23
|
+
plugin: () => plugin
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(codegen_exports);
|
|
26
|
+
|
|
27
|
+
// src/core/graphql/document.ts
|
|
28
|
+
var import_printer = require("graphql/language/printer.js");
|
|
29
|
+
var import_stripIgnoredCharacters = require("graphql/utilities/stripIgnoredCharacters.js");
|
|
30
|
+
|
|
31
|
+
// src/core/server.ts
|
|
32
|
+
var PreprError = class extends Error {
|
|
33
|
+
constructor(message, code, context, originalError) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.code = code;
|
|
36
|
+
this.context = context;
|
|
37
|
+
this.originalError = originalError;
|
|
38
|
+
this.name = "PreprError";
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/core/graphql/document.ts
|
|
43
|
+
var printed = /* @__PURE__ */ new WeakMap();
|
|
44
|
+
function isDocumentNode(value) {
|
|
45
|
+
return typeof value === "object" && value !== null && value.kind === "Document" && Array.isArray(value.definitions);
|
|
46
|
+
}
|
|
47
|
+
function invalidDocument(originalError) {
|
|
48
|
+
return new PreprError(
|
|
49
|
+
"Expected a GraphQL query string, TypedDocumentString or DocumentNode",
|
|
50
|
+
"INVALID_DOCUMENT",
|
|
51
|
+
"createPreprClient",
|
|
52
|
+
originalError
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
var minified = /* @__PURE__ */ new Map();
|
|
56
|
+
var MINIFIED_CACHE_LIMIT = 500;
|
|
57
|
+
function minify(query) {
|
|
58
|
+
const cached = minified.get(query);
|
|
59
|
+
if (cached !== void 0) return cached;
|
|
60
|
+
let result = query;
|
|
61
|
+
try {
|
|
62
|
+
result = (0, import_stripIgnoredCharacters.stripIgnoredCharacters)(query);
|
|
63
|
+
} catch {
|
|
64
|
+
}
|
|
65
|
+
if (minified.size < MINIFIED_CACHE_LIMIT) minified.set(query, result);
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
function resolveQuery(document) {
|
|
69
|
+
if (typeof document === "string") return minify(document);
|
|
70
|
+
if (document instanceof String) return minify(document.toString());
|
|
71
|
+
if (!isDocumentNode(document)) throw invalidDocument();
|
|
72
|
+
let query = printed.get(document);
|
|
73
|
+
if (query === void 0) {
|
|
74
|
+
try {
|
|
75
|
+
query = minify((0, import_printer.print)(document));
|
|
76
|
+
} catch (error) {
|
|
77
|
+
throw invalidDocument(error instanceof Error ? error : void 0);
|
|
78
|
+
}
|
|
79
|
+
printed.set(document, query);
|
|
80
|
+
}
|
|
81
|
+
return query;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/core/variants/ast.ts
|
|
85
|
+
var import_parser = require("graphql/language/parser.js");
|
|
86
|
+
|
|
87
|
+
// src/core/variants/constants.ts
|
|
88
|
+
var PREPR_CONTEXT_ALIAS = "_preprContext";
|
|
89
|
+
var PREPR_TYPE_ALIAS = "_preprType";
|
|
90
|
+
|
|
91
|
+
// src/core/variants/ast.ts
|
|
92
|
+
var name = (value) => ({ kind: "Name", value });
|
|
93
|
+
function selectionSet(selections) {
|
|
94
|
+
return { kind: "SelectionSet", selections };
|
|
95
|
+
}
|
|
96
|
+
function contextField(withKey) {
|
|
97
|
+
const fields = ["kind", "group_id", "variant_id"];
|
|
98
|
+
if (withKey) fields.push("variant_key");
|
|
99
|
+
return {
|
|
100
|
+
kind: "Field",
|
|
101
|
+
alias: name(PREPR_CONTEXT_ALIAS),
|
|
102
|
+
name: name("_context"),
|
|
103
|
+
selectionSet: selectionSet(
|
|
104
|
+
fields.map(
|
|
105
|
+
(field) => ({ kind: "Field", name: name(field) })
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function typenameField() {
|
|
111
|
+
return {
|
|
112
|
+
kind: "Field",
|
|
113
|
+
alias: name(PREPR_TYPE_ALIAS),
|
|
114
|
+
name: name("__typename")
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function inlineFragment(typeName, selections) {
|
|
118
|
+
return {
|
|
119
|
+
kind: "InlineFragment",
|
|
120
|
+
typeCondition: { kind: "NamedType", name: name(typeName) },
|
|
121
|
+
selectionSet: selectionSet(selections)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function contextSelections(typeName, schema, onlyTypes, withType = true) {
|
|
125
|
+
const fields = () => withType ? [contextField(true), typenameField()] : [contextField(false)];
|
|
126
|
+
const type = schema.types[typeName];
|
|
127
|
+
if (!type) return [];
|
|
128
|
+
if (type.possibleTypes.length > 0) {
|
|
129
|
+
return type.possibleTypes.filter(
|
|
130
|
+
(member) => schema.types[member]?.hasContext && (!onlyTypes || onlyTypes.has(member))
|
|
131
|
+
).map((member) => inlineFragment(member, fields()));
|
|
132
|
+
}
|
|
133
|
+
return type.hasContext ? fields() : [];
|
|
134
|
+
}
|
|
135
|
+
function withoutPersonalizeArg(args = []) {
|
|
136
|
+
return args.filter((arg) => arg.name.value !== "personalize");
|
|
137
|
+
}
|
|
138
|
+
function documentOf(document) {
|
|
139
|
+
if (typeof document === "string") return (0, import_parser.parse)(document);
|
|
140
|
+
if (document instanceof String) return (0, import_parser.parse)(document.toString());
|
|
141
|
+
return document;
|
|
142
|
+
}
|
|
143
|
+
function collectVariables(node, used) {
|
|
144
|
+
if (!node || typeof node !== "object") return;
|
|
145
|
+
if (Array.isArray(node)) {
|
|
146
|
+
for (const item of node) collectVariables(item, used);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const value = node;
|
|
150
|
+
if (value.kind === "Variable") {
|
|
151
|
+
used.add(value.name.value);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
for (const key of Object.keys(value)) {
|
|
155
|
+
if (key !== "loc") collectVariables(value[key], used);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/core/variants/decision-document.ts
|
|
160
|
+
function decisionSelectionSet(set, typeName, path, ctx) {
|
|
161
|
+
const selections = [];
|
|
162
|
+
for (const selection of set.selections) {
|
|
163
|
+
if (selection.kind === "FragmentSpread") {
|
|
164
|
+
const fragment = ctx.fragments[selection.name.value];
|
|
165
|
+
if (!fragment) continue;
|
|
166
|
+
const pruned = decisionSelectionSet(
|
|
167
|
+
fragment.selectionSet,
|
|
168
|
+
fragment.typeCondition.name.value,
|
|
169
|
+
path,
|
|
170
|
+
ctx
|
|
171
|
+
);
|
|
172
|
+
if (pruned) {
|
|
173
|
+
selections.push(
|
|
174
|
+
inlineFragment(fragment.typeCondition.name.value, [
|
|
175
|
+
...pruned.selections
|
|
176
|
+
])
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (selection.kind === "InlineFragment") {
|
|
182
|
+
const pruned = decisionSelectionSet(
|
|
183
|
+
selection.selectionSet,
|
|
184
|
+
selection.typeCondition?.name.value ?? typeName,
|
|
185
|
+
path,
|
|
186
|
+
ctx
|
|
187
|
+
);
|
|
188
|
+
if (pruned) selections.push({ ...selection, selectionSet: pruned });
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (!selection.selectionSet) continue;
|
|
192
|
+
const field = ctx.schema.types[typeName]?.fields[selection.name.value];
|
|
193
|
+
if (!field) continue;
|
|
194
|
+
const key = selection.alias?.value ?? selection.name.value;
|
|
195
|
+
const fieldPath = path ? `${path}.${key}` : key;
|
|
196
|
+
if (ctx.paths && !ctx.paths.prefixes.has(fieldPath)) continue;
|
|
197
|
+
const child = decisionSelectionSet(
|
|
198
|
+
selection.selectionSet,
|
|
199
|
+
field.type,
|
|
200
|
+
fieldPath,
|
|
201
|
+
ctx
|
|
202
|
+
);
|
|
203
|
+
const groupTypes = ctx.paths?.groups.get(fieldPath);
|
|
204
|
+
const holdsGroups = field.personalizable && (!ctx.paths || groupTypes !== void 0);
|
|
205
|
+
if (holdsGroups) {
|
|
206
|
+
selections.push({
|
|
207
|
+
...selection,
|
|
208
|
+
arguments: withoutPersonalizeArg(selection.arguments),
|
|
209
|
+
selectionSet: selectionSet([
|
|
210
|
+
...ctx.topLevelOnly ? [] : child?.selections ?? [],
|
|
211
|
+
...contextSelections(
|
|
212
|
+
field.type,
|
|
213
|
+
ctx.schema,
|
|
214
|
+
groupTypes && !groupTypes.has("*") ? groupTypes : void 0,
|
|
215
|
+
false
|
|
216
|
+
)
|
|
217
|
+
])
|
|
218
|
+
});
|
|
219
|
+
} else if (child) {
|
|
220
|
+
selections.push({
|
|
221
|
+
...selection,
|
|
222
|
+
arguments: field.personalizable ? withoutPersonalizeArg(selection.arguments) : selection.arguments,
|
|
223
|
+
selectionSet: child
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return selections.length > 0 ? selectionSet(selections) : null;
|
|
228
|
+
}
|
|
229
|
+
function toDecisionDocument(document, schema, groupPaths, options) {
|
|
230
|
+
const doc = documentOf(document);
|
|
231
|
+
const fragments = {};
|
|
232
|
+
for (const definition of doc.definitions) {
|
|
233
|
+
if (definition.kind === "FragmentDefinition") {
|
|
234
|
+
fragments[definition.name.value] = definition;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const operation = doc.definitions.find(
|
|
238
|
+
(definition) => definition.kind === "OperationDefinition"
|
|
239
|
+
);
|
|
240
|
+
if (!operation) return null;
|
|
241
|
+
let paths;
|
|
242
|
+
if (groupPaths) {
|
|
243
|
+
const prefixes = /* @__PURE__ */ new Set();
|
|
244
|
+
for (const groupPath of groupPaths.keys()) {
|
|
245
|
+
const parts = groupPath.split(".");
|
|
246
|
+
for (let i = 1; i <= parts.length; i++) {
|
|
247
|
+
prefixes.add(parts.slice(0, i).join("."));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
paths = { groups: groupPaths, prefixes };
|
|
251
|
+
}
|
|
252
|
+
const pruned = decisionSelectionSet(
|
|
253
|
+
operation.selectionSet,
|
|
254
|
+
schema.queryType,
|
|
255
|
+
"",
|
|
256
|
+
{ schema, fragments, paths, topLevelOnly: options?.topLevelOnly }
|
|
257
|
+
);
|
|
258
|
+
if (!pruned) return null;
|
|
259
|
+
const used = /* @__PURE__ */ new Set();
|
|
260
|
+
collectVariables(pruned, used);
|
|
261
|
+
collectVariables(operation.directives, used);
|
|
262
|
+
return {
|
|
263
|
+
kind: "Document",
|
|
264
|
+
definitions: [
|
|
265
|
+
{
|
|
266
|
+
...operation,
|
|
267
|
+
name: operation.name && name(`${operation.name.value}Decision`),
|
|
268
|
+
variableDefinitions: (operation.variableDefinitions ?? []).filter(
|
|
269
|
+
(definition) => used.has(definition.variable.name.value)
|
|
270
|
+
),
|
|
271
|
+
selectionSet: pruned
|
|
272
|
+
}
|
|
273
|
+
]
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// src/core/variants/resolve.ts
|
|
278
|
+
function personalizationPaths(data) {
|
|
279
|
+
const paths = /* @__PURE__ */ new Map();
|
|
280
|
+
const walk = (value, path) => {
|
|
281
|
+
if (Array.isArray(value)) {
|
|
282
|
+
for (const item of value) {
|
|
283
|
+
if (contextOf(item)?.kind === "PERSONALIZATION") {
|
|
284
|
+
const record = item;
|
|
285
|
+
const type = record[PREPR_TYPE_ALIAS] ?? record.__typename;
|
|
286
|
+
const types = paths.get(path) ?? /* @__PURE__ */ new Set();
|
|
287
|
+
types.add(typeof type === "string" ? type : "*");
|
|
288
|
+
paths.set(path, types);
|
|
289
|
+
}
|
|
290
|
+
walk(item, path);
|
|
291
|
+
}
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (!value || typeof value !== "object") return;
|
|
295
|
+
for (const [key, child] of Object.entries(value)) {
|
|
296
|
+
if (key !== PREPR_CONTEXT_ALIAS && key !== PREPR_TYPE_ALIAS)
|
|
297
|
+
walk(child, path ? `${path}.${key}` : key);
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
walk(data, "");
|
|
301
|
+
return paths;
|
|
302
|
+
}
|
|
303
|
+
function contextOf(item) {
|
|
304
|
+
if (!item || typeof item !== "object") return null;
|
|
305
|
+
const context = item[PREPR_CONTEXT_ALIAS];
|
|
306
|
+
const ctx = context;
|
|
307
|
+
return ctx?.kind && ctx.group_id ? ctx : null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// src/core/variants/variants-document.ts
|
|
311
|
+
function variantsSelectionSet(set, typeName, schema) {
|
|
312
|
+
const selections = set.selections.map((selection) => {
|
|
313
|
+
if (selection.kind === "InlineFragment") {
|
|
314
|
+
return {
|
|
315
|
+
...selection,
|
|
316
|
+
selectionSet: variantsSelectionSet(
|
|
317
|
+
selection.selectionSet,
|
|
318
|
+
selection.typeCondition?.name.value ?? typeName,
|
|
319
|
+
schema
|
|
320
|
+
)
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
if (selection.kind !== "Field" || !selection.selectionSet) return selection;
|
|
324
|
+
const field = schema.types[typeName]?.fields[selection.name.value];
|
|
325
|
+
if (!field) return selection;
|
|
326
|
+
const child = variantsSelectionSet(
|
|
327
|
+
selection.selectionSet,
|
|
328
|
+
field.type,
|
|
329
|
+
schema
|
|
330
|
+
);
|
|
331
|
+
if (!field.personalizable) return { ...selection, selectionSet: child };
|
|
332
|
+
return {
|
|
333
|
+
...selection,
|
|
334
|
+
arguments: [
|
|
335
|
+
...withoutPersonalizeArg(selection.arguments),
|
|
336
|
+
{
|
|
337
|
+
kind: "Argument",
|
|
338
|
+
name: name("personalize"),
|
|
339
|
+
value: { kind: "BooleanValue", value: false }
|
|
340
|
+
}
|
|
341
|
+
],
|
|
342
|
+
selectionSet: selectionSet([
|
|
343
|
+
...child.selections,
|
|
344
|
+
...contextSelections(field.type, schema)
|
|
345
|
+
])
|
|
346
|
+
};
|
|
347
|
+
});
|
|
348
|
+
return { ...set, selections };
|
|
349
|
+
}
|
|
350
|
+
function toVariantsDocument(document, schema) {
|
|
351
|
+
const doc = documentOf(document);
|
|
352
|
+
return {
|
|
353
|
+
...doc,
|
|
354
|
+
definitions: doc.definitions.map((definition) => {
|
|
355
|
+
if (definition.kind === "OperationDefinition") {
|
|
356
|
+
return {
|
|
357
|
+
...definition,
|
|
358
|
+
selectionSet: variantsSelectionSet(
|
|
359
|
+
definition.selectionSet,
|
|
360
|
+
schema.queryType,
|
|
361
|
+
schema
|
|
362
|
+
)
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
if (definition.kind === "FragmentDefinition") {
|
|
366
|
+
return {
|
|
367
|
+
...definition,
|
|
368
|
+
selectionSet: variantsSelectionSet(
|
|
369
|
+
definition.selectionSet,
|
|
370
|
+
definition.typeCondition.name.value,
|
|
371
|
+
schema
|
|
372
|
+
)
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
return definition;
|
|
376
|
+
})
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/core/variants/queries.ts
|
|
381
|
+
function toVariantsQuery(document, schema) {
|
|
382
|
+
return resolveQuery(toVariantsDocument(document, schema));
|
|
383
|
+
}
|
|
384
|
+
function toDecisionQuery(document, schema, options = {}) {
|
|
385
|
+
const paths = options.from === void 0 ? void 0 : personalizationPaths(options.from);
|
|
386
|
+
if (paths && paths.size === 0) return null;
|
|
387
|
+
const decision = toDecisionDocument(document, schema, paths, {
|
|
388
|
+
topLevelOnly: !paths && options.topLevelOnly
|
|
389
|
+
});
|
|
390
|
+
return decision ? resolveQuery(decision) : null;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// src/core/variants/schema.ts
|
|
394
|
+
function namedType(ref) {
|
|
395
|
+
let current = ref;
|
|
396
|
+
while (current && !current.name) current = current.ofType;
|
|
397
|
+
return current?.name ?? "";
|
|
398
|
+
}
|
|
399
|
+
function toSchemaInfo(introspection) {
|
|
400
|
+
const types = {};
|
|
401
|
+
for (const type of introspection.__schema.types) {
|
|
402
|
+
if (type.name.startsWith("__")) continue;
|
|
403
|
+
const fields = {};
|
|
404
|
+
for (const field of type.fields ?? []) {
|
|
405
|
+
fields[field.name] = {
|
|
406
|
+
type: namedType(field.type),
|
|
407
|
+
personalizable: field.args.some((arg) => arg.name === "personalize")
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
types[type.name] = {
|
|
411
|
+
fields,
|
|
412
|
+
possibleTypes: (type.possibleTypes ?? []).map((t) => t.name),
|
|
413
|
+
hasContext: "_context" in fields
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
return { queryType: introspection.__schema.queryType.name, types };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// src/codegen/index.ts
|
|
420
|
+
function typeRef(type) {
|
|
421
|
+
const value = type;
|
|
422
|
+
return value.ofType ? { name: null, ofType: typeRef(value.ofType) } : { name: value.name ?? null };
|
|
423
|
+
}
|
|
424
|
+
function introspect(schema) {
|
|
425
|
+
const types = Object.values(
|
|
426
|
+
schema.getTypeMap()
|
|
427
|
+
).map((type) => {
|
|
428
|
+
const possible = "getTypes" in type || "getInterfaces" in type ? schema.getPossibleTypes(type) : [];
|
|
429
|
+
return {
|
|
430
|
+
name: type.name,
|
|
431
|
+
possibleTypes: possible.length ? possible.map((member) => ({ name: member.name })) : null,
|
|
432
|
+
fields: type.getFields ? Object.values(type.getFields()).map((field) => ({
|
|
433
|
+
name: field.name,
|
|
434
|
+
args: (field.args ?? []).map((arg) => ({ name: arg.name })),
|
|
435
|
+
type: typeRef(field.type)
|
|
436
|
+
})) : null
|
|
437
|
+
};
|
|
438
|
+
});
|
|
439
|
+
return {
|
|
440
|
+
__schema: {
|
|
441
|
+
queryType: { name: schema.getQueryType()?.name ?? "Query" },
|
|
442
|
+
types
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
function spreadNames(node, into) {
|
|
447
|
+
if (Array.isArray(node)) {
|
|
448
|
+
for (const child of node) spreadNames(child, into);
|
|
449
|
+
} else if (node && typeof node === "object") {
|
|
450
|
+
const value = node;
|
|
451
|
+
if (value.kind === "FragmentSpread" && value.name) {
|
|
452
|
+
into.add(value.name.value);
|
|
453
|
+
}
|
|
454
|
+
for (const [key, child] of Object.entries(node)) {
|
|
455
|
+
if (key !== "loc") spreadNames(child, into);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return into;
|
|
459
|
+
}
|
|
460
|
+
function plugin(schema, documents, _config) {
|
|
461
|
+
const info = toSchemaInfo(introspect(schema));
|
|
462
|
+
const definitions = documents.flatMap((source) => [
|
|
463
|
+
...source.document?.definitions ?? []
|
|
464
|
+
]);
|
|
465
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
466
|
+
for (const definition of definitions) {
|
|
467
|
+
if (definition.kind === "FragmentDefinition") {
|
|
468
|
+
fragments.set(definition.name.value, definition);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
const lines = [
|
|
472
|
+
"// Generated by @preprio/toolkit/graphql/codegen. Do not edit."
|
|
473
|
+
];
|
|
474
|
+
const seen = /* @__PURE__ */ new Set();
|
|
475
|
+
for (const definition of definitions) {
|
|
476
|
+
if (definition.kind !== "OperationDefinition") continue;
|
|
477
|
+
const operation = definition;
|
|
478
|
+
if (operation.operation !== "query" || !operation.name) continue;
|
|
479
|
+
const name2 = operation.name.value;
|
|
480
|
+
if (seen.has(name2)) {
|
|
481
|
+
throw new Error(
|
|
482
|
+
`@preprio/toolkit codegen: two queries are named "${name2}"; rename one so their exports do not collide.`
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
seen.add(name2);
|
|
486
|
+
const used = /* @__PURE__ */ new Set();
|
|
487
|
+
const queue = [...spreadNames(operation, /* @__PURE__ */ new Set())];
|
|
488
|
+
while (queue.length) {
|
|
489
|
+
const next = queue.pop();
|
|
490
|
+
if (used.has(next) || !fragments.has(next)) continue;
|
|
491
|
+
used.add(next);
|
|
492
|
+
queue.push(...spreadNames(fragments.get(next), /* @__PURE__ */ new Set()));
|
|
493
|
+
}
|
|
494
|
+
const document = {
|
|
495
|
+
kind: "Document",
|
|
496
|
+
definitions: [operation, ...[...used].map((n) => fragments.get(n))]
|
|
497
|
+
};
|
|
498
|
+
const decision = toDecisionQuery(document, info);
|
|
499
|
+
if (!decision) continue;
|
|
500
|
+
lines.push(
|
|
501
|
+
`export const ${name2}VariantsQuery = ${JSON.stringify(toVariantsQuery(document, info))};`,
|
|
502
|
+
`export const ${name2}DecisionQuery = ${JSON.stringify(decision)};`
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
return lines.join("\n") + "\n";
|
|
506
|
+
}
|
|
507
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
508
|
+
0 && (module.exports = {
|
|
509
|
+
plugin
|
|
510
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GraphQLSchema, DocumentNode } from 'graphql';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* graphql-codegen plugin: for every named query that touches personalizable
|
|
5
|
+
* fields, writes the variants query and the decision query as string
|
|
6
|
+
* constants, `<Name>VariantsQuery` and `<Name>DecisionQuery`. For sites that
|
|
7
|
+
* send queries with their own client and want the rewrites done at build
|
|
8
|
+
* time instead of loading the schema at runtime.
|
|
9
|
+
*
|
|
10
|
+
* The decision query asks about every personalizable list of the query. When
|
|
11
|
+
* the variants result is at hand at runtime, `toDecisionQuery(query, schema,
|
|
12
|
+
* { from })` is smaller and faster for visitors Prepr has not seen before.
|
|
13
|
+
*
|
|
14
|
+
* Register it in codegen.ts: `plugins: ['@preprio/toolkit/graphql/codegen']`.
|
|
15
|
+
*
|
|
16
|
+
* @throws Error when two queries share a name, since their exports would
|
|
17
|
+
* collide.
|
|
18
|
+
*/
|
|
19
|
+
declare function plugin(schema: GraphQLSchema, documents: {
|
|
20
|
+
document?: DocumentNode;
|
|
21
|
+
}[], _config?: Record<string, never>): string;
|
|
22
|
+
|
|
23
|
+
export { plugin };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GraphQLSchema, DocumentNode } from 'graphql';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* graphql-codegen plugin: for every named query that touches personalizable
|
|
5
|
+
* fields, writes the variants query and the decision query as string
|
|
6
|
+
* constants, `<Name>VariantsQuery` and `<Name>DecisionQuery`. For sites that
|
|
7
|
+
* send queries with their own client and want the rewrites done at build
|
|
8
|
+
* time instead of loading the schema at runtime.
|
|
9
|
+
*
|
|
10
|
+
* The decision query asks about every personalizable list of the query. When
|
|
11
|
+
* the variants result is at hand at runtime, `toDecisionQuery(query, schema,
|
|
12
|
+
* { from })` is smaller and faster for visitors Prepr has not seen before.
|
|
13
|
+
*
|
|
14
|
+
* Register it in codegen.ts: `plugins: ['@preprio/toolkit/graphql/codegen']`.
|
|
15
|
+
*
|
|
16
|
+
* @throws Error when two queries share a name, since their exports would
|
|
17
|
+
* collide.
|
|
18
|
+
*/
|
|
19
|
+
declare function plugin(schema: GraphQLSchema, documents: {
|
|
20
|
+
document?: DocumentNode;
|
|
21
|
+
}[], _config?: Record<string, never>): string;
|
|
22
|
+
|
|
23
|
+
export { plugin };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {
|
|
2
|
+
toDecisionQuery,
|
|
3
|
+
toSchemaInfo,
|
|
4
|
+
toVariantsQuery
|
|
5
|
+
} from "../chunk-TBKSBXGH.js";
|
|
6
|
+
import "../chunk-3JLWZQ37.js";
|
|
7
|
+
import "../chunk-YWIKKWBR.js";
|
|
8
|
+
|
|
9
|
+
// src/codegen/index.ts
|
|
10
|
+
function typeRef(type) {
|
|
11
|
+
const value = type;
|
|
12
|
+
return value.ofType ? { name: null, ofType: typeRef(value.ofType) } : { name: value.name ?? null };
|
|
13
|
+
}
|
|
14
|
+
function introspect(schema) {
|
|
15
|
+
const types = Object.values(
|
|
16
|
+
schema.getTypeMap()
|
|
17
|
+
).map((type) => {
|
|
18
|
+
const possible = "getTypes" in type || "getInterfaces" in type ? schema.getPossibleTypes(type) : [];
|
|
19
|
+
return {
|
|
20
|
+
name: type.name,
|
|
21
|
+
possibleTypes: possible.length ? possible.map((member) => ({ name: member.name })) : null,
|
|
22
|
+
fields: type.getFields ? Object.values(type.getFields()).map((field) => ({
|
|
23
|
+
name: field.name,
|
|
24
|
+
args: (field.args ?? []).map((arg) => ({ name: arg.name })),
|
|
25
|
+
type: typeRef(field.type)
|
|
26
|
+
})) : null
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
return {
|
|
30
|
+
__schema: {
|
|
31
|
+
queryType: { name: schema.getQueryType()?.name ?? "Query" },
|
|
32
|
+
types
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function spreadNames(node, into) {
|
|
37
|
+
if (Array.isArray(node)) {
|
|
38
|
+
for (const child of node) spreadNames(child, into);
|
|
39
|
+
} else if (node && typeof node === "object") {
|
|
40
|
+
const value = node;
|
|
41
|
+
if (value.kind === "FragmentSpread" && value.name) {
|
|
42
|
+
into.add(value.name.value);
|
|
43
|
+
}
|
|
44
|
+
for (const [key, child] of Object.entries(node)) {
|
|
45
|
+
if (key !== "loc") spreadNames(child, into);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return into;
|
|
49
|
+
}
|
|
50
|
+
function plugin(schema, documents, _config) {
|
|
51
|
+
const info = toSchemaInfo(introspect(schema));
|
|
52
|
+
const definitions = documents.flatMap((source) => [
|
|
53
|
+
...source.document?.definitions ?? []
|
|
54
|
+
]);
|
|
55
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
56
|
+
for (const definition of definitions) {
|
|
57
|
+
if (definition.kind === "FragmentDefinition") {
|
|
58
|
+
fragments.set(definition.name.value, definition);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const lines = [
|
|
62
|
+
"// Generated by @preprio/toolkit/graphql/codegen. Do not edit."
|
|
63
|
+
];
|
|
64
|
+
const seen = /* @__PURE__ */ new Set();
|
|
65
|
+
for (const definition of definitions) {
|
|
66
|
+
if (definition.kind !== "OperationDefinition") continue;
|
|
67
|
+
const operation = definition;
|
|
68
|
+
if (operation.operation !== "query" || !operation.name) continue;
|
|
69
|
+
const name = operation.name.value;
|
|
70
|
+
if (seen.has(name)) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`@preprio/toolkit codegen: two queries are named "${name}"; rename one so their exports do not collide.`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
seen.add(name);
|
|
76
|
+
const used = /* @__PURE__ */ new Set();
|
|
77
|
+
const queue = [...spreadNames(operation, /* @__PURE__ */ new Set())];
|
|
78
|
+
while (queue.length) {
|
|
79
|
+
const next = queue.pop();
|
|
80
|
+
if (used.has(next) || !fragments.has(next)) continue;
|
|
81
|
+
used.add(next);
|
|
82
|
+
queue.push(...spreadNames(fragments.get(next), /* @__PURE__ */ new Set()));
|
|
83
|
+
}
|
|
84
|
+
const document = {
|
|
85
|
+
kind: "Document",
|
|
86
|
+
definitions: [operation, ...[...used].map((n) => fragments.get(n))]
|
|
87
|
+
};
|
|
88
|
+
const decision = toDecisionQuery(document, info);
|
|
89
|
+
if (!decision) continue;
|
|
90
|
+
lines.push(
|
|
91
|
+
`export const ${name}VariantsQuery = ${JSON.stringify(toVariantsQuery(document, info))};`,
|
|
92
|
+
`export const ${name}DecisionQuery = ${JSON.stringify(decision)};`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return lines.join("\n") + "\n";
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
plugin
|
|
99
|
+
};
|