@remkoj/optimizely-graph-functions 6.0.0-pre9 → 6.0.0-rc.3
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/AGENTS.md +105 -0
- package/README.md +60 -14
- package/dist/_transform/handleDependDirective.d.ts +11 -0
- package/dist/_transform/handleDependDirective.js +128 -0
- package/dist/_transform/handleDependDirective.js.map +1 -0
- package/dist/_transform/injectComponentDocuments.d.ts +3 -0
- package/dist/_transform/injectComponentDocuments.js +78 -24
- package/dist/_transform/injectComponentDocuments.js.map +1 -1
- package/dist/_transform/injectPageQueries.d.ts +1 -0
- package/dist/_transform/injectPageQueries.js +40 -7
- package/dist/_transform/injectPageQueries.js.map +1 -1
- package/dist/_transform/injectSectionQueries.d.ts +1 -0
- package/dist/_transform/injectSectionQueries.js +64 -17
- package/dist/_transform/injectSectionQueries.js.map +1 -1
- package/dist/_transform/normalizeFragmentNames.js +2 -0
- package/dist/_transform/normalizeFragmentNames.js.map +1 -1
- package/dist/_transform/performInjections.js +2 -35
- package/dist/_transform/performInjections.js.map +1 -1
- package/dist/cms/index.d.ts +4 -10
- package/dist/cms/index.js +64 -29
- package/dist/cms/index.js.map +1 -1
- package/dist/contenttype-loader.d.ts +1 -48
- package/dist/contenttype-loader.js +21 -286
- package/dist/contenttype-loader.js.map +1 -1
- package/dist/document-ast-plugin.d.ts +1 -1
- package/dist/document-ast-plugin.js +1 -1
- package/dist/document-ast-plugin.js.map +1 -1
- package/dist/documents/fragments.cms12.js.map +1 -1
- package/dist/documents/fragments.cms13.js +77 -13
- package/dist/documents/fragments.cms13.js.map +1 -1
- package/dist/documents/queries.cms13.js +4 -4
- package/dist/generator/collision-tracker.d.ts +22 -0
- package/dist/generator/collision-tracker.js +104 -0
- package/dist/generator/collision-tracker.js.map +1 -0
- package/dist/generator/generator.d.ts +77 -0
- package/dist/generator/generator.js +277 -0
- package/dist/generator/generator.js.map +1 -0
- package/dist/generator/index.d.ts +3 -0
- package/dist/generator/index.js +42 -0
- package/dist/generator/index.js.map +1 -0
- package/dist/generator/virtual-location.d.ts +41 -0
- package/dist/generator/virtual-location.js +139 -0
- package/dist/generator/virtual-location.js.map +1 -0
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/patch.js.map +1 -1
- package/dist/preset.js +103 -38
- package/dist/preset.js.map +1 -1
- package/dist/transform.d.ts +4 -3
- package/dist/transform.js +8 -1
- package/dist/transform.js.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +13 -3
- package/dist/utils.js.map +1 -1
- package/package.json +31 -21
package/AGENTS.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# @remkoj/optimizely-graph-functions — usage
|
|
2
|
+
|
|
3
|
+
`graphql-codegen` preset and plugin for Optimizely Graph. Generates typed GraphQL
|
|
4
|
+
operations and fragments for querying Optimizely Content Graph.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install --save-dev @remkoj/optimizely-graph-functions
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Subpath exports
|
|
13
|
+
|
|
14
|
+
| Import path | Contents |
|
|
15
|
+
| --- | --- |
|
|
16
|
+
| `@remkoj/optimizely-graph-functions` | Plugin entry point (`plugin`, `validate`) |
|
|
17
|
+
| `@remkoj/optimizely-graph-functions/preset` | Codegen preset (primary entrypoint for codegen config) |
|
|
18
|
+
| `@remkoj/optimizely-graph-functions/plugin` | Standalone codegen plugin |
|
|
19
|
+
| `@remkoj/optimizely-graph-functions/transform` | Document transform |
|
|
20
|
+
| `@remkoj/optimizely-graph-functions/documents` | Shared Optimizely Graph fragment documents |
|
|
21
|
+
| `@remkoj/optimizely-graph-functions/loader` | GraphQL document loader |
|
|
22
|
+
| `@remkoj/optimizely-graph-functions/contenttype-loader` | Content-type aware loader |
|
|
23
|
+
| `@remkoj/optimizely-graph-functions/generate` | Code generation utilities |
|
|
24
|
+
|
|
25
|
+
## Usage in `codegen.ts`
|
|
26
|
+
|
|
27
|
+
The package exposes a full codegen **preset** (not just a plugin). Use it as the `preset` value for your output target. The preset auto-discovers your component GraphQL files and injects the right fragments based on the file-name convention below.
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import type { CodegenConfig } from '@graphql-codegen/cli'
|
|
31
|
+
import getSchemaInfo from '@remkoj/optimizely-graph-client/codegen'
|
|
32
|
+
import OptimizelyGraphPreset, { type PresetOptions } from '@remkoj/optimizely-graph-functions/preset'
|
|
33
|
+
|
|
34
|
+
const config: CodegenConfig = {
|
|
35
|
+
schema: getSchemaInfo(), // reads env vars automatically
|
|
36
|
+
documents: ['src/**/*.graphql'],
|
|
37
|
+
generates: {
|
|
38
|
+
'./gql/': {
|
|
39
|
+
preset: OptimizelyGraphPreset,
|
|
40
|
+
presetConfig: {
|
|
41
|
+
// Allow recursive fragments (requires patched visitor-plugin-common)
|
|
42
|
+
recursion: true,
|
|
43
|
+
|
|
44
|
+
// Map component GraphQL files to the correct base fragment
|
|
45
|
+
injections: [
|
|
46
|
+
{ into: 'PageData', pathRegex: 'src\/components\/cms\/.*\.page\.graphql' },
|
|
47
|
+
{ into: 'PageData', pathRegex: 'src\/components\/cms\/.*\.experience\.graphql' },
|
|
48
|
+
{ into: 'BlockData', pathRegex: 'src\/components\/cms\/.*\.block\.graphql' },
|
|
49
|
+
{ into: 'BlockData', pathRegex: 'src\/components\/cms\/.*\.component\.graphql' },
|
|
50
|
+
{ into: 'BlockData', pathRegex: 'src\/components\/cms\/.*\.section\.graphql' },
|
|
51
|
+
{ into: 'ElementData', pathRegex: 'src\/components\/cms\/.*\.element\.graphql' },
|
|
52
|
+
{ into: 'IContentListItem', pathRegex: 'src\/components\/cms\/.*\.contentarea\.graphql' },
|
|
53
|
+
],
|
|
54
|
+
} as PresetOptions,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default config
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## GraphQL file naming conventions
|
|
63
|
+
|
|
64
|
+
Files discovered by `injections.pathRegex` are automatically injected into the named base fragment:
|
|
65
|
+
|
|
66
|
+
| File suffix | Injected into |
|
|
67
|
+
| --- | --- |
|
|
68
|
+
| `*.page.graphql` | `PageData` |
|
|
69
|
+
| `*.experience.graphql` | `PageData` |
|
|
70
|
+
| `*.block.graphql` | `BlockData` |
|
|
71
|
+
| `*.component.graphql` | `BlockData` |
|
|
72
|
+
| `*.section.graphql` | `BlockData` |
|
|
73
|
+
| `*.element.graphql` | `ElementData` |
|
|
74
|
+
| `*.contentarea.graphql` | `IContentListItem` |
|
|
75
|
+
|
|
76
|
+
Each file should contain a single named fragment that matches its content type, e.g. `fragment HeroBlockData on HeroBlock { ... }`.
|
|
77
|
+
|
|
78
|
+
## Preset options (`PresetOptions`)
|
|
79
|
+
|
|
80
|
+
`PresetOptions` extends `ClientPresetConfig` (from `@graphql-codegen/client-preset`) with:
|
|
81
|
+
|
|
82
|
+
| Option | Default | Description |
|
|
83
|
+
| --- | --- | --- |
|
|
84
|
+
| `functions` | `['getContentType', 'getContentByPath', 'getContentById']` | Built-in Optimizely Graph functions to generate typed wrappers for |
|
|
85
|
+
| `prettyPrintQuery` | `false` | Format generated query strings |
|
|
86
|
+
| `clientPath` | `"./graphql"` | Path to the generated GraphQL client |
|
|
87
|
+
| `recursion` | `false` | Enable recursive fragment support (requires patched `visitor-plugin-common`) |
|
|
88
|
+
| `injections` | `[]` | Array of `{ into, pathRegex?, nameRegex? }` injection rules |
|
|
89
|
+
| `cleanup` | `true` | Strip injected fragment spreads after processing |
|
|
90
|
+
|
|
91
|
+
## Loading Optimizely-provided fragments
|
|
92
|
+
|
|
93
|
+
The loader protocol `opti-cms:/fragments/13` fetches the built-in Optimizely Graph fragments (the preset adds this automatically; you normally do not need to add it manually):
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// Only needed when building a custom codegen target that references Optimizely fragments
|
|
97
|
+
{ 'opti-cms:/fragments/13': { loader: '@remkoj/optimizely-graph-functions/loader' } }
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Constraints
|
|
101
|
+
|
|
102
|
+
- Requires `@graphql-codegen/cli` and `@graphql-codegen/client-preset` as peer dependencies.
|
|
103
|
+
- Depends on a **patched** `@graphql-codegen/visitor-plugin-common`; after `yarn install`/`upgrade` run `yarn opti-graph patches:apply`.
|
|
104
|
+
- CommonJS module.
|
|
105
|
+
- Use the preset (`./preset`) as the entry point, not the bare plugin, for all new projects.
|
package/README.md
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
# Optimizely GraphQL Codegen Plugin
|
|
1
|
+
# Optimizely GraphQL Codegen Plugin <!-- omit in toc -->
|
|
2
|
+
|
|
3
|
+
> [!WARNING]
|
|
4
|
+
> There'll be an update of Optimizely SaaS CMS that is incompatible with all SDK versions prior to 5.1.6. If you don't upgrade, you will see empty pages (main website) and "Component not found" messages (preview).
|
|
5
|
+
|
|
2
6
|
GraphQL Codegen plugin and preset which generate both the GraphQL type definitions and a few convenienece methods for useage with [Optimizely Graph Client](../optimizely-graph-client/README.md).
|
|
3
7
|
|
|
4
8
|
[Release notes](https://github.com/remkoj/optimizely-dxp-clients/releases)
|
|
@@ -6,7 +10,22 @@ GraphQL Codegen plugin and preset which generate both the GraphQL type definitio
|
|
|
6
10
|
> [!WARNING]
|
|
7
11
|
> The GraphQL Codegen preset requires a patch to enable it to work with recursive queries. Make sure to run this command after every update to ensure you're using the latest patches: `yarn opti-graph patches:apply`. Adjust when used in a mono-repo to patch the correct package.json, for example: `yarn workspace frontend opti-graph patches:apply -p ../../`
|
|
8
12
|
|
|
9
|
-
##
|
|
13
|
+
## Contents <!-- omit in toc -->
|
|
14
|
+
- [1. Install package](#1-install-package)
|
|
15
|
+
- [2. Configure package](#2-configure-package)
|
|
16
|
+
- [3. GraphQL Document Processing](#3-graphql-document-processing)
|
|
17
|
+
- [3.1. Allow overwriting of built-in fragments \& queries](#31-allow-overwriting-of-built-in-fragments--queries)
|
|
18
|
+
- [3.2. Injection of fragments \& queries for ContentTypes](#32-injection-of-fragments--queries-for-contenttypes)
|
|
19
|
+
- [3.3. Auto inject fragments](#33-auto-inject-fragments)
|
|
20
|
+
- [3.4. Remove fragments and spreads that target non-existing types](#34-remove-fragments-and-spreads-that-target-non-existing-types)
|
|
21
|
+
- [3.5. Dedicated Queries \& Fragments](#35-dedicated-queries--fragments)
|
|
22
|
+
- [Compiler field directive `@depend`](#compiler-field-directive-depend)
|
|
23
|
+
- [4. Usage](#4-usage)
|
|
24
|
+
- [4.1. Option 1: Direct methods](#41-option-1-direct-methods)
|
|
25
|
+
- [4.2. Option 2: Enhanced Client](#42-option-2-enhanced-client)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## 1. Install package
|
|
10
29
|
To install using Yarn, use the following command:
|
|
11
30
|
|
|
12
31
|
```bash
|
|
@@ -19,7 +38,7 @@ yarn add --dev @remkoj/optimizely-graph-cli
|
|
|
19
38
|
yarn opti-graph patches:apply
|
|
20
39
|
```
|
|
21
40
|
|
|
22
|
-
## Configure package
|
|
41
|
+
## 2. Configure package
|
|
23
42
|
Create a codegen.ts within your application root folder (e.g. apps/frontend/codegen.ts within the example site). Within the codegen.ts create the following configuration:
|
|
24
43
|
|
|
25
44
|
```typescript
|
|
@@ -104,11 +123,46 @@ The presetConfig of the `OptimizelyGraphPreset` is an extension of the configura
|
|
|
104
123
|
| functions | The list of GraphQL Functions that should be made available in the `functions.ts` file. When specified, this overrides the default list.<br/>*Default value: `['getContentType','getContentByPath','getContentById']`*
|
|
105
124
|
| verbose | Set to `true` to enable debugging output of the preset, loader, plugin and transform |
|
|
106
125
|
|
|
126
|
+
## 3. GraphQL Document Processing
|
|
127
|
+
### 3.1. Allow overwriting of built-in fragments & queries
|
|
128
|
+
All fragment and query names injected / generated by this package start with an underscore (for example: `_getContentById`). During the document pre-processing, all these fragments and queries will be processed with this logic:
|
|
129
|
+
- If there's no query or fragment with the name without the leading underscore, it will be renamed. (i.e. `_getContentById` becomes `getContentById`)
|
|
130
|
+
- If a query or fragment with the name without leading underscore does exist, the built-in version with underscore will be removed from the document set.
|
|
131
|
+
|
|
132
|
+
This allows a project to overwrite built-in fragments and queries, while still ensuring type-safety.
|
|
133
|
+
|
|
134
|
+
### 3.2. Injection of fragments & queries for ContentTypes
|
|
135
|
+
For each content type the appropriate fragments and queries will be auto generated an injected into the document set.
|
|
136
|
+
|
|
137
|
+
### 3.3. Auto inject fragments
|
|
138
|
+
Dynamically build queries & fragments based upon the `injections` configuration, and the default injections from the auto-generated fragments & queries.
|
|
139
|
+
|
|
140
|
+
This allows you to tell wich group of fragments you want at a given location and then during GraphQL compilation generate the full queries based upon the current content schema in Optimizely CMS and your project configuration.
|
|
141
|
+
|
|
142
|
+
### 3.4. Remove fragments and spreads that target non-existing types
|
|
143
|
+
Depending on the features you have enabled in your CMS, not all types might actually be present in Optimizely Graph. This logic removes these fragments and spreads from the documents.
|
|
144
|
+
|
|
145
|
+
This approach assumes you're using TypeScript, as missing, but required types or fields should cause an error in the TypeScript compilation step of you application. Hence it moves the error from GraphQL Codegen to TypeScript, allowing some of the changes to be handled in the code.
|
|
146
|
+
|
|
147
|
+
### 3.5. Dedicated Queries & Fragments
|
|
148
|
+
The preset automatically injects a number of fragments and documents into the generated code. These can be found in their respective document:
|
|
149
|
+
|
|
150
|
+
- [`opti-cms:/queries/13`](./src/documents/queries.cms13.ts)
|
|
151
|
+
- [`opti-cms:/queries/12`](./src/documents/queries.cms12.ts)
|
|
152
|
+
- [`opti-cms:/fragments/13`](./src/documents/fragments.cms13.ts)
|
|
153
|
+
- [`opti-cms:/fragments/12`](./src/documents/fragments.cms12.ts)
|
|
154
|
+
|
|
155
|
+
### Compiler field directive `@depend`
|
|
156
|
+
A compile time directive `@depend` is processed, allowing you to make selections depend on a field to present in the schema.
|
|
107
157
|
|
|
108
|
-
|
|
158
|
+
For example: `componentData: item @depend(on: "ContentReference.item") {}` will remove the field selection `componentData` from the document if there's no type with the name `ContentReference` that has a field named `item`.
|
|
159
|
+
|
|
160
|
+
This allows to write generic queries that can cope with differen configurations within the CMS.
|
|
161
|
+
|
|
162
|
+
## 4. Usage
|
|
109
163
|
After running the code generation, you can use the following API's (assuming the folder where the generated files are stored is available at `@/gql`):
|
|
110
164
|
|
|
111
|
-
### Option 1: Direct methods
|
|
165
|
+
### 4.1. Option 1: Direct methods
|
|
112
166
|
Only the methods specified by the `functions` preset configuration are available using this method.
|
|
113
167
|
|
|
114
168
|
```typescript
|
|
@@ -123,7 +177,7 @@ const contentFromId = await getContentById(client, { guidValue: '00000000-0000-0
|
|
|
123
177
|
const contentFromPath = await getContentByPath(client, { path: '/en' })
|
|
124
178
|
```
|
|
125
179
|
|
|
126
|
-
### Option 2: Enhanced Client
|
|
180
|
+
### 4.2. Option 2: Enhanced Client
|
|
127
181
|
All GraphQL operations (e.g. Queries, Mutations, ...) defined within the documents are available using this method.
|
|
128
182
|
|
|
129
183
|
```typescript
|
|
@@ -138,11 +192,3 @@ const locale = Schema.Locales.En
|
|
|
138
192
|
|
|
139
193
|
const contentItem = await client.getContentById({ guidValue: contentId, locale })
|
|
140
194
|
```
|
|
141
|
-
|
|
142
|
-
## Query Fragments
|
|
143
|
-
The preset automatically injects a number of fragments and documents into the generated code. These can be found in their respective document:
|
|
144
|
-
|
|
145
|
-
- [`opti-cms:/queries/13`](./src/documents/queries.cms13.ts)
|
|
146
|
-
- [`opti-cms:/queries/12`](./src/documents/queries.cms12.ts)
|
|
147
|
-
- [`opti-cms:/fragments/13`](./src/documents/fragments.cms13.ts)
|
|
148
|
-
- [`opti-cms:/fragments/12`](./src/documents/fragments.cms12.ts)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import type { PresetOptions } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Remove the "item" field in queries and fragments from the "ContentReference" type if it's not in the schema
|
|
5
|
+
*
|
|
6
|
+
* @param files
|
|
7
|
+
* @param options
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function handleDependDirective(files: Types.DocumentFile[], options: Types.PresetFnArgs<PresetOptions>): Promise<Types.DocumentFile[]>;
|
|
11
|
+
export default handleDependDirective;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleDependDirective = handleDependDirective;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
/**
|
|
6
|
+
* Remove the "item" field in queries and fragments from the "ContentReference" type if it's not in the schema
|
|
7
|
+
*
|
|
8
|
+
* @param files
|
|
9
|
+
* @param options
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
async function handleDependDirective(files, options) {
|
|
13
|
+
// Debug output
|
|
14
|
+
if (options.presetConfig.verbose)
|
|
15
|
+
console.log(`✨ [Optimizely] Checking ContentReference to determine if it has an "item" field`);
|
|
16
|
+
// Ouch, this is an old SaaS CMS instance, so we're going to clean this field
|
|
17
|
+
if (options.presetConfig.verbose)
|
|
18
|
+
console.log(`✨ [Optimizely] Nope it hasn't removing any reference to it`);
|
|
19
|
+
const filteredFiles = files.map((file) => {
|
|
20
|
+
let isModified = false;
|
|
21
|
+
const newDocument = file.document ? (0, graphql_1.visit)(file.document, {
|
|
22
|
+
Field: {
|
|
23
|
+
enter(node) {
|
|
24
|
+
// Check if the field has the `depend` directive
|
|
25
|
+
const dependDirective = node.directives?.find(x => x.name.value === 'depend');
|
|
26
|
+
if (dependDirective) {
|
|
27
|
+
// If so read the arguments and validate if the required "on" argument is there
|
|
28
|
+
const args = parseArgs(dependDirective.arguments);
|
|
29
|
+
const dependency = args.get('on');
|
|
30
|
+
if (typeof dependency !== 'string' || dependency.length === 0)
|
|
31
|
+
throw new Error(`The "@depend" directive requires the parameter "on" to be a non-empty string ${buildLocString(dependDirective)}`);
|
|
32
|
+
// Parse & validate the argument
|
|
33
|
+
const [typeName, fieldName, ...remaining] = dependency.split('.');
|
|
34
|
+
if (remaining.length > 0)
|
|
35
|
+
throw new Error(`The "on" parameter of the "@depend" directive must have the form "typeName.fieldName" ${buildLocString(dependDirective)}`);
|
|
36
|
+
// Retrieve the type fields
|
|
37
|
+
const fields = getObjectFieldNames(options.schema, typeName);
|
|
38
|
+
isModified = true;
|
|
39
|
+
if (!(fields?.includes(fieldName) ?? false)) {
|
|
40
|
+
return null; // Remove the item
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const newNode = {
|
|
44
|
+
...node,
|
|
45
|
+
directives: node.directives?.filter(x => x.name.value !== 'depend')
|
|
46
|
+
};
|
|
47
|
+
return newNode;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}) : undefined;
|
|
53
|
+
return isModified
|
|
54
|
+
? {
|
|
55
|
+
...file,
|
|
56
|
+
rawSDL: newDocument ? (0, graphql_1.print)(newDocument) : undefined,
|
|
57
|
+
document: newDocument,
|
|
58
|
+
}
|
|
59
|
+
: file;
|
|
60
|
+
});
|
|
61
|
+
return filteredFiles;
|
|
62
|
+
}
|
|
63
|
+
exports.default = handleDependDirective;
|
|
64
|
+
function buildLocString(node) {
|
|
65
|
+
if (!node.loc)
|
|
66
|
+
return "";
|
|
67
|
+
const sourceName = node.loc.source.name;
|
|
68
|
+
const startLine = node.loc.startToken.line;
|
|
69
|
+
const startChar = node.loc.startToken.column;
|
|
70
|
+
return `in ${sourceName} at line ${startLine}, position ${startChar}`;
|
|
71
|
+
}
|
|
72
|
+
function parseArgs(args) {
|
|
73
|
+
return args?.reduce((out, arg) => {
|
|
74
|
+
const argName = arg.name.value;
|
|
75
|
+
switch (arg.value.kind) {
|
|
76
|
+
case graphql_1.Kind.INT:
|
|
77
|
+
out.set(argName, parseInt(arg.value.value));
|
|
78
|
+
break;
|
|
79
|
+
case graphql_1.Kind.BOOLEAN:
|
|
80
|
+
out.set(argName, new Boolean(arg.value.value));
|
|
81
|
+
break;
|
|
82
|
+
case graphql_1.Kind.STRING:
|
|
83
|
+
out.set(argName, arg.value.value);
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
throw new Error(`Error parsing directive arguments, encountered unsupported argument kind ${arg.value.kind} for ${argName} ${buildLocString(arg)}`);
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}, new Map()) ?? new Map();
|
|
90
|
+
}
|
|
91
|
+
function getObjectFieldNames(schema, objectName) {
|
|
92
|
+
let currentObjectName;
|
|
93
|
+
const objectFields = [];
|
|
94
|
+
let hasType = false;
|
|
95
|
+
(0, graphql_1.visit)(schema, {
|
|
96
|
+
ObjectTypeDefinition: {
|
|
97
|
+
enter(node) {
|
|
98
|
+
if (node.name.value === objectName)
|
|
99
|
+
hasType = true;
|
|
100
|
+
currentObjectName = node.name.value;
|
|
101
|
+
},
|
|
102
|
+
leave(node) {
|
|
103
|
+
if (currentObjectName === node.name.value)
|
|
104
|
+
currentObjectName = undefined;
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
FieldDefinition: {
|
|
108
|
+
enter(node) {
|
|
109
|
+
if (currentObjectName === objectName)
|
|
110
|
+
objectFields.push(node.name.value);
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
return objectFields;
|
|
115
|
+
}
|
|
116
|
+
function isASTNode(node) {
|
|
117
|
+
return !Array.isArray(node);
|
|
118
|
+
}
|
|
119
|
+
function isNotNullOrUndefined(toTest) {
|
|
120
|
+
return toTest !== null && toTest !== undefined;
|
|
121
|
+
}
|
|
122
|
+
function isFragmentOrOperation(x) {
|
|
123
|
+
if (Array.isArray(x) || x == undefined || x == null)
|
|
124
|
+
return false;
|
|
125
|
+
return (x.kind == graphql_1.Kind.FRAGMENT_DEFINITION ||
|
|
126
|
+
x.kind == graphql_1.Kind.OPERATION_DEFINITION);
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=handleDependDirective.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleDependDirective.js","sourceRoot":"","sources":["../../src/_transform/handleDependDirective.ts"],"names":[],"mappings":";;AAsBA,sDA0DC;AA/ED,qCAWgB;AAGhB;;;;;;GAMG;AACI,KAAK,UAAU,qBAAqB,CACzC,KAA2B,EAC3B,OAA0C;IAE1C,eAAe;IACf,IAAI,OAAO,CAAC,YAAY,CAAC,OAAO;QAC9B,OAAO,CAAC,GAAG,CACT,iFAAiF,CAClF,CAAA;IAEH,6EAA6E;IAC7E,IAAI,OAAO,CAAC,YAAY,CAAC,OAAO;QAC9B,OAAO,CAAC,GAAG,CAAC,4DAA4D,CAAC,CAAA;IAE3E,MAAM,aAAa,GAAyB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QAC7D,IAAI,UAAU,GAAG,KAAK,CAAA;QACtB,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAA,eAAK,EAAC,IAAI,CAAC,QAAQ,EAAE;YACvD,KAAK,EAAE;gBACL,KAAK,CAAC,IAAI;oBACR,gDAAgD;oBAChD,MAAM,eAAe,GAAG,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAA;oBAC7E,IAAI,eAAe,EAAE,CAAC;wBACpB,+EAA+E;wBAC/E,MAAM,IAAI,GAAG,SAAS,CAAC,eAAe,CAAC,SAAS,CAAC,CAAA;wBACjD,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;wBACjC,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC;4BAC3D,MAAM,IAAI,KAAK,CAAC,gFAAiF,cAAc,CAAC,eAAe,CAAC,EAAE,CAAC,CAAA;wBAErI,gCAAgC;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-helpers';
|
|
2
2
|
import type { PresetOptions } from '../types';
|
|
3
|
+
export declare function getComponentDocuments(loader?: string): Promise<Types.CustomDocumentLoader[]>;
|
|
4
|
+
export declare function getInjectionTargetDocuments(loader?: string): Promise<Types.CustomDocumentLoader[]>;
|
|
5
|
+
export declare function injectInjectionTargets(files: Types.DocumentFile[], options: Types.PresetFnArgs<PresetOptions>): Promise<Types.DocumentFile[]>;
|
|
3
6
|
/**
|
|
4
7
|
* Check all fragments within the project and ensure that there's at least a fragment for every
|
|
5
8
|
* content type defined in Optimizely CMS. This assumes that when overriding the fragments the
|
|
@@ -33,27 +33,65 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getComponentDocuments = getComponentDocuments;
|
|
37
|
+
exports.getInjectionTargetDocuments = getInjectionTargetDocuments;
|
|
38
|
+
exports.injectInjectionTargets = injectInjectionTargets;
|
|
36
39
|
exports.injectComponentDocuments = injectComponentDocuments;
|
|
37
40
|
const graphql_1 = require("graphql");
|
|
38
|
-
const
|
|
41
|
+
const generator_1 = require("../generator");
|
|
39
42
|
const OptiCMS = __importStar(require("../cms"));
|
|
40
43
|
const tools_1 = require("./tools");
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
44
|
+
const utils_1 = require("../utils");
|
|
45
|
+
async function getComponentDocuments(loader = '@remkoj/optimizely-graph-functions/contenttype-loader') {
|
|
46
|
+
const componentTypes = await OptiCMS.getContentTypesList(undefined, (ct) => {
|
|
47
|
+
if (!ct.key) // The key is required
|
|
48
|
+
return false;
|
|
49
|
+
if (ct.source === 'graph' || ct.source === 'globalcontract' || ct.source === '_system') // Only CMS managed types are allowed
|
|
50
|
+
return false;
|
|
51
|
+
if (ct.isContract) // Contracts must be ignored
|
|
52
|
+
return false;
|
|
53
|
+
return true;
|
|
54
|
+
});
|
|
55
|
+
const documents = [];
|
|
56
|
+
for (const componentType of componentTypes) {
|
|
57
|
+
const vLoc = generator_1.VirtualLocation.build(componentType, { type: 'fragment', forProperty: false });
|
|
58
|
+
if (vLoc) {
|
|
59
|
+
// Inject virtual location
|
|
60
|
+
const def = {};
|
|
61
|
+
def[vLoc] = { loader };
|
|
62
|
+
documents.push(def);
|
|
63
|
+
// Get properties
|
|
64
|
+
const propertyComponentTypeNames = generator_1.DocumentGenerator.getReferencedPropertyComponents(componentType);
|
|
65
|
+
const propertyComponentTypes = (await Promise.all(propertyComponentTypeNames.map(componentTypeName => OptiCMS.getContentType(componentTypeName)))).filter(utils_1.isNotNullOrUndefined);
|
|
66
|
+
for (const propertyComponentType of propertyComponentTypes) {
|
|
67
|
+
const propVLoc = generator_1.VirtualLocation.build(propertyComponentType, { type: 'fragment', forProperty: true });
|
|
68
|
+
if (propVLoc && !documents.some(x => typeof x === 'object' && x[propVLoc])) {
|
|
69
|
+
// Inject virtual location
|
|
70
|
+
const def = {};
|
|
71
|
+
def[propVLoc] = { loader };
|
|
72
|
+
documents.push(def);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return documents;
|
|
78
|
+
}
|
|
79
|
+
async function getInjectionTargetDocuments(loader = '@remkoj/optimizely-graph-functions/contenttype-loader') {
|
|
80
|
+
const documents = [];
|
|
81
|
+
for (const injectionTarget of generator_1.VirtualLocation.getInjectionTargets()) {
|
|
82
|
+
const vLoc = generator_1.VirtualLocation.build(injectionTarget);
|
|
83
|
+
const def = {};
|
|
84
|
+
def[vLoc] = { loader };
|
|
85
|
+
documents.push(def);
|
|
86
|
+
}
|
|
87
|
+
return documents;
|
|
88
|
+
}
|
|
89
|
+
async function injectInjectionTargets(files, options) {
|
|
51
90
|
const allFragments = (0, tools_1.getAllFragments)(files);
|
|
52
|
-
const allGraphTypes = (0, tools_1.getAllTypeNames)(options.schema);
|
|
53
91
|
const addedFiles = [];
|
|
54
92
|
if (options.presetConfig.verbose)
|
|
55
|
-
console.log(`✨ [Optimizely] Generating injection target fragments
|
|
56
|
-
for (const injectionTarget of
|
|
93
|
+
console.log(`✨ [Optimizely] Generating injection target fragments`);
|
|
94
|
+
for (const injectionTarget of generator_1.VirtualLocation.getInjectionTargets()) {
|
|
57
95
|
if (!allFragments.some(x => x.fragmentName === injectionTarget)) {
|
|
58
96
|
const vLoc = `opti-cms:/injectiontarget/${injectionTarget}`;
|
|
59
97
|
const rawSDL = `fragment ${injectionTarget} on _IContent { ...IContentData }`;
|
|
@@ -67,28 +105,44 @@ async function injectComponentDocuments(files, options) {
|
|
|
67
105
|
});
|
|
68
106
|
}
|
|
69
107
|
}
|
|
108
|
+
return [...files, ...addedFiles];
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Check all fragments within the project and ensure that there's at least a fragment for every
|
|
112
|
+
* content type defined in Optimizely CMS. This assumes that when overriding the fragments the
|
|
113
|
+
* project will ensure that the injections are correct.
|
|
114
|
+
*
|
|
115
|
+
* @param files
|
|
116
|
+
* @param options
|
|
117
|
+
* @returns
|
|
118
|
+
*/
|
|
119
|
+
async function injectComponentDocuments(files, options) {
|
|
120
|
+
const allFragments = (0, tools_1.getAllFragments)(files);
|
|
121
|
+
const allGraphTypes = (0, tools_1.getAllTypeNames)(options.schema);
|
|
122
|
+
const addedFiles = [];
|
|
70
123
|
if (options.presetConfig.verbose)
|
|
71
124
|
console.log(`✨ [Optimizely] Generating component fragments that have not been defined by the implementation`);
|
|
72
|
-
const allContentTypes = OptiCMS.
|
|
125
|
+
const allContentTypes = await OptiCMS.getContentTypes(options.presetConfig.cmsClient, (ct) => {
|
|
73
126
|
return ct.key && ct.source !== 'graph' ? true : false;
|
|
74
127
|
});
|
|
128
|
+
const queryGen = new generator_1.DocumentGenerator(allContentTypes);
|
|
75
129
|
const propTracker = new Map();
|
|
76
|
-
for
|
|
130
|
+
for (const [, contentType] of allContentTypes) {
|
|
77
131
|
// Skip over content types without a key
|
|
78
132
|
if (!contentType.key)
|
|
79
133
|
continue;
|
|
80
134
|
// Construct the type names expected to be in Graph
|
|
81
|
-
const graphType =
|
|
82
|
-
const graphPropertyType =
|
|
83
|
-
const fragmentName =
|
|
84
|
-
const propertyFragmentName =
|
|
135
|
+
const graphType = queryGen.getGraphType(contentType);
|
|
136
|
+
const graphPropertyType = queryGen.getGraphPropertyType(contentType);
|
|
137
|
+
const fragmentName = queryGen.getDefaultFragmentName(contentType);
|
|
138
|
+
const propertyFragmentName = queryGen.getDefaultPropertyFragmentName(contentType);
|
|
85
139
|
// Check if the types exist
|
|
86
140
|
const graphTypeExists = allGraphTypes.includes(graphType);
|
|
87
141
|
const graphPropertyTypeExists = allGraphTypes.includes(graphPropertyType);
|
|
88
142
|
// Add Component Data Fragment
|
|
89
143
|
if (graphTypeExists && !allFragments.has(fragmentName)) {
|
|
90
|
-
const rawSDL =
|
|
91
|
-
const vLoc =
|
|
144
|
+
const rawSDL = queryGen.buildFragment(contentType, fragmentName, false, propTracker);
|
|
145
|
+
const vLoc = generator_1.VirtualLocation.build(contentType);
|
|
92
146
|
if (rawSDL) {
|
|
93
147
|
if (options.presetConfig.verbose)
|
|
94
148
|
console.log(` - Generated fragment ${fragmentName} for ${contentType.key} at ${vLoc}`);
|
|
@@ -102,8 +156,8 @@ async function injectComponentDocuments(files, options) {
|
|
|
102
156
|
}
|
|
103
157
|
// Add Component Property Data Fragment
|
|
104
158
|
if (graphPropertyTypeExists && contentType.baseType === '_component' && !allFragments.has(propertyFragmentName)) {
|
|
105
|
-
const rawSDL =
|
|
106
|
-
const vLoc =
|
|
159
|
+
const rawSDL = queryGen.buildFragment(contentType, propertyFragmentName, true, propTracker);
|
|
160
|
+
const vLoc = generator_1.VirtualLocation.build(contentType, { forProperty: true });
|
|
107
161
|
if (rawSDL) {
|
|
108
162
|
if (options.presetConfig.verbose)
|
|
109
163
|
console.log(` - Generated property fragment ${propertyFragmentName} for ${contentType.key} at ${vLoc}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectComponentDocuments.js","sourceRoot":"","sources":["../../src/_transform/injectComponentDocuments.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"injectComponentDocuments.js","sourceRoot":"","sources":["../../src/_transform/injectComponentDocuments.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,sDAoCC;AAED,kEAUC;AAED,wDAuBC;AAWD,4DAgEC;AA3JD,qCAA+B;AAC/B,4CAAiE;AACjE,gDAAiC;AAEjC,mCAA0D;AAC1D,oCAA+C;AAExC,KAAK,UAAU,qBAAqB,CAAC,SAAiB,uDAAuD;IAElH,MAAM,cAAc,GAAG,MAAM,OAAO,CAAC,mBAAmB,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE,EAAE;QACzE,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,sBAAsB;YACjC,OAAO,KAAK,CAAC;QACf,IAAI,EAAE,CAAC,MAAM,KAAK,OAAO,IAAI,EAAE,CAAC,MAAM,KAAK,gBAAgB,IAAI,EAAE,CAAC,MAAM,KAAK,SAAS,EAAE,qCAAqC;YAC3H,OAAO,KAAK,CAAC;QACf,IAAI,EAAE,CAAC,UAAU,EAAE,4BAA4B;YAC7C,OAAO,KAAK,CAAC;QACf,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;IAEH,MAAM,SAAS,GAAiC,EAAE,CAAC;IACnD,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE,CAAC;QAC3C,MAAM,IAAI,GAAG,2BAAe,CAAC,KAAK,CAAC,aAAa,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC,CAAA;QAC3F,IAAI,IAAI,EAAE,CAAC;YACT,0BAA0B;YAC1B,MAAM,GAAG,G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AAI;iBACX,CAAC,CAAA;YACJ,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAG,KAAK,EAAE,GAAG,UAAU,CAAC,CAAA;AAClC,CAAC;AAED,kBAAe,wBAAwB,CAAA"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Types } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import type { PresetOptions } from '../types';
|
|
3
|
+
export declare function getPageDocuments(loader?: string): Promise<Types.CustomDocumentLoader[]>;
|
|
3
4
|
export declare function injectPageQueries(files: Types.DocumentFile[], options: Types.PresetFnArgs<PresetOptions>): Promise<Types.DocumentFile[]>;
|
|
4
5
|
export default injectPageQueries;
|
|
@@ -33,25 +33,58 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getPageDocuments = getPageDocuments;
|
|
36
37
|
exports.injectPageQueries = injectPageQueries;
|
|
37
38
|
const graphql_1 = require("graphql");
|
|
38
|
-
const QueryGen = __importStar(require("../contenttype-loader"));
|
|
39
39
|
const OptiCMS = __importStar(require("../cms"));
|
|
40
40
|
const tools_1 = require("./tools");
|
|
41
|
+
const utils_1 = require("../utils");
|
|
42
|
+
const generator_1 = require("../generator");
|
|
43
|
+
async function getPageDocuments(loader = '@remkoj/optimizely-graph-functions/contenttype-loader') {
|
|
44
|
+
const pageTypes = OptiCMS.getContentTypesList(undefined, (ct) => {
|
|
45
|
+
if (ct.source == "graph")
|
|
46
|
+
return false;
|
|
47
|
+
return ["_page", "_experience"].includes((ct.baseType || "").toLowerCase());
|
|
48
|
+
});
|
|
49
|
+
const documents = [];
|
|
50
|
+
for (const pageType of await pageTypes) {
|
|
51
|
+
const vLoc = generator_1.VirtualLocation.build(pageType, { type: 'query' });
|
|
52
|
+
if (vLoc) {
|
|
53
|
+
const def = {};
|
|
54
|
+
def[vLoc] = { loader };
|
|
55
|
+
documents.push(def);
|
|
56
|
+
}
|
|
57
|
+
// Get properties
|
|
58
|
+
const propertyComponentTypeNames = generator_1.DocumentGenerator.getReferencedPropertyComponents(pageType);
|
|
59
|
+
const propertyComponentTypes = (await Promise.all(propertyComponentTypeNames.map(componentTypeName => OptiCMS.getContentType(componentTypeName)))).filter(utils_1.isNotNullOrUndefined);
|
|
60
|
+
for (const propertyComponentType of propertyComponentTypes) {
|
|
61
|
+
const propVLoc = generator_1.VirtualLocation.build(propertyComponentType, { type: 'fragment', forProperty: true });
|
|
62
|
+
if (propVLoc && !documents.some(x => typeof x === 'object' && x[propVLoc])) {
|
|
63
|
+
// Inject virtual location
|
|
64
|
+
const def = {};
|
|
65
|
+
def[propVLoc] = { loader };
|
|
66
|
+
documents.push(def);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return documents;
|
|
71
|
+
}
|
|
41
72
|
async function injectPageQueries(files, options) {
|
|
42
73
|
if (options.presetConfig.verbose)
|
|
43
74
|
console.log(`✨ [Optimizely] Generating page queries that have not been defined by the implementation`);
|
|
44
75
|
const existingQueries = (0, tools_1.getAllQueries)(files);
|
|
45
76
|
const existingTypes = (0, tools_1.getAllTypeNames)(options.schema);
|
|
46
|
-
const
|
|
77
|
+
const allTypes = OptiCMS.getContentTypes(undefined);
|
|
78
|
+
const pageLevelTypes = OptiCMS.getContentTypesList(undefined, (ct) => {
|
|
47
79
|
if (ct.source == "graph")
|
|
48
80
|
return false;
|
|
49
81
|
return ["_page", "_experience"].includes((ct.baseType || "").toLowerCase());
|
|
50
82
|
});
|
|
83
|
+
const queryGen = new generator_1.DocumentGenerator(await allTypes);
|
|
51
84
|
const newFiles = [];
|
|
52
|
-
for
|
|
53
|
-
const graphDataType =
|
|
54
|
-
const queryName =
|
|
85
|
+
for (const pageContentType of await pageLevelTypes) {
|
|
86
|
+
const graphDataType = queryGen.getGraphType(pageContentType);
|
|
87
|
+
const queryName = queryGen.getDefaultQueryName(pageContentType);
|
|
55
88
|
// Check if the type exists in the Schema, if not skip it
|
|
56
89
|
if (!existingTypes.includes(graphDataType)) {
|
|
57
90
|
if (options.presetConfig.verbose)
|
|
@@ -64,8 +97,8 @@ async function injectPageQueries(files, options) {
|
|
|
64
97
|
console.log(` - Skipping query generation for ${pageContentType.key}, the project already includes a definition for ${queryName}`);
|
|
65
98
|
continue;
|
|
66
99
|
}
|
|
67
|
-
const rawSDL =
|
|
68
|
-
const vLoc =
|
|
100
|
+
const rawSDL = queryGen.buildGetQuery(pageContentType, queryName);
|
|
101
|
+
const vLoc = generator_1.VirtualLocation.build(pageContentType, { type: 'query' });
|
|
69
102
|
if (options.presetConfig.verbose)
|
|
70
103
|
console.log(` - Generated query for ${pageContentType.key} at ${vLoc}`);
|
|
71
104
|
newFiles.push({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectPageQueries.js","sourceRoot":"","sources":["../../src/_transform/injectPageQueries.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"injectPageQueries.js","sourceRoot":"","sources":["../../src/_transform/injectPageQueries.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AASA,4CA+BC;AAED,8CAgDC;AAzFD,qCAA+B;AAE/B,gDAAiC;AAEjC,mCAAwD;AACxD,oCAA+C;AAC/C,4CAAiE;AAE1D,KAAK,UAAU,gBAAgB,CAAC,SAAiB,uDAAuD;IAE7G,MAAM,SAAS,GAAG,OAAO,CAAC,mBAAmB,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE,EAAE;QAC9D,IAAI,EAAE,CAAC,MAAM,IAAI,OAAO;YACtB,OAAO,KAAK,CAAC;QACf,OAAO,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;IAC9E,CAAC,CA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