@remkoj/optimizely-graph-functions 2.0.3 → 2.1.0
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 +137 -99
- package/dist/documents/fragments.cms12.d.ts +2 -0
- package/dist/documents/fragments.cms12.js +142 -0
- package/dist/documents/fragments.cms12.js.map +1 -0
- package/dist/documents/fragments.cms13.d.ts +2 -0
- package/dist/documents/fragments.cms13.js +85 -0
- package/dist/documents/fragments.cms13.js.map +1 -0
- package/dist/documents/index.d.ts +18 -0
- package/dist/documents/index.js +27 -0
- package/dist/documents/index.js.map +1 -0
- package/dist/documents/queries.cms12.d.ts +2 -0
- package/dist/documents/queries.cms12.js +66 -0
- package/dist/documents/queries.cms12.js.map +1 -0
- package/dist/documents/queries.cms13.d.ts +2 -0
- package/dist/documents/queries.cms13.js +53 -0
- package/dist/documents/queries.cms13.js.map +1 -0
- package/dist/embedded-loader.d.ts +18 -0
- package/dist/embedded-loader.js +49 -0
- package/dist/embedded-loader.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/preset.d.ts +1 -1
- package/dist/preset.js +70 -13
- package/dist/preset.js.map +1 -1
- package/dist/transform.d.ts +1 -6
- package/dist/transform.js +68 -90
- package/dist/transform.js.map +1 -1
- package/dist/types.d.ts +20 -0
- package/package.json +12 -10
- package/dist/documents.d.ts +0 -4
- package/dist/documents.js +0 -138
- package/dist/documents.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queries.cms13.js","sourceRoot":"","sources":["../../src/documents/queries.cms13.ts"],"names":[],"mappings":";;AAAA,kBAAe;IACX;;;;;;;;;;;;;;;;MAgBE;IACF;;;;;;;;;;;;MAYE;IACF;;;;;;;;;;;;;;;;;MAiBE;CACL,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
type LoaderConfig = {
|
|
3
|
+
pluginContext?: {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
};
|
|
6
|
+
};
|
|
7
|
+
type LoaderFunction = (documentUri: string, config: LoaderConfig) => Promise<Types.DocumentFile | undefined | void>;
|
|
8
|
+
/**
|
|
9
|
+
* Custom loader for embedded Optimizely CMS 12/13 fragments & queries. It
|
|
10
|
+
* requires the document to be specified as an supported URI.
|
|
11
|
+
*
|
|
12
|
+
* `opti-cms:/(fragments|queries)/(12|13)`
|
|
13
|
+
*
|
|
14
|
+
* @param documentUri The identifier of the embedded fragments/queries to load
|
|
15
|
+
* @param config The configuration
|
|
16
|
+
*/
|
|
17
|
+
declare const EmbeddedLoader: LoaderFunction;
|
|
18
|
+
export default EmbeddedLoader;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const graphql_1 = require("graphql");
|
|
4
|
+
/**
|
|
5
|
+
* Custom loader for embedded Optimizely CMS 12/13 fragments & queries. It
|
|
6
|
+
* requires the document to be specified as an supported URI.
|
|
7
|
+
*
|
|
8
|
+
* `opti-cms:/(fragments|queries)/(12|13)`
|
|
9
|
+
*
|
|
10
|
+
* @param documentUri The identifier of the embedded fragments/queries to load
|
|
11
|
+
* @param config The configuration
|
|
12
|
+
*/
|
|
13
|
+
const EmbeddedLoader = async (documentUri, config) => {
|
|
14
|
+
const docId = new URL(documentUri);
|
|
15
|
+
if (docId.protocol != "opti-cms:")
|
|
16
|
+
throw new Error("[Optimizely Graph Functions - Embedded Documents] Unsupported protocol, only the \"opti-cms:\" protocol is supported");
|
|
17
|
+
let rawData;
|
|
18
|
+
switch (docId.pathname) {
|
|
19
|
+
case "/fragments/13":
|
|
20
|
+
rawData = require("./documents/fragments.cms13").default;
|
|
21
|
+
break;
|
|
22
|
+
case "/queries/13":
|
|
23
|
+
rawData = require("./documents/queries.cms13").default;
|
|
24
|
+
break;
|
|
25
|
+
case "/fragments/12":
|
|
26
|
+
rawData = require("./documents/fragments.cms12").default;
|
|
27
|
+
break;
|
|
28
|
+
case "/queries/12":
|
|
29
|
+
rawData = require("./documents/queries.cms12").default;
|
|
30
|
+
break;
|
|
31
|
+
default:
|
|
32
|
+
throw new Error(`[Optimizely Graph Functions - Embedded Documents] Path not found: ${docId.pathname}`);
|
|
33
|
+
}
|
|
34
|
+
return generateDocument(rawData, documentUri);
|
|
35
|
+
};
|
|
36
|
+
function generateDocument(rawData, location) {
|
|
37
|
+
if (rawData == undefined)
|
|
38
|
+
return undefined;
|
|
39
|
+
const rawSDL = Array.isArray(rawData) ? rawData.join("\n\n") : rawData;
|
|
40
|
+
const document = (0, graphql_1.parse)(rawSDL);
|
|
41
|
+
return {
|
|
42
|
+
document,
|
|
43
|
+
location,
|
|
44
|
+
rawSDL,
|
|
45
|
+
hash: location
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
exports.default = EmbeddedLoader;
|
|
49
|
+
//# sourceMappingURL=embedded-loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedded-loader.js","sourceRoot":"","sources":["../src/embedded-loader.ts"],"names":[],"mappings":";;AACA,qCAA+B;AAU/B;;;;;;;;GAQG;AACH,MAAM,cAAc,GAAoB,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,EAAE;IAClE,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,WAAW,CAAC,CAAA;IAClC,IAAI,KAAK,CAAC,QAAQ,IAAI,WAAW;QAC7B,MAAM,IAAI,KAAK,CAAC,sHAAsH,CAAC,CAAA;IAE3I,IAAI,OAAuC,CAAA;IAC3C,QAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;QACpB,KAAK,eAAe;YAChB,OAAO,GAAG,OAAO,CAAC,6BAA6B,CAAC,CAAC,OAAO,CAAA;YACxD,MAAK;QAET,KAAK,aAAa;YACd,OAAO,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAAC,OAAO,CAAA;YACtD,MAAK;QAET,KAAK,eAAe;YAChB,OAAO,GAAG,OAAO,CAAC,6BAA6B,CAAC,CAAC,OAAO,CAAA;YACxD,MAAK;QAET,KAAK,aAAa;YACd,OAAO,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAAC,OAAO,CAAA;YACtD,MAAK;QAET;YACI,MAAM,IAAI,KAAK,CAAC,qEAAsE,KAAK,CAAC,QAAS,EAAE,CAAC,CAAA;IAChH,CAAC;IAED,OAAO,gBAAgB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;AACjD,CAAC,CAAA;AAED,SAAS,gBAAgB,CAAC,OAAsC,EAAE,QAAiB;IAE/E,IAAI,OAAO,IAAI,SAAS;QACpB,OAAO,SAAS,CAAA;IACpB,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAA;IACtE,MAAM,QAAQ,GAAG,IAAA,eAAK,EAAC,MAAM,CAAC,CAAA;IAE9B,OAAO;QACH,QAAQ;QACR,QAAQ;QACR,MAAM;QACN,IAAI,EAAE,QAAQ;KACjB,CAAA;AACL,CAAC;AAED,kBAAe,cAAc,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export type PluginOptions = {
|
|
|
4
4
|
prettyPrintQuery?: boolean;
|
|
5
5
|
clientPath?: string;
|
|
6
6
|
};
|
|
7
|
+
export declare const DefaultFunctions: string[];
|
|
7
8
|
export declare function pickPluginOptions(options: Record<string, any>): PluginOptions;
|
|
8
9
|
/**
|
|
9
10
|
* Validate the plugin configuration
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.plugin = exports.validate = void 0;
|
|
3
|
+
exports.plugin = exports.validate = exports.DefaultFunctions = void 0;
|
|
4
4
|
exports.pickPluginOptions = pickPluginOptions;
|
|
5
5
|
const graphql_1 = require("graphql");
|
|
6
6
|
const utils_1 = require("./utils");
|
|
7
|
-
|
|
7
|
+
exports.DefaultFunctions = ['getContentType', 'getContentByPath', 'getContentById'];
|
|
8
8
|
function pickPluginOptions(options) {
|
|
9
9
|
return {
|
|
10
|
-
|
|
10
|
+
...(options.config ?? {}),
|
|
11
|
+
functions: options.functions ?? exports.DefaultFunctions,
|
|
11
12
|
prettyPrintQuery: options.prettyPrintQuery ?? false,
|
|
12
13
|
clientPath: options.clientPath ?? "./graphql"
|
|
13
14
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAYA,8CAQC;AAnBD,qCAA0J;AAC1J,mCAA8C;AAQjC,QAAA,gBAAgB,GAAG,CAAC,gBAAgB,EAAC,kBAAkB,EAAC,gBAAgB,CAAC,CAAA;AAEtF,SAAgB,iBAAiB,CAAC,OAA2B;IAEzD,OAAO;QACH,GAAG,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC;QACzB,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,wBAAgB;QAChD,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,KAAK;QACnD,UAAU,EAAE,OAAO,CAAC,UAAU,IAAI,WAAW;KAChD,CAAA;AACL,CAAC;AAED;;;;;;;;;GASG;AACI,MAAM,QAAQ,GAAoC,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,aAAa,EAAE,EAAE;IAEzH,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACnB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC;YAChC,M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AAE,QAAQ,EAAE,eAAe,CAAC,CAAA;QAChF,YAAY,CAAC,IAAI,CAAC,GAAG,oBAAoB,CAAC,CAAA;IAC9C,CAAC,CAAC,CAAA;IACF,OAAO,CAAE,GAAG,SAAS,EAAE,GAAG,YAAY,CAAC,CAAA;AAC3C,CAAC;AAED,kBAAe,EAAE,QAAQ,EAAR,gBAAQ,EAAE,MAAM,EAAN,cAAM,EAAkC,CAAA"}
|
package/dist/preset.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Types } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import { type ClientPresetConfig as ClientPresetOptions } from '@graphql-codegen/client-preset';
|
|
3
3
|
import { type PluginOptions } from './index';
|
|
4
|
-
import { type TransformOptions } from './
|
|
4
|
+
import { type TransformOptions } from './types';
|
|
5
5
|
export type PresetOptions = ClientPresetOptions & PluginOptions & TransformOptions;
|
|
6
6
|
export declare const preset: Types.OutputPreset<PresetOptions>;
|
|
7
7
|
export default preset;
|
package/dist/preset.js
CHANGED
|
@@ -24,7 +24,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.preset = void 0;
|
|
27
|
-
const documents_1 = require("./documents");
|
|
28
27
|
const graphql_1 = require("graphql");
|
|
29
28
|
// Import base preset
|
|
30
29
|
const client_preset_1 = require("@graphql-codegen/client-preset");
|
|
@@ -42,25 +41,56 @@ exports.preset = {
|
|
|
42
41
|
* @returns An awaitable with the modified list of documents.
|
|
43
42
|
*/
|
|
44
43
|
prepareDocuments: async (outputFilePath, outputSpecificDocuments) => {
|
|
44
|
+
// Get the configured documents
|
|
45
|
+
const optiDocs = outputSpecificDocuments.filter((x => typeof (x) == 'string' && x.startsWith('opti-cms:')));
|
|
46
|
+
const normalDocs = outputSpecificDocuments.filter(x => !(typeof (x) == 'string' && x.startsWith('opti-cms:')));
|
|
45
47
|
// Get the base documents
|
|
46
48
|
const documents = client_preset_1.preset.prepareDocuments ?
|
|
47
|
-
await client_preset_1.preset.prepareDocuments(outputFilePath,
|
|
48
|
-
[...
|
|
49
|
+
await client_preset_1.preset.prepareDocuments(outputFilePath, normalDocs) :
|
|
50
|
+
[...normalDocs, `!${outputFilePath}`];
|
|
51
|
+
// Transform / inject the Opti-CMS documents
|
|
52
|
+
const CmsDocLoaders = (optiDocs.length == 0 ? ['opti-cms:/fragments/13', 'opti-cms:/queries/13'] : optiDocs).map(optiDoc => {
|
|
53
|
+
const loader = {};
|
|
54
|
+
loader[optiDoc] = { loader: "@remkoj/optimizely-graph-functions/loader" };
|
|
55
|
+
return loader;
|
|
56
|
+
});
|
|
49
57
|
// Create a new, extended, array
|
|
50
|
-
return [...documents, ...
|
|
58
|
+
return [...documents, ...CmsDocLoaders];
|
|
51
59
|
},
|
|
52
60
|
buildGeneratesSection: async (options) => {
|
|
53
|
-
// Extend the
|
|
61
|
+
// Extend the default plugin configuration
|
|
54
62
|
options.config = {
|
|
63
|
+
// Overwriteable defaults
|
|
64
|
+
dedupeFragments: true, // Remove duplicate fragment references
|
|
65
|
+
emitLegacyCommonJSImports: false, //Switch to ESM
|
|
66
|
+
// Provided options
|
|
55
67
|
...options.config,
|
|
68
|
+
// Enforced settings
|
|
56
69
|
namingConvention: "keep", // Keep casing "as-is" from Optimizely Graph
|
|
57
70
|
};
|
|
71
|
+
// Change the default for fragment masking from 'useFragment' to
|
|
72
|
+
// 'getFragmentData', in order to prevent issues with code checks for
|
|
73
|
+
// React hooks
|
|
74
|
+
if (options.presetConfig.fragmentMasking !== false) {
|
|
75
|
+
options.presetConfig = {
|
|
76
|
+
...options.presetConfig,
|
|
77
|
+
fragmentMasking: {
|
|
78
|
+
unmaskFunctionName: 'getFragmentData',
|
|
79
|
+
...(typeof (options.presetConfig?.fragmentMasking) == 'object' ? options.presetConfig?.fragmentMasking : {})
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// Extend the document transforms in order to apply the transformations
|
|
84
|
+
// needed to automatically inject Block, Page & Element fragments
|
|
58
85
|
options.documentTransforms = [
|
|
59
86
|
...(options.documentTransforms || []),
|
|
60
87
|
{
|
|
61
88
|
name: 'optly-transform',
|
|
62
89
|
transformObject: transform_1.default,
|
|
63
|
-
config:
|
|
90
|
+
config: {
|
|
91
|
+
...options.config,
|
|
92
|
+
...(0, transform_1.pickTransformOptions)(options.presetConfig)
|
|
93
|
+
}
|
|
64
94
|
}
|
|
65
95
|
];
|
|
66
96
|
// The packages contain quite a few utility fragments, however these
|
|
@@ -73,6 +103,8 @@ exports.preset = {
|
|
|
73
103
|
FragmentDefinition: {
|
|
74
104
|
enter: (node) => {
|
|
75
105
|
if (!options.schema.definitions.some(x => (x.kind == graphql_1.Kind.OBJECT_TYPE_DEFINITION || x.kind == graphql_1.Kind.INTERFACE_TYPE_DEFINITION) && x.name.value == node.typeCondition.name.value)) {
|
|
106
|
+
if (options.presetConfig.verbose)
|
|
107
|
+
console.log(`⚠ Removing fragment ${node.name.value} from the documents, as its target ${node.typeCondition.name.value} is not available in the schema`);
|
|
76
108
|
return null;
|
|
77
109
|
}
|
|
78
110
|
}
|
|
@@ -102,16 +134,17 @@ exports.preset = {
|
|
|
102
134
|
},
|
|
103
135
|
{
|
|
104
136
|
'typescript-graphql-request': {
|
|
105
|
-
|
|
137
|
+
...options.config,
|
|
106
138
|
useTypeImports: true,
|
|
107
|
-
|
|
108
|
-
importOperationTypesFrom: "Schema"
|
|
139
|
+
importOperationTypesFrom: "Schema",
|
|
109
140
|
}
|
|
110
141
|
}
|
|
111
142
|
],
|
|
112
143
|
schema: options.schema,
|
|
113
144
|
schemaAst: options.schemaAst,
|
|
114
|
-
config:
|
|
145
|
+
config: {
|
|
146
|
+
...options.config,
|
|
147
|
+
},
|
|
115
148
|
profiler: options.profiler,
|
|
116
149
|
documents: options.documents,
|
|
117
150
|
documentTransforms: options.documentTransforms
|
|
@@ -130,18 +163,42 @@ exports.preset = {
|
|
|
130
163
|
schema: options.schema,
|
|
131
164
|
schemaAst: options.schemaAst,
|
|
132
165
|
profiler: options.profiler,
|
|
133
|
-
config:
|
|
166
|
+
config: {
|
|
167
|
+
...options.config,
|
|
168
|
+
...(0, index_1.pickPluginOptions)(options.presetConfig),
|
|
169
|
+
},
|
|
134
170
|
documents: options.documents,
|
|
135
171
|
documentTransforms: options.documentTransforms
|
|
136
172
|
});
|
|
137
|
-
//
|
|
173
|
+
// Update all sections for Optimizely Graph
|
|
138
174
|
section.forEach((fileConfig, idx) => {
|
|
175
|
+
// Modify index.ts with additional exports
|
|
139
176
|
if (fileConfig.filename.endsWith("index.ts")) {
|
|
140
177
|
section[idx].plugins.unshift({
|
|
141
178
|
add: {
|
|
142
|
-
content: [
|
|
179
|
+
content: [
|
|
180
|
+
'export * as Schema from "./graphql";',
|
|
181
|
+
'export * from "./functions";',
|
|
182
|
+
'export { getSdk, type Sdk } from "./client";',
|
|
183
|
+
]
|
|
143
184
|
}
|
|
144
185
|
});
|
|
186
|
+
section[idx].plugins.push({
|
|
187
|
+
add: {
|
|
188
|
+
content: ['', `export const WITH_RECURSIVE_SUPPORT = ${options.presetConfig.recursion === true ? 'true' : 'false'};`]
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
// Optimizely Graph supports recursive queries to allow fetching
|
|
193
|
+
// data as created in the CMS. This can cause issues when using
|
|
194
|
+
// multiple GraphQL sources, hence the ability to enable/disalbe
|
|
195
|
+
// the support for recursive queries.
|
|
196
|
+
if (fileConfig.skipDocumentsValidation != true && options.presetConfig.recursion === true) {
|
|
197
|
+
const currentOptions = fileConfig.skipDocumentsValidation || {};
|
|
198
|
+
section[idx].skipDocumentsValidation = {
|
|
199
|
+
...currentOptions,
|
|
200
|
+
ignoreRules: [...(currentOptions.ignoreRules ?? []), 'NoFragmentCyclesRule']
|
|
201
|
+
};
|
|
145
202
|
}
|
|
146
203
|
});
|
|
147
204
|
return section;
|
package/dist/preset.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"preset.js","sourceRoot":"","sources":["../src/preset.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,
|
|
1
|
+
{"version":3,"file":"preset.js","sourceRoot":"","sources":["../src/preset.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,qCAAqC;AAErC,qBAAqB;AACrB,kEAAuH;AACvH,kGAAmF;AACnF,gEAAiD;AAEjD,wBAAwB;AACxB,iDAAuE;AACvE,yDAA6D;AAMhD,QAAA,MAAM,GACnB;IACI;;;;;;OAMG;IACH,gBAAgB,EAAE,KAAK,EAAE,cAAgC,EAAE,uBAA+D,EAAE,EAAE;QAC1H,+BAA+B;QAC/B,MAAM,QAAQ,GAAG,uBAAuB,CAAC,MAAM,CAAS,CAAC,CAAC,CAAC,EAAE,CAAC,OAAM,CAAC,CAAC,CAAC,IAAI,QAAQ,IAAI,CAAC,CAAC,UAAU,CAAC,WAAW,CAAC,CAAgD,CAAC,CAAA;QACjK,MAAM,UAAU,GAAG,uBAAuB,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAM,CAAC,CAAC,CAAC,IAAI,QAAQ,IAAI,CAAC,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;QAE7G,yBAAyB;QACzB,MAAM,SAAS,GAAG,sBAAY,CAAC,gBAAgB,CAAC,CAAC;YAC7C,MAAM,sBAAY,CAAC,gBAAgB,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC,CAAC;YACjE,CAAC,GAAG,UAAU,EAAE,IAAI,cAAc,EAAE,CAAC,CAAA;QAEzC,4CAA4C;QAC5C,MAAM,aAAa,GACf,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,wBAAwB,EAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,GAAG,CA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0C;YAC1C,IAAI,UAAU,CAAC,QAAQ,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC3C,OAAO,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;oBACzB,GAAG,EAAE;wBACD,OAAO,EAAE;4BACL,sCAAsC;4BACtC,8BAA8B;4BAC9B,8CAA8C;yBACjD;qBACJ;iBACJ,CAAC,CAAA;gBACF,OAAO,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC;oBACtB,GAAG,EAAE;wBACD,OAAO,EAAE,CAAC,EAAE,EAAC,yCAA0C,OAAO,CAAC,YAAY,CAAC,SAAS,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAQ,GAAG,CAAC;qBACzH;iBACJ,CAAC,CAAA;YACN,CAAC;YAED,gEAAgE;YAChE,+DAA+D;YAC/D,gEAAgE;YAChE,qCAAqC;YACrC,IAAI,UAAU,CAAC,uBAAuB,IAAI,IAAI,IAAI,OAAO,CAAC,YAAY,CAAC,SAAS,KAAK,IAAI,EAAE,CAAC;gBACxF,MAAM,cAAc,GAAG,UAAU,CAAC,uBAAuB,IAAI,EAAE,CAAA;gBAC/D,OAAO,CAAC,GAAG,CAAC,CAAC,uBAAuB,GAAG;oBACnC,GAAG,cAAc;oBACjB,WAAW,EAAE,CAAC,GAAG,CAAC,cAAc,CAAC,WAAW,IAAI,EAAE,CAAC,EAAE,sBAAsB,CAAC;iBAC/E,CAAA;YACL,CAAC;QACL,CAAC,CAAC,CAAA;QAEF,OAAO,OAAO,CAAA;IAClB,CAAC;CACJ,CAAA;AAED,kBAAe,cAAM,CAAA"}
|
package/dist/transform.d.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import { type Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
-
import type {
|
|
3
|
-
export type TransformOptions = {
|
|
4
|
-
injections?: Injection[];
|
|
5
|
-
verbose?: boolean;
|
|
6
|
-
recursion?: boolean;
|
|
7
|
-
};
|
|
2
|
+
import type { TransformOptions } from './types';
|
|
8
3
|
export declare function pickTransformOptions(options: Record<string, any>): TransformOptions;
|
|
9
4
|
export declare const transform: Types.DocumentTransformFunction<TransformOptions>;
|
|
10
5
|
declare const _default: {
|
package/dist/transform.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.transform = void 0;
|
|
7
4
|
exports.pickTransformOptions = pickTransformOptions;
|
|
8
5
|
const graphql_1 = require("graphql");
|
|
9
|
-
const node_fs_1 = __importDefault(require("node:fs"));
|
|
10
6
|
function pickTransformOptions(options) {
|
|
11
7
|
return {
|
|
12
8
|
injections: options.injections ?? [],
|
|
@@ -14,10 +10,13 @@ function pickTransformOptions(options) {
|
|
|
14
10
|
recursion: options.recursion ?? true
|
|
15
11
|
};
|
|
16
12
|
}
|
|
17
|
-
function isArray(toTest) { return Array.isArray(toTest); }
|
|
18
13
|
const transform = ({ documents: files, config, schema, pluginContext }) => {
|
|
14
|
+
if (config.verbose)
|
|
15
|
+
console.debug(`[ OPTIMIZELY ] Starting Optimizely Graph Query & Fragment transformations`);
|
|
19
16
|
const injections = config.injections ?? [];
|
|
20
17
|
// Retrieve component fragments
|
|
18
|
+
if (config.verbose)
|
|
19
|
+
console.debug(`[ OPTIMIZELY ] Searching for fragments to inject`);
|
|
21
20
|
const componentFragments = {};
|
|
22
21
|
files.forEach(file => {
|
|
23
22
|
if (!file.document)
|
|
@@ -46,82 +45,55 @@ const transform = ({ documents: files, config, schema, pluginContext }) => {
|
|
|
46
45
|
});
|
|
47
46
|
// Get the names we actually need to inject into, and return when none are present
|
|
48
47
|
const intoNames = Object.getOwnPropertyNames(componentFragments);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (config.
|
|
52
|
-
|
|
53
|
-
const recursiveFragments = ["IContentListItem"];
|
|
54
|
-
intoNames.forEach(intoName => {
|
|
55
|
-
componentFragments[intoName].forEach(fragment => {
|
|
56
|
-
(0, graphql_1.visit)(fragment, {
|
|
57
|
-
FragmentSpread: {
|
|
58
|
-
leave(node, key, parent, path, ancestors) {
|
|
59
|
-
if (recursiveFragments.includes(node.name.value) && !isArray(ancestors[0]) && ancestors[0].kind == graphql_1.Kind.FRAGMENT_DEFINITION) {
|
|
60
|
-
if (config.verbose)
|
|
61
|
-
console.debug(`[ OPTIMIZELY ] Found ${node.name.value} within ${fragment.name.value} for ${intoName}, creating recursive fragment`);
|
|
62
|
-
const fields = ancestors.filter(a => !isArray(a) && a.kind != graphql_1.Kind.FRAGMENT_DEFINITION && a.kind != graphql_1.Kind.SELECTION_SET);
|
|
63
|
-
if (fields.length < 1)
|
|
64
|
-
return undefined;
|
|
65
|
-
if (fields.length > 1)
|
|
66
|
-
throw new Error("Recursive items on embedded blocks are not supported at the moment");
|
|
67
|
-
const newNode = {
|
|
68
|
-
kind: graphql_1.Kind.INLINE_FRAGMENT,
|
|
69
|
-
typeCondition: ancestors[0].typeCondition,
|
|
70
|
-
selectionSet: {
|
|
71
|
-
kind: graphql_1.Kind.SELECTION_SET,
|
|
72
|
-
selections: [{
|
|
73
|
-
kind: graphql_1.Kind.FIELD,
|
|
74
|
-
name: fields[0].name,
|
|
75
|
-
alias: fields[0].alias,
|
|
76
|
-
directives: [{
|
|
77
|
-
kind: graphql_1.Kind.DIRECTIVE,
|
|
78
|
-
name: { kind: graphql_1.Kind.NAME, value: "recursive" },
|
|
79
|
-
arguments: [{
|
|
80
|
-
kind: graphql_1.Kind.ARGUMENT,
|
|
81
|
-
name: { kind: graphql_1.Kind.NAME, value: "depth" },
|
|
82
|
-
value: { kind: graphql_1.Kind.INT, value: "5" }
|
|
83
|
-
}]
|
|
84
|
-
}],
|
|
85
|
-
selectionSet: {
|
|
86
|
-
kind: graphql_1.Kind.SELECTION_SET,
|
|
87
|
-
selections: recursiveSelections
|
|
88
|
-
}
|
|
89
|
-
}]
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
if (!componentSpreads[intoName])
|
|
93
|
-
componentSpreads[intoName] = [];
|
|
94
|
-
componentSpreads[intoName].push(newNode);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
}
|
|
48
|
+
if (intoNames.length == 0)
|
|
49
|
+
return files;
|
|
50
|
+
if (config.verbose)
|
|
51
|
+
console.debug(`[ OPTIMIZELY ] Update queries & fragments using the fragment(s): ${intoNames.join(', ')}`);
|
|
102
52
|
// Update the documents
|
|
103
|
-
|
|
53
|
+
const transformedFiles = files.map(file => {
|
|
54
|
+
if (config.verbose)
|
|
55
|
+
console.debug(`[ OPTIMIZELY ] Processing ${file.location}`);
|
|
104
56
|
const document = file.document ? (0, graphql_1.visit)(file.document, {
|
|
105
57
|
// Remove fragments from the preset, for which the target type does not exist
|
|
106
|
-
FragmentDefinition: {
|
|
58
|
+
/*FragmentDefinition: {
|
|
107
59
|
enter(node) {
|
|
108
|
-
if (file.location && !
|
|
109
|
-
const typePresent = schema.definitions.some(definition => (definition.kind ==
|
|
60
|
+
if (file.location && !fs.existsSync(file.location)) {
|
|
61
|
+
const typePresent = schema.definitions.some(definition => (definition.kind == Kind.OBJECT_TYPE_DEFINITION || definition.kind == Kind.INTERFACE_TYPE_DEFINITION) && definition.name.value == node.typeCondition.name.value)
|
|
110
62
|
if (!typePresent) {
|
|
111
|
-
if (config.verbose)
|
|
112
|
-
|
|
113
|
-
return null;
|
|
63
|
+
if (config.verbose) console.debug(`[OPTIMIZELY] Type ${ node.typeCondition.name.value } not found, dropping fragment ${ node.name.value }`)
|
|
64
|
+
return null
|
|
114
65
|
}
|
|
115
66
|
}
|
|
116
67
|
}
|
|
117
|
-
}
|
|
118
|
-
//
|
|
68
|
+
},*/
|
|
69
|
+
// Replace the fragment occurances
|
|
119
70
|
SelectionSet: {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
71
|
+
leave(node, key, parent, path, ancestors) {
|
|
72
|
+
const parentName = [...ancestors].reverse().filter(isFragmentOrOperation).at(0)?.name?.value;
|
|
73
|
+
const sectionsToAdd = node.selections
|
|
74
|
+
.map(selection => {
|
|
75
|
+
if (selection.kind != graphql_1.Kind.FRAGMENT_SPREAD)
|
|
76
|
+
return undefined;
|
|
77
|
+
const testableName = config.recursion && selection.name.value.startsWith('Recursive') ? selection.name.value.substring(9) : selection.name.value;
|
|
78
|
+
if (config.recursion && config.verbose && testableName != selection.name.value)
|
|
79
|
+
console.debug(`[ OPTIMIZELY ] Using ${selection.name.value} in ${parentName} as ${testableName} to allow recursion`);
|
|
80
|
+
return intoNames.includes(testableName) ? testableName : undefined;
|
|
81
|
+
})
|
|
82
|
+
.filter(isNotNullOrUndefined);
|
|
83
|
+
if (sectionsToAdd.length == 0)
|
|
84
|
+
return;
|
|
85
|
+
if (config.verbose)
|
|
86
|
+
console.debug(`[ OPTIMIZELY ] Identified usage of fragment(s) ${sectionsToAdd.join(', ')} in ${parentName}, starting injection procedure`);
|
|
87
|
+
const newSelections = []; //.filter(selection => !(selection.kind == Kind.FRAGMENT_SPREAD && intoNames.includes(selection.name.value)))
|
|
88
|
+
sectionsToAdd.forEach(sectionName => {
|
|
89
|
+
const addedSelections = componentFragments[sectionName].map(fragment => {
|
|
90
|
+
if (newSelections.some(selection => selection.kind == graphql_1.Kind.FRAGMENT_SPREAD && selection.name.value == fragment.name.value)) {
|
|
91
|
+
if (config.verbose)
|
|
92
|
+
console.debug(`[ OPTIMIZELY ] Fragment ${fragment.name.value} is already adjacent to ${sectionName}`);
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
/*if (config.verbose)
|
|
96
|
+
console.debug(`[ OPTIMIZELY ] Adding fragment ${ fragment.name.value } adjacent to ${ sectionName }`)*/
|
|
125
97
|
return {
|
|
126
98
|
kind: graphql_1.Kind.FRAGMENT_SPREAD,
|
|
127
99
|
directives: [],
|
|
@@ -130,21 +102,20 @@ const transform = ({ documents: files, config, schema, pluginContext }) => {
|
|
|
130
102
|
value: fragment.name.value
|
|
131
103
|
}
|
|
132
104
|
};
|
|
133
|
-
});
|
|
134
|
-
|
|
105
|
+
}).filter(isNotNullOrUndefined);
|
|
106
|
+
if (addedSelections.length > 0) {
|
|
135
107
|
if (config.verbose)
|
|
136
|
-
console.
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return undefined;
|
|
108
|
+
console.log(`[ OPTIMIZELY ] Added fragments ${addedSelections.map(a => a.name.value).join(', ')} adjacent to ${sectionName}`);
|
|
109
|
+
newSelections.push(...addedSelections);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
if (newSelections.length == 0)
|
|
113
|
+
return;
|
|
114
|
+
const newNode = {
|
|
115
|
+
...node,
|
|
116
|
+
selections: [...node.selections, ...newSelections]
|
|
117
|
+
};
|
|
118
|
+
return newNode;
|
|
148
119
|
}
|
|
149
120
|
}
|
|
150
121
|
}) : undefined;
|
|
@@ -153,11 +124,18 @@ const transform = ({ documents: files, config, schema, pluginContext }) => {
|
|
|
153
124
|
document: document,
|
|
154
125
|
};
|
|
155
126
|
});
|
|
127
|
+
if (config.verbose)
|
|
128
|
+
console.debug(`[ OPTIMIZELY ] Finished transformation procedure`);
|
|
129
|
+
return transformedFiles;
|
|
156
130
|
};
|
|
157
131
|
exports.transform = transform;
|
|
158
132
|
exports.default = { transform: exports.transform };
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
133
|
+
function isNotNullOrUndefined(toTest) {
|
|
134
|
+
return toTest !== null && toTest !== undefined;
|
|
135
|
+
}
|
|
136
|
+
function isFragmentOrOperation(x) {
|
|
137
|
+
if (Array.isArray(x) || x == undefined || x == null)
|
|
138
|
+
return false;
|
|
139
|
+
return x.kind == graphql_1.Kind.FRAGMENT_DEFINITION || x.kind == graphql_1.Kind.OPERATION_DEFINITION;
|
|
140
|
+
}
|
|
163
141
|
//# sourceMappingURL=transform.js.map
|
package/dist/transform.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.js","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transform.js","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":";;;AAMA,oDAOC;AAVD,qCAAqC;AAGrC,SAAgB,oBAAoB,CAAC,OAA2B;IAE5D,OAAO;QACH,UAAU,EAAE,OAAO,CAAC,UAAU,IAAI,EAAE;QACpC,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,KAAK;QACjC,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,IAAI;KACvC,CAAA;AACL,CAAC;AAEM,MAAM,SAAS,GAAuD,CAAC,EAAC,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,EAAE,EAAE;IAEhI,IAAI,MAAM,CAAC,OAAO;QACd,OAAO,CAAC,KAAK,CAAC,2EAA2E,CAAC,CAAA;IAC9F,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,IAAI,EAAE,CAAA;IAE1C,+BAA+B;IAC/B,IAAI,MAAM,CAAC,OAAO;QACd,OAAO,CAAC,KAAK,CAAC,kDAAkD,CAAC,CAAA;IACrE,MAAM,kBAAkB,GAAmD,EAAE,CAAA;IAC7E,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QACjB,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,OAAM;QAC1B,MAAM,oBAAoB,GAAG,UAAU,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,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AAA;AA9HY,QAAA,SAAS,aA8HrB;AAED,kBAAe,EAAE,SAAS,EAAT,iBAAS,EAAE,CAAA;AAE5B,SAAS,oBAAoB,CAAI,MAAiB;IAE9C,OAAO,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS,CAAA;AAClD,CAAC;AACD,SAAS,qBAAqB,CAAC,CAAmD;IAE9E,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,SAAS,IAAI,CAAC,IAAI,IAAI;QAC/C,OAAO,KAAK,CAAA;IAChB,OAAQ,CAAa,CAAC,IAAI,IAAI,cAAI,CAAC,mBAAmB,IAAK,CAAa,CAAC,IAAI,IAAI,cAAI,CAAC,oBAAoB,CAAA;AAC9G,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,24 @@
|
|
|
1
1
|
import type { FragmentDefinitionNode, OperationDefinitionNode } from 'graphql';
|
|
2
|
+
export type TransformOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* Configure fragment injection targets and filters as a set of injection
|
|
5
|
+
* rules.
|
|
6
|
+
*/
|
|
7
|
+
injections?: Injection[];
|
|
8
|
+
/**
|
|
9
|
+
* Enable verbose output
|
|
10
|
+
*/
|
|
11
|
+
verbose?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Disables the recursive validation of GraphQL-Codegen and updates the
|
|
14
|
+
* rules to ensure that the code is properly generated.
|
|
15
|
+
*
|
|
16
|
+
* *NOTE:* This requires a custom resolution of the
|
|
17
|
+
* `@graphql-codegen/visitor-plugin-common` package, which patches an unhandled
|
|
18
|
+
* infinite loop when the recursive validation has been disabled.
|
|
19
|
+
*/
|
|
20
|
+
recursion?: boolean;
|
|
21
|
+
};
|
|
2
22
|
export type Injection = {
|
|
3
23
|
into: string;
|
|
4
24
|
nameRegex?: string;
|