@graphql-codegen/visitor-plugin-common 4.0.0-alpha-20230508130212-af82ca2ac → 4.0.0-alpha-20230516100505-a720d57f8
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/cjs/base-resolvers-visitor.js +4 -2
- package/cjs/selection-set-to-object.js +13 -3
- package/esm/base-resolvers-visitor.js +4 -2
- package/esm/selection-set-to-object.js +13 -3
- package/package.json +2 -2
- package/typings/base-resolvers-visitor.d.cts +43 -0
- package/typings/base-resolvers-visitor.d.ts +43 -0
- package/typings/types.d.cts +1 -0
- package/typings/types.d.ts +1 -0
|
@@ -259,10 +259,11 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
259
259
|
const unionTypes = typeNames.reduce((res, typeName) => {
|
|
260
260
|
const schemaType = allSchemaTypes[typeName];
|
|
261
261
|
if ((0, graphql_1.isUnionType)(schemaType)) {
|
|
262
|
+
const { unionMember, excludeTypes } = this.config.resolversNonOptionalTypename;
|
|
262
263
|
res[typeName] = this.getAbstractMembersType({
|
|
263
264
|
typeName,
|
|
264
265
|
memberTypes: schemaType.getTypes(),
|
|
265
|
-
isTypenameNonOptional:
|
|
266
|
+
isTypenameNonOptional: unionMember && !(excludeTypes === null || excludeTypes === void 0 ? void 0 : excludeTypes.includes(typeName)),
|
|
266
267
|
});
|
|
267
268
|
}
|
|
268
269
|
return res;
|
|
@@ -288,10 +289,11 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
288
289
|
}
|
|
289
290
|
}
|
|
290
291
|
}
|
|
292
|
+
const { interfaceImplementingType, excludeTypes } = this.config.resolversNonOptionalTypename;
|
|
291
293
|
res[typeName] = this.getAbstractMembersType({
|
|
292
294
|
typeName,
|
|
293
295
|
memberTypes: implementingTypes,
|
|
294
|
-
isTypenameNonOptional:
|
|
296
|
+
isTypenameNonOptional: interfaceImplementingType && !(excludeTypes === null || excludeTypes === void 0 ? void 0 : excludeTypes.includes(typeName)),
|
|
295
297
|
});
|
|
296
298
|
}
|
|
297
299
|
return res;
|
|
@@ -320,10 +320,13 @@ class SelectionSetToObject {
|
|
|
320
320
|
// Also use fragment hashing if skipTypename is true, since we
|
|
321
321
|
// then don't have a typename for naming the fragment.
|
|
322
322
|
acc[selectedTypes.length <= 3
|
|
323
|
-
?
|
|
323
|
+
? // Remove quote marks to produce a valid type name
|
|
324
|
+
selectedTypes.map(t => t.replace(/'/g, '')).join('_')
|
|
324
325
|
: (0, crypto_1.createHash)('sha256')
|
|
325
326
|
.update(selectedTypes.join() || transformedSet || '')
|
|
326
|
-
|
|
327
|
+
// Remove invalid characters to produce a valid type name
|
|
328
|
+
.digest('base64')
|
|
329
|
+
.replace(/[=+/]/g, '')] = [transformedSet];
|
|
327
330
|
}
|
|
328
331
|
return acc;
|
|
329
332
|
}, {});
|
|
@@ -538,7 +541,14 @@ class SelectionSetToObject {
|
|
|
538
541
|
}
|
|
539
542
|
return null;
|
|
540
543
|
}
|
|
541
|
-
|
|
544
|
+
const content = possibleFields
|
|
545
|
+
.map(selectionObject => {
|
|
546
|
+
if (typeof selectionObject === 'string')
|
|
547
|
+
return selectionObject;
|
|
548
|
+
return '(' + selectionObject.union.join(' | ') + ')';
|
|
549
|
+
})
|
|
550
|
+
.join(' & ');
|
|
551
|
+
return { name: declarationName, content };
|
|
542
552
|
})
|
|
543
553
|
.filter(Boolean);
|
|
544
554
|
const fragmentTypeName = this.buildFragmentTypeName(fragmentName, fragmentSuffix);
|
|
@@ -255,10 +255,11 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
255
255
|
const unionTypes = typeNames.reduce((res, typeName) => {
|
|
256
256
|
const schemaType = allSchemaTypes[typeName];
|
|
257
257
|
if (isUnionType(schemaType)) {
|
|
258
|
+
const { unionMember, excludeTypes } = this.config.resolversNonOptionalTypename;
|
|
258
259
|
res[typeName] = this.getAbstractMembersType({
|
|
259
260
|
typeName,
|
|
260
261
|
memberTypes: schemaType.getTypes(),
|
|
261
|
-
isTypenameNonOptional:
|
|
262
|
+
isTypenameNonOptional: unionMember && !(excludeTypes === null || excludeTypes === void 0 ? void 0 : excludeTypes.includes(typeName)),
|
|
262
263
|
});
|
|
263
264
|
}
|
|
264
265
|
return res;
|
|
@@ -284,10 +285,11 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
284
285
|
}
|
|
285
286
|
}
|
|
286
287
|
}
|
|
288
|
+
const { interfaceImplementingType, excludeTypes } = this.config.resolversNonOptionalTypename;
|
|
287
289
|
res[typeName] = this.getAbstractMembersType({
|
|
288
290
|
typeName,
|
|
289
291
|
memberTypes: implementingTypes,
|
|
290
|
-
isTypenameNonOptional:
|
|
292
|
+
isTypenameNonOptional: interfaceImplementingType && !(excludeTypes === null || excludeTypes === void 0 ? void 0 : excludeTypes.includes(typeName)),
|
|
291
293
|
});
|
|
292
294
|
}
|
|
293
295
|
return res;
|
|
@@ -316,10 +316,13 @@ export class SelectionSetToObject {
|
|
|
316
316
|
// Also use fragment hashing if skipTypename is true, since we
|
|
317
317
|
// then don't have a typename for naming the fragment.
|
|
318
318
|
acc[selectedTypes.length <= 3
|
|
319
|
-
?
|
|
319
|
+
? // Remove quote marks to produce a valid type name
|
|
320
|
+
selectedTypes.map(t => t.replace(/'/g, '')).join('_')
|
|
320
321
|
: createHash('sha256')
|
|
321
322
|
.update(selectedTypes.join() || transformedSet || '')
|
|
322
|
-
|
|
323
|
+
// Remove invalid characters to produce a valid type name
|
|
324
|
+
.digest('base64')
|
|
325
|
+
.replace(/[=+/]/g, '')] = [transformedSet];
|
|
323
326
|
}
|
|
324
327
|
return acc;
|
|
325
328
|
}, {});
|
|
@@ -534,7 +537,14 @@ export class SelectionSetToObject {
|
|
|
534
537
|
}
|
|
535
538
|
return null;
|
|
536
539
|
}
|
|
537
|
-
|
|
540
|
+
const content = possibleFields
|
|
541
|
+
.map(selectionObject => {
|
|
542
|
+
if (typeof selectionObject === 'string')
|
|
543
|
+
return selectionObject;
|
|
544
|
+
return '(' + selectionObject.union.join(' | ') + ')';
|
|
545
|
+
})
|
|
546
|
+
.join(' & ');
|
|
547
|
+
return { name: declarationName, content };
|
|
538
548
|
})
|
|
539
549
|
.filter(Boolean);
|
|
540
550
|
const fragmentTypeName = this.buildFragmentTypeName(fragmentName, fragmentSuffix);
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/visitor-plugin-common",
|
|
3
|
-
"version": "4.0.0-alpha-
|
|
3
|
+
"version": "4.0.0-alpha-20230516100505-a720d57f8",
|
|
4
4
|
"peerDependencies": {
|
|
5
5
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@graphql-tools/optimize": "^1.3.0",
|
|
9
|
-
"@graphql-codegen/plugin-helpers": "4.2.1-alpha-
|
|
9
|
+
"@graphql-codegen/plugin-helpers": "4.2.1-alpha-20230516100505-a720d57f8",
|
|
10
10
|
"@graphql-tools/relay-operation-optimizer": "^6.5.0",
|
|
11
11
|
"@graphql-tools/utils": "^9.0.0",
|
|
12
12
|
"auto-bind": "~4.0.0",
|
|
@@ -497,6 +497,49 @@ export interface RawResolversConfig extends RawConfig {
|
|
|
497
497
|
/**
|
|
498
498
|
* @description Makes `__typename` of resolver mappings non-optional without affecting the base types.
|
|
499
499
|
* @default false
|
|
500
|
+
*
|
|
501
|
+
* @exampleMarkdown
|
|
502
|
+
* ## Enable for all
|
|
503
|
+
*
|
|
504
|
+
* ```ts filename="codegen.ts"
|
|
505
|
+
* import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
506
|
+
*
|
|
507
|
+
* const config: CodegenConfig = {
|
|
508
|
+
* // ...
|
|
509
|
+
* generates: {
|
|
510
|
+
* 'path/to/file': {
|
|
511
|
+
* plugins: ['typescript', 'typescript-resolver'],
|
|
512
|
+
* config: {
|
|
513
|
+
* resolversNonOptionalTypename: true // or { unionMember: true, interfaceImplementingType: true }
|
|
514
|
+
* },
|
|
515
|
+
* },
|
|
516
|
+
* },
|
|
517
|
+
* };
|
|
518
|
+
* export default config;
|
|
519
|
+
* ```
|
|
520
|
+
*
|
|
521
|
+
* ## Enable except for some types
|
|
522
|
+
*
|
|
523
|
+
* ```ts filename="codegen.ts"
|
|
524
|
+
* import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
525
|
+
*
|
|
526
|
+
* const config: CodegenConfig = {
|
|
527
|
+
* // ...
|
|
528
|
+
* generates: {
|
|
529
|
+
* 'path/to/file': {
|
|
530
|
+
* plugins: ['typescript', 'typescript-resolver'],
|
|
531
|
+
* config: {
|
|
532
|
+
* resolversNonOptionalTypename: {
|
|
533
|
+
* unionMember: true,
|
|
534
|
+
* interfaceImplementingType: true,
|
|
535
|
+
* excludeTypes: ['MyType'],
|
|
536
|
+
* }
|
|
537
|
+
* },
|
|
538
|
+
* },
|
|
539
|
+
* },
|
|
540
|
+
* };
|
|
541
|
+
* export default config;
|
|
542
|
+
* ```
|
|
500
543
|
*/
|
|
501
544
|
resolversNonOptionalTypename?: boolean | ResolversNonOptionalTypenameConfig;
|
|
502
545
|
/**
|
|
@@ -497,6 +497,49 @@ export interface RawResolversConfig extends RawConfig {
|
|
|
497
497
|
/**
|
|
498
498
|
* @description Makes `__typename` of resolver mappings non-optional without affecting the base types.
|
|
499
499
|
* @default false
|
|
500
|
+
*
|
|
501
|
+
* @exampleMarkdown
|
|
502
|
+
* ## Enable for all
|
|
503
|
+
*
|
|
504
|
+
* ```ts filename="codegen.ts"
|
|
505
|
+
* import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
506
|
+
*
|
|
507
|
+
* const config: CodegenConfig = {
|
|
508
|
+
* // ...
|
|
509
|
+
* generates: {
|
|
510
|
+
* 'path/to/file': {
|
|
511
|
+
* plugins: ['typescript', 'typescript-resolver'],
|
|
512
|
+
* config: {
|
|
513
|
+
* resolversNonOptionalTypename: true // or { unionMember: true, interfaceImplementingType: true }
|
|
514
|
+
* },
|
|
515
|
+
* },
|
|
516
|
+
* },
|
|
517
|
+
* };
|
|
518
|
+
* export default config;
|
|
519
|
+
* ```
|
|
520
|
+
*
|
|
521
|
+
* ## Enable except for some types
|
|
522
|
+
*
|
|
523
|
+
* ```ts filename="codegen.ts"
|
|
524
|
+
* import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
525
|
+
*
|
|
526
|
+
* const config: CodegenConfig = {
|
|
527
|
+
* // ...
|
|
528
|
+
* generates: {
|
|
529
|
+
* 'path/to/file': {
|
|
530
|
+
* plugins: ['typescript', 'typescript-resolver'],
|
|
531
|
+
* config: {
|
|
532
|
+
* resolversNonOptionalTypename: {
|
|
533
|
+
* unionMember: true,
|
|
534
|
+
* interfaceImplementingType: true,
|
|
535
|
+
* excludeTypes: ['MyType'],
|
|
536
|
+
* }
|
|
537
|
+
* },
|
|
538
|
+
* },
|
|
539
|
+
* },
|
|
540
|
+
* };
|
|
541
|
+
* export default config;
|
|
542
|
+
* ```
|
|
500
543
|
*/
|
|
501
544
|
resolversNonOptionalTypename?: boolean | ResolversNonOptionalTypenameConfig;
|
|
502
545
|
/**
|
package/typings/types.d.cts
CHANGED