@remkoj/optimizely-graph-functions 6.0.0-pre1 → 6.0.0-pre11
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/LICENSE +12 -12
- package/README.md +56 -14
- package/dist/_transform/cleanFragments.d.ts +10 -0
- package/dist/_transform/cleanFragments.js +39 -0
- package/dist/_transform/cleanFragments.js.map +1 -0
- package/dist/_transform/cleanSpreads.d.ts +10 -0
- package/dist/_transform/cleanSpreads.js +36 -0
- package/dist/_transform/cleanSpreads.js.map +1 -0
- package/dist/_transform/handleDependDirective.d.ts +11 -0
- package/dist/_transform/handleDependDirective.js +121 -0
- package/dist/_transform/handleDependDirective.js.map +1 -0
- package/dist/_transform/injectComponentDocuments.d.ts +13 -0
- package/dist/_transform/injectComponentDocuments.js +122 -0
- package/dist/_transform/injectComponentDocuments.js.map +1 -0
- package/dist/_transform/injectPageQueries.d.ts +4 -0
- package/dist/_transform/injectPageQueries.js +81 -0
- package/dist/_transform/injectPageQueries.js.map +1 -0
- package/dist/_transform/injectSectionQueries.d.ts +4 -0
- package/dist/_transform/injectSectionQueries.js +81 -0
- package/dist/_transform/injectSectionQueries.js.map +1 -0
- package/dist/_transform/normalizeFragmentNames.d.ts +12 -0
- package/dist/_transform/normalizeFragmentNames.js +75 -0
- package/dist/_transform/normalizeFragmentNames.js.map +1 -0
- package/dist/_transform/normalizeQueryNames.d.ts +12 -0
- package/dist/_transform/normalizeQueryNames.js +83 -0
- package/dist/_transform/normalizeQueryNames.js.map +1 -0
- package/dist/_transform/options.d.ts +3 -0
- package/dist/_transform/options.js +21 -0
- package/dist/_transform/options.js.map +1 -0
- package/dist/_transform/performInjections.d.ts +24 -0
- package/dist/_transform/performInjections.js +225 -0
- package/dist/_transform/performInjections.js.map +1 -0
- package/dist/_transform/removeReferenceItem.d.ts +11 -0
- package/dist/_transform/removeReferenceItem.js +113 -0
- package/dist/_transform/removeReferenceItem.js.map +1 -0
- package/dist/_transform/tools.d.ts +34 -0
- package/dist/_transform/tools.js +103 -0
- package/dist/_transform/tools.js.map +1 -0
- package/dist/cms/index.d.ts +17 -0
- package/dist/cms/index.js +88 -0
- package/dist/cms/index.js.map +1 -0
- package/dist/contenttype-loader.d.ts +58 -0
- package/dist/contenttype-loader.js +332 -0
- package/dist/contenttype-loader.js.map +1 -0
- package/dist/document-ast-plugin.d.ts +1 -0
- package/dist/document-ast-plugin.js +15 -0
- package/dist/document-ast-plugin.js.map +1 -0
- package/dist/documents/fragments.cms13.js +148 -39
- package/dist/documents/fragments.cms13.js.map +1 -1
- package/dist/documents/queries.cms13.js +9 -3
- package/dist/documents/queries.cms13.js.map +1 -1
- package/dist/embedded-loader.js +31 -16
- package/dist/embedded-loader.js.map +1 -1
- package/dist/index.d.ts +2 -5
- package/dist/index.js.map +1 -1
- package/dist/preset.d.ts +2 -4
- package/dist/preset.js +38 -43
- package/dist/preset.js.map +1 -1
- package/dist/tools.d.ts +4 -0
- package/dist/tools.js +20 -0
- package/dist/tools.js.map +1 -0
- package/dist/transform.d.ts +12 -1
- package/dist/transform.js +43 -58
- package/dist/transform.js.map +1 -1
- package/dist/types.d.ts +34 -0
- package/package.json +20 -13
package/LICENSE
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
Copyright 2024 Remko Jantzen
|
|
2
|
-
|
|
3
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
you may not use this file except in compliance with the License.
|
|
5
|
-
You may obtain a copy of the License at
|
|
6
|
-
|
|
7
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
|
|
9
|
-
Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
See the License for the specific language governing permissions and
|
|
1
|
+
Copyright 2024 Remko Jantzen
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
13
|
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Optimizely GraphQL Codegen Plugin
|
|
1
|
+
# Optimizely GraphQL Codegen Plugin <!-- omit in toc -->
|
|
2
2
|
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
3
|
|
|
4
4
|
[Release notes](https://github.com/remkoj/optimizely-dxp-clients/releases)
|
|
@@ -6,7 +6,22 @@ GraphQL Codegen plugin and preset which generate both the GraphQL type definitio
|
|
|
6
6
|
> [!WARNING]
|
|
7
7
|
> 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
8
|
|
|
9
|
-
##
|
|
9
|
+
## Contents <!-- omit in toc -->
|
|
10
|
+
- [1. Install package](#1-install-package)
|
|
11
|
+
- [2. Configure package](#2-configure-package)
|
|
12
|
+
- [3. GraphQL Document Processing](#3-graphql-document-processing)
|
|
13
|
+
- [3.1. Allow overwriting of built-in fragments \& queries](#31-allow-overwriting-of-built-in-fragments--queries)
|
|
14
|
+
- [3.2. Injection of fragments \& queries for ContentTypes](#32-injection-of-fragments--queries-for-contenttypes)
|
|
15
|
+
- [3.3. Auto inject fragments](#33-auto-inject-fragments)
|
|
16
|
+
- [3.4. Remove fragments and spreads that target non-existing types](#34-remove-fragments-and-spreads-that-target-non-existing-types)
|
|
17
|
+
- [3.5. Dedicated Queries \& Fragments](#35-dedicated-queries--fragments)
|
|
18
|
+
- [Compiler field directive `@depend`](#compiler-field-directive-depend)
|
|
19
|
+
- [4. Usage](#4-usage)
|
|
20
|
+
- [4.1. Option 1: Direct methods](#41-option-1-direct-methods)
|
|
21
|
+
- [4.2. Option 2: Enhanced Client](#42-option-2-enhanced-client)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## 1. Install package
|
|
10
25
|
To install using Yarn, use the following command:
|
|
11
26
|
|
|
12
27
|
```bash
|
|
@@ -19,7 +34,7 @@ yarn add --dev @remkoj/optimizely-graph-cli
|
|
|
19
34
|
yarn opti-graph patches:apply
|
|
20
35
|
```
|
|
21
36
|
|
|
22
|
-
## Configure package
|
|
37
|
+
## 2. Configure package
|
|
23
38
|
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
39
|
|
|
25
40
|
```typescript
|
|
@@ -104,11 +119,46 @@ The presetConfig of the `OptimizelyGraphPreset` is an extension of the configura
|
|
|
104
119
|
| 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
120
|
| verbose | Set to `true` to enable debugging output of the preset, loader, plugin and transform |
|
|
106
121
|
|
|
122
|
+
## 3. GraphQL Document Processing
|
|
123
|
+
### 3.1. Allow overwriting of built-in fragments & queries
|
|
124
|
+
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:
|
|
125
|
+
- If there's no query or fragment with the name without the leading underscore, it will be renamed. (i.e. `_getContentById` becomes `getContentById`)
|
|
126
|
+
- 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.
|
|
107
127
|
|
|
108
|
-
|
|
128
|
+
This allows a project to overwrite built-in fragments and queries, while still ensuring type-safety.
|
|
129
|
+
|
|
130
|
+
### 3.2. Injection of fragments & queries for ContentTypes
|
|
131
|
+
For each content type the appropriate fragments and queries will be auto generated an injected into the document set.
|
|
132
|
+
|
|
133
|
+
### 3.3. Auto inject fragments
|
|
134
|
+
Dynamically build queries & fragments based upon the `injections` configuration, and the default injections from the auto-generated fragments & queries.
|
|
135
|
+
|
|
136
|
+
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.
|
|
137
|
+
|
|
138
|
+
### 3.4. Remove fragments and spreads that target non-existing types
|
|
139
|
+
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.
|
|
140
|
+
|
|
141
|
+
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.
|
|
142
|
+
|
|
143
|
+
### 3.5. Dedicated Queries & Fragments
|
|
144
|
+
The preset automatically injects a number of fragments and documents into the generated code. These can be found in their respective document:
|
|
145
|
+
|
|
146
|
+
- [`opti-cms:/queries/13`](./src/documents/queries.cms13.ts)
|
|
147
|
+
- [`opti-cms:/queries/12`](./src/documents/queries.cms12.ts)
|
|
148
|
+
- [`opti-cms:/fragments/13`](./src/documents/fragments.cms13.ts)
|
|
149
|
+
- [`opti-cms:/fragments/12`](./src/documents/fragments.cms12.ts)
|
|
150
|
+
|
|
151
|
+
### Compiler field directive `@depend`
|
|
152
|
+
A compile time directive `@depend` is processed, allowing you to make selections depend on a field to present in the schema.
|
|
153
|
+
|
|
154
|
+
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`.
|
|
155
|
+
|
|
156
|
+
This allows to write generic queries that can cope with differen configurations within the CMS.
|
|
157
|
+
|
|
158
|
+
## 4. Usage
|
|
109
159
|
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
160
|
|
|
111
|
-
### Option 1: Direct methods
|
|
161
|
+
### 4.1. Option 1: Direct methods
|
|
112
162
|
Only the methods specified by the `functions` preset configuration are available using this method.
|
|
113
163
|
|
|
114
164
|
```typescript
|
|
@@ -123,7 +173,7 @@ const contentFromId = await getContentById(client, { guidValue: '00000000-0000-0
|
|
|
123
173
|
const contentFromPath = await getContentByPath(client, { path: '/en' })
|
|
124
174
|
```
|
|
125
175
|
|
|
126
|
-
### Option 2: Enhanced Client
|
|
176
|
+
### 4.2. Option 2: Enhanced Client
|
|
127
177
|
All GraphQL operations (e.g. Queries, Mutations, ...) defined within the documents are available using this method.
|
|
128
178
|
|
|
129
179
|
```typescript
|
|
@@ -138,11 +188,3 @@ const locale = Schema.Locales.En
|
|
|
138
188
|
|
|
139
189
|
const contentItem = await client.getContentById({ guidValue: contentId, locale })
|
|
140
190
|
```
|
|
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,10 @@
|
|
|
1
|
+
import { type Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import type { PresetOptions } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Remove all fragments that target a type that is not present in the Schema
|
|
5
|
+
*
|
|
6
|
+
* @param files
|
|
7
|
+
* @param options
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function cleanFragments(files: Types.DocumentFile[], options: Types.PresetFnArgs<PresetOptions>): Types.DocumentFile[];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cleanFragments = cleanFragments;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
/**
|
|
6
|
+
* Remove all fragments that target a type that is not present in the Schema
|
|
7
|
+
*
|
|
8
|
+
* @param files
|
|
9
|
+
* @param options
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
function cleanFragments(files, options) {
|
|
13
|
+
if (options.presetConfig.verbose)
|
|
14
|
+
console.log(`✨ [Optimizely] Removing fragments that target non-existing types from the documents`);
|
|
15
|
+
return files.map(doc => {
|
|
16
|
+
if (doc.document) {
|
|
17
|
+
let isModified = false;
|
|
18
|
+
const newDocument = (0, graphql_1.visit)(doc.document, {
|
|
19
|
+
FragmentDefinition: {
|
|
20
|
+
enter: (node) => {
|
|
21
|
+
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)) {
|
|
22
|
+
if (options.presetConfig.verbose)
|
|
23
|
+
console.log(`⚠ Removing fragment ${node.name.value} from the documents, as its target ${node.typeCondition.name.value} is not available in the schema`);
|
|
24
|
+
isModified = true;
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return isModified ? {
|
|
31
|
+
...doc,
|
|
32
|
+
rawSDL: (0, graphql_1.print)(newDocument),
|
|
33
|
+
document: newDocument
|
|
34
|
+
} : doc;
|
|
35
|
+
}
|
|
36
|
+
return doc;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=cleanFragments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cleanFragments.js","sourceRoot":"","sources":["../../src/_transform/cleanFragments.ts"],"names":[],"mappings":";;AAWA,wCA0BC;AApCD,qCAAmD;AA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A;AACJ,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import type { PresetOptions } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Remove all fragment spreads that target non-existing fragments from the documents.
|
|
5
|
+
*
|
|
6
|
+
* @param files
|
|
7
|
+
* @param options
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function cleanFragmentSpreads(files: Types.DocumentFile[], options: Types.PresetFnArgs<PresetOptions>): Types.DocumentFile[];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cleanFragmentSpreads = cleanFragmentSpreads;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const tools_1 = require("./tools");
|
|
6
|
+
/**
|
|
7
|
+
* Remove all fragment spreads that target non-existing fragments from the documents.
|
|
8
|
+
*
|
|
9
|
+
* @param files
|
|
10
|
+
* @param options
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
function cleanFragmentSpreads(files, options) {
|
|
14
|
+
const allFragments = (0, tools_1.getAllFragments)(files);
|
|
15
|
+
return files.map(file => {
|
|
16
|
+
if (!file.document)
|
|
17
|
+
return file;
|
|
18
|
+
let isModified = false;
|
|
19
|
+
const newDocument = (0, graphql_1.visit)(file.document, {
|
|
20
|
+
FragmentSpread: {
|
|
21
|
+
enter: (node) => {
|
|
22
|
+
if (!allFragments.some(x => x.fragmentName === node.name.value)) {
|
|
23
|
+
isModified = true;
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return isModified ? {
|
|
30
|
+
...file,
|
|
31
|
+
rawSDL: (0, graphql_1.print)(newDocument),
|
|
32
|
+
document: newDocument
|
|
33
|
+
} : file;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=cleanSpreads.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cleanSpreads.js","sourceRoot":"","sources":["../../src/_transform/cleanSpreads.ts"],"names":[],"mappings":";;AAYA,oDAwBC;AAnCD,qCAAmD;AAEnD,mCAAyC;AAEzC;;;;;;GAMG;AACH,SAAgB,oBAAoB,CAAC,KAA2B,EAAE,OAA0C;IAC1G,MAAM,YAAY,GAAG,IAAA,uBAAe,EAAC,KAAK,CAAC,CAAA;IAC3C,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;QACtB,IAAI,CAAC,IAAI,CAAC,QAAQ;YAChB,OAAO,IAAI,CAAC;QAEd,IAAI,UAAU,GAAY,KAAK,CAAC;QAChC,MAAM,WAAW,GAAG,IAAA,eAAK,EAAC,IAAI,CAAC,QAAQ,EAAE;YACvC,cAAc,EAAE;gBACd,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE;oBACd,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,KAAK,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;wBAChE,UAAU,GAAG,IAAI,CAAC;wBAClB,OAAO,IAAI,CAAC;oBACd,CAAC;gBACH,CAAC;aACF;SACF,CAAC,CAAC;QAEH,OAAO,UAAU,CAAC,CAAC,CAAC;YAClB,GAAG,IAAI;YACP,MAAM,EAAE,IAAA,eAAK,EAAC,WAAW,CAAC;YAC1B,QAAQ,EAAE,WAAW;SACtB,CAAC,CAAC,CAAC,IAAI,CAAA;IACV,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -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,121 @@
|
|
|
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
|
+
if (!(fields?.includes(fieldName) ?? false)) {
|
|
39
|
+
isModified = true;
|
|
40
|
+
return null; // Remove the item
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}) : undefined;
|
|
46
|
+
return isModified
|
|
47
|
+
? {
|
|
48
|
+
...file,
|
|
49
|
+
rawSDL: newDocument ? (0, graphql_1.print)(newDocument) : undefined,
|
|
50
|
+
document: newDocument,
|
|
51
|
+
}
|
|
52
|
+
: file;
|
|
53
|
+
});
|
|
54
|
+
return filteredFiles;
|
|
55
|
+
}
|
|
56
|
+
exports.default = handleDependDirective;
|
|
57
|
+
function buildLocString(node) {
|
|
58
|
+
if (!node.loc)
|
|
59
|
+
return "";
|
|
60
|
+
const sourceName = node.loc.source.name;
|
|
61
|
+
const startLine = node.loc.startToken.line;
|
|
62
|
+
const startChar = node.loc.startToken.column;
|
|
63
|
+
return `in ${sourceName} at line ${startLine}, position ${startChar}`;
|
|
64
|
+
}
|
|
65
|
+
function parseArgs(args) {
|
|
66
|
+
return args?.reduce((out, arg) => {
|
|
67
|
+
const argName = arg.name.value;
|
|
68
|
+
switch (arg.value.kind) {
|
|
69
|
+
case graphql_1.Kind.INT:
|
|
70
|
+
out.set(argName, parseInt(arg.value.value));
|
|
71
|
+
break;
|
|
72
|
+
case graphql_1.Kind.BOOLEAN:
|
|
73
|
+
out.set(argName, new Boolean(arg.value.value));
|
|
74
|
+
break;
|
|
75
|
+
case graphql_1.Kind.STRING:
|
|
76
|
+
out.set(argName, arg.value.value);
|
|
77
|
+
break;
|
|
78
|
+
default:
|
|
79
|
+
throw new Error(`Error parsing directive arguments, encountered unsupported argument kind ${arg.value.kind} for ${argName} ${buildLocString(arg)}`);
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}, new Map()) ?? new Map();
|
|
83
|
+
}
|
|
84
|
+
function getObjectFieldNames(schema, objectName) {
|
|
85
|
+
let currentObjectName;
|
|
86
|
+
let objectFields = [];
|
|
87
|
+
let hasType = false;
|
|
88
|
+
(0, graphql_1.visit)(schema, {
|
|
89
|
+
ObjectTypeDefinition: {
|
|
90
|
+
enter(node) {
|
|
91
|
+
if (node.name.value === objectName)
|
|
92
|
+
hasType = true;
|
|
93
|
+
currentObjectName = node.name.value;
|
|
94
|
+
},
|
|
95
|
+
leave(node) {
|
|
96
|
+
if (currentObjectName === node.name.value)
|
|
97
|
+
currentObjectName = undefined;
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
FieldDefinition: {
|
|
101
|
+
enter(node) {
|
|
102
|
+
if (currentObjectName === objectName)
|
|
103
|
+
objectFields.push(node.name.value);
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
return objectFields;
|
|
108
|
+
}
|
|
109
|
+
function isASTNode(node) {
|
|
110
|
+
return !Array.isArray(node);
|
|
111
|
+
}
|
|
112
|
+
function isNotNullOrUndefined(toTest) {
|
|
113
|
+
return toTest !== null && toTest !== undefined;
|
|
114
|
+
}
|
|
115
|
+
function isFragmentOrOperation(x) {
|
|
116
|
+
if (Array.isArray(x) || x == undefined || x == null)
|
|
117
|
+
return false;
|
|
118
|
+
return (x.kind == graphql_1.Kind.FRAGMENT_DEFINITION ||
|
|
119
|
+
x.kind == graphql_1.Kind.OPERATION_DEFINITION);
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=handleDependDirective.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleDependDirective.js","sourceRoot":"","sources":["../../src/_transform/handleDependDirective.ts"],"names":[],"mappings":";;AA2BA,sDAqDC;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,KAAK;oBAAE,iBAAiB,GAAG,SAAS,CAAA;YAC1E,CAAC;SACF;QACD,eAAe,EAAE;YACf,KAAK,CAAC,IAAI;gBACR,IAAI,iBAAiB,KAAK,UAAU;oBAAE,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YAC1E,CAAC;SACF;KACF,CAAC,CAAA;IACF,OAAO,YAAY,CAAA;AACrB,CAAC;AAED,SAAS,SAAS,CAAC,IAAkC;IAEnD,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;AAC7B,CAAC;AAED,SAAS,oBAAoB,CAAI,MAAiB;IAChD,OAAO,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS,CAAA;AAChD,CAAC;AACD,SAAS,qBAAqB,CAC5B,CAAmD;IAEnD,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,SAAS,IAAI,CAAC,IAAI,IAAI;QAAE,OAAO,KAAK,CAAA;IACjE,OAAO,CACJ,CAAa,CAAC,IAAI,IAAI,cAAI,CAAC,mBAAmB;QAC9C,CAAa,CAAC,IAAI,IAAI,cAAI,CAAC,oBAAoB,CACjD,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import type { PresetOptions } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Check all fragments within the project and ensure that there's at least a fragment for every
|
|
5
|
+
* content type defined in Optimizely CMS. This assumes that when overriding the fragments the
|
|
6
|
+
* project will ensure that the injections are correct.
|
|
7
|
+
*
|
|
8
|
+
* @param files
|
|
9
|
+
* @param options
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export declare function injectComponentDocuments(files: Types.DocumentFile[], options: Types.PresetFnArgs<PresetOptions>): Promise<Types.DocumentFile[]>;
|
|
13
|
+
export default injectComponentDocuments;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.injectComponentDocuments = injectComponentDocuments;
|
|
37
|
+
const graphql_1 = require("graphql");
|
|
38
|
+
const QueryGen = __importStar(require("../contenttype-loader"));
|
|
39
|
+
const OptiCMS = __importStar(require("../cms"));
|
|
40
|
+
const tools_1 = require("./tools");
|
|
41
|
+
/**
|
|
42
|
+
* Check all fragments within the project and ensure that there's at least a fragment for every
|
|
43
|
+
* content type defined in Optimizely CMS. This assumes that when overriding the fragments the
|
|
44
|
+
* project will ensure that the injections are correct.
|
|
45
|
+
*
|
|
46
|
+
* @param files
|
|
47
|
+
* @param options
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
async function injectComponentDocuments(files, options) {
|
|
51
|
+
const allFragments = (0, tools_1.getAllFragments)(files);
|
|
52
|
+
const allGraphTypes = (0, tools_1.getAllTypeNames)(options.schema);
|
|
53
|
+
const addedFiles = [];
|
|
54
|
+
if (options.presetConfig.verbose)
|
|
55
|
+
console.log(`✨ [Optimizely] Generating injection target fragments that have not been defined by the implementation`);
|
|
56
|
+
for (const injectionTarget of Object.getOwnPropertyNames(QueryGen.ContentTypeTarget)) {
|
|
57
|
+
if (!allFragments.some(x => x.fragmentName === injectionTarget)) {
|
|
58
|
+
const vLoc = `opti-cms:/injectiontarget/${injectionTarget}`;
|
|
59
|
+
const rawSDL = `fragment ${injectionTarget} on _IContent { ...IContentData }`;
|
|
60
|
+
if (options.presetConfig.verbose)
|
|
61
|
+
console.log(` - Generated fragment ${injectionTarget} at ${vLoc}`);
|
|
62
|
+
addedFiles.push({
|
|
63
|
+
rawSDL,
|
|
64
|
+
document: (0, graphql_1.parse)(rawSDL),
|
|
65
|
+
location: vLoc,
|
|
66
|
+
hash: vLoc
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (options.presetConfig.verbose)
|
|
71
|
+
console.log(`✨ [Optimizely] Generating component fragments that have not been defined by the implementation`);
|
|
72
|
+
const allContentTypes = OptiCMS.getAllContentTypes(options.presetConfig.cmsClient, 100, (ct) => {
|
|
73
|
+
return ct.key && ct.source !== 'graph' ? true : false;
|
|
74
|
+
});
|
|
75
|
+
const propTracker = new Map();
|
|
76
|
+
for await (const contentType of allContentTypes) {
|
|
77
|
+
// Skip over content types without a key
|
|
78
|
+
if (!contentType.key)
|
|
79
|
+
continue;
|
|
80
|
+
// Construct the type names expected to be in Graph
|
|
81
|
+
const graphType = QueryGen.getGraphType(contentType);
|
|
82
|
+
const graphPropertyType = QueryGen.getGraphPropertyType(contentType);
|
|
83
|
+
const fragmentName = QueryGen.Tools.ucFirst(graphType) + "Data";
|
|
84
|
+
const propertyFragmentName = QueryGen.Tools.ucFirst(graphPropertyType) + "Data";
|
|
85
|
+
// Check if the types exist
|
|
86
|
+
const graphTypeExists = allGraphTypes.includes(graphType);
|
|
87
|
+
const graphPropertyTypeExists = allGraphTypes.includes(graphPropertyType);
|
|
88
|
+
// Add Component Data Fragment
|
|
89
|
+
if (graphTypeExists && !allFragments.has(fragmentName)) {
|
|
90
|
+
const rawSDL = QueryGen.buildFragment(contentType, fragmentName, false, propTracker);
|
|
91
|
+
const vLoc = QueryGen.buildVirtualLocation(contentType);
|
|
92
|
+
if (rawSDL) {
|
|
93
|
+
if (options.presetConfig.verbose)
|
|
94
|
+
console.log(` - Generated fragment ${fragmentName} for ${contentType.key} at ${vLoc}`);
|
|
95
|
+
addedFiles.push({
|
|
96
|
+
rawSDL,
|
|
97
|
+
document: (0, graphql_1.parse)(rawSDL),
|
|
98
|
+
location: vLoc,
|
|
99
|
+
hash: vLoc
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// Add Component Property Data Fragment
|
|
104
|
+
if (graphPropertyTypeExists && contentType.baseType === '_component' && !allFragments.has(propertyFragmentName)) {
|
|
105
|
+
const rawSDL = QueryGen.buildFragment(contentType, propertyFragmentName, true, propTracker);
|
|
106
|
+
const vLoc = QueryGen.buildVirtualLocation(contentType, { forProperty: true });
|
|
107
|
+
if (rawSDL) {
|
|
108
|
+
if (options.presetConfig.verbose)
|
|
109
|
+
console.log(` - Generated property fragment ${propertyFragmentName} for ${contentType.key} at ${vLoc}`);
|
|
110
|
+
addedFiles.push({
|
|
111
|
+
rawSDL,
|
|
112
|
+
document: (0, graphql_1.parse)(rawSDL),
|
|
113
|
+
location: vLoc,
|
|
114
|
+
hash: vLoc
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return [...files, ...addedFiles];
|
|
120
|
+
}
|
|
121
|
+
exports.default = injectComponentDocuments;
|
|
122
|
+
//# sourceMappingURL=injectComponentDocuments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"injectComponentDocuments.js","sourceRoot":"","sources":["../../src/_transform/injectComponentDocuments.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,4DAiFC;AAhGD,qCAA+B;AAC/B,gEAAiD;AACjD,gDAAiC;AAEjC,mCAA0D;AAE1D;;;;;;;;GAQG;AACI,KAAK,UAAU,wBAAwB,CAAC,KAA2B,EAAE,OAA0C;IACpH,MAAM,YAAY,GAAG,IAAA,uBAAe,EAAC,KAAK,CAAC,CAAA;IAC3C,MAAM,aAAa,GAAG,IAAA,uBAAe,EAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IACrD,MAAM,UAAU,GAAyB,EAAE,CAAA;IAE3C,IAAI,OAAO,CAAC,YAAY,CAAC,OAAO;QAC9B,OAAO,CAAC,GAAG,CAAC,uGAAuG,CAAC,CAAA;IAEtH,KAAK,MAAM,eAAe,IAAI,MAAM,CAAC,mBAAmB,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC;QACrF,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,KAAK,eAAe,CAAC,EAAE,CAAC;YAChE,MAAM,IAAI,GAAG,6BAA6B,eAAe,EAAE,CAAA;YAC3D,MAAM,MAAM,GAAG,YAAY,eAAe,mCAAmC,CAAA;YAC7E,IAAI,OAAO,CAA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AA;gBAC5G,UAAU,CAAC,IAAI,CAAC;oBACd,MAAM;oBACN,QAAQ,EAAE,IAAA,eAAK,EAAC,MAAM,CAAC;oBACvB,QAAQ,EAAE,IAAI;oBACd,IAAI,EAAE,IAAI;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"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import type { PresetOptions } from '../types';
|
|
3
|
+
export declare function injectPageQueries(files: Types.DocumentFile[], options: Types.PresetFnArgs<PresetOptions>): Promise<Types.DocumentFile[]>;
|
|
4
|
+
export default injectPageQueries;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.injectPageQueries = injectPageQueries;
|
|
37
|
+
const graphql_1 = require("graphql");
|
|
38
|
+
const QueryGen = __importStar(require("../contenttype-loader"));
|
|
39
|
+
const OptiCMS = __importStar(require("../cms"));
|
|
40
|
+
const tools_1 = require("./tools");
|
|
41
|
+
async function injectPageQueries(files, options) {
|
|
42
|
+
if (options.presetConfig.verbose)
|
|
43
|
+
console.log(`✨ [Optimizely] Generating page queries that have not been defined by the implementation`);
|
|
44
|
+
const existingQueries = (0, tools_1.getAllQueries)(files);
|
|
45
|
+
const existingTypes = (0, tools_1.getAllTypeNames)(options.schema);
|
|
46
|
+
const pageLevelTypes = OptiCMS.getAllContentTypes(undefined, 100, (ct) => {
|
|
47
|
+
if (ct.source == "graph")
|
|
48
|
+
return false;
|
|
49
|
+
return ["_page", "_experience"].includes((ct.baseType || "").toLowerCase());
|
|
50
|
+
});
|
|
51
|
+
const newFiles = [];
|
|
52
|
+
for await (const pageContentType of pageLevelTypes) {
|
|
53
|
+
const graphDataType = QueryGen.getGraphType(pageContentType);
|
|
54
|
+
const queryName = `get${QueryGen.Tools.ucFirst(graphDataType)}Data`;
|
|
55
|
+
// Check if the type exists in the Schema, if not skip it
|
|
56
|
+
if (!existingTypes.includes(graphDataType)) {
|
|
57
|
+
if (options.presetConfig.verbose)
|
|
58
|
+
console.log(` - Skipping query generation for ${pageContentType.key}, the schema does not contain the type ${graphDataType}`);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
// Check if the query has already been defined, if so skip it
|
|
62
|
+
if (existingQueries.has(queryName)) {
|
|
63
|
+
if (options.presetConfig.verbose)
|
|
64
|
+
console.log(` - Skipping query generation for ${pageContentType.key}, the project already includes a definition for ${queryName}`);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const rawSDL = QueryGen.buildGetQuery(pageContentType, queryName);
|
|
68
|
+
const vLoc = QueryGen.buildVirtualLocation(pageContentType, { type: 'query' });
|
|
69
|
+
if (options.presetConfig.verbose)
|
|
70
|
+
console.log(` - Generated query for ${pageContentType.key} at ${vLoc}`);
|
|
71
|
+
newFiles.push({
|
|
72
|
+
rawSDL,
|
|
73
|
+
document: (0, graphql_1.parse)(rawSDL),
|
|
74
|
+
location: vLoc,
|
|
75
|
+
hash: vLoc
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return [...files, ...newFiles];
|
|
79
|
+
}
|
|
80
|
+
exports.default = injectPageQueries;
|
|
81
|
+
//# sourceMappingURL=injectPageQueries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"injectPageQueries.js","sourceRoot":"","sources":["../../src/_transform/injectPageQueries.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,8CA8CC;AApDD,qCAA+B;AAC/B,gEAAiD;AACjD,gDAAiC;AAEjC,mCAAwD;AAEjD,KAAK,UAAU,iBAAiB,CAAC,KAA2B,EAAE,OAA0C;IAC7G,IAAI,OAAO,CAAC,YAAY,CAAC,OAAO;QAC9B,OAAO,CAAC,GAAG,CAAC,yFAAyF,CAAC,CAAA;IAExG,MAAM,eAAe,GAAG,IAAA,qBAAa,EAAC,KAAK,CAAC,CAAA;IAC5C,MAAM,aAAa,GAAG,IAAA,uBAA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AAE,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAA;QAC9E,IAAI,OAAO,CAAC,YAAY,CAAC,OAAO;YAC9B,OAAO,CAAC,GAAG,CAAC,6BAA6B,eAAe,CAAC,GAAG,OAAO,IAAI,EAAE,CAAC,CAAA;QAE5E,QAAQ,CAAC,IAAI,CAAC;YACZ,MAAM;YACN,QAAQ,EAAE,IAAA,eAAK,EAAC,MAAM,CAAC;YACvB,QAAQ,EAAE,IAAI;YACd,IAAI,EAAE,IAAI;SACX,CAAC,CAAA;IACJ,CAAC;IAED,OAAO,CAAC,GAAG,KAAK,EAAE,GAAG,QAAQ,CAAC,CAAA;AAChC,CAAC;AAED,kBAAe,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import type { PresetOptions } from '../types';
|
|
3
|
+
export declare function injectSectionQueries(files: Types.DocumentFile[], options: Types.PresetFnArgs<PresetOptions>): Promise<Types.DocumentFile[]>;
|
|
4
|
+
export default injectSectionQueries;
|