@graphql-codegen/visitor-plugin-common 3.0.2 → 3.1.0-alpha-20230310110943-3acf07a7b
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.
|
@@ -41,6 +41,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
41
41
|
mappers: (0, mappers_js_1.transformMappers)(rawConfig.mappers || {}, rawConfig.mapperTypeSuffix),
|
|
42
42
|
scalars: (0, utils_js_1.buildScalarsFromConfig)(_schema, rawConfig, defaultScalars),
|
|
43
43
|
internalResolversPrefix: (0, utils_js_1.getConfigValue)(rawConfig.internalResolversPrefix, '__'),
|
|
44
|
+
resolversNonOptionalTypename: normalizeResolversNonOptionalTypename((0, utils_js_1.getConfigValue)(rawConfig.resolversNonOptionalTypename, false)),
|
|
44
45
|
...additionalConfig,
|
|
45
46
|
});
|
|
46
47
|
this._schema = _schema;
|
|
@@ -281,7 +282,10 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
|
|
|
281
282
|
const finalTypename = isScalar ? this._getScalar(typeName) : unionMemberValue;
|
|
282
283
|
return replacePlaceholder(this.config.defaultMapper.type, finalTypename);
|
|
283
284
|
}
|
|
284
|
-
|
|
285
|
+
const nonOptionalTypenameModifier = this.config.resolversNonOptionalTypename.union
|
|
286
|
+
? ` & { __typename: "${unionMemberType}" }`
|
|
287
|
+
: '';
|
|
288
|
+
return `${unionMemberValue}${nonOptionalTypenameModifier}`;
|
|
285
289
|
});
|
|
286
290
|
res[typeName] = referencedTypes.map(type => `( ${type} )`).join(' | '); // Must wrap every union member in explicit "( )" to separate the members
|
|
287
291
|
}
|
|
@@ -785,3 +789,17 @@ function replacePlaceholder(pattern, typename) {
|
|
|
785
789
|
function hasPlaceholder(pattern) {
|
|
786
790
|
return pattern.includes('{T}');
|
|
787
791
|
}
|
|
792
|
+
function normalizeResolversNonOptionalTypename(input) {
|
|
793
|
+
const defaultConfig = {
|
|
794
|
+
union: false,
|
|
795
|
+
};
|
|
796
|
+
if (typeof input === 'boolean') {
|
|
797
|
+
return {
|
|
798
|
+
union: input,
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
return {
|
|
802
|
+
...defaultConfig,
|
|
803
|
+
...input,
|
|
804
|
+
};
|
|
805
|
+
}
|
|
@@ -37,6 +37,7 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
37
37
|
mappers: transformMappers(rawConfig.mappers || {}, rawConfig.mapperTypeSuffix),
|
|
38
38
|
scalars: buildScalarsFromConfig(_schema, rawConfig, defaultScalars),
|
|
39
39
|
internalResolversPrefix: getConfigValue(rawConfig.internalResolversPrefix, '__'),
|
|
40
|
+
resolversNonOptionalTypename: normalizeResolversNonOptionalTypename(getConfigValue(rawConfig.resolversNonOptionalTypename, false)),
|
|
40
41
|
...additionalConfig,
|
|
41
42
|
});
|
|
42
43
|
this._schema = _schema;
|
|
@@ -277,7 +278,10 @@ export class BaseResolversVisitor extends BaseVisitor {
|
|
|
277
278
|
const finalTypename = isScalar ? this._getScalar(typeName) : unionMemberValue;
|
|
278
279
|
return replacePlaceholder(this.config.defaultMapper.type, finalTypename);
|
|
279
280
|
}
|
|
280
|
-
|
|
281
|
+
const nonOptionalTypenameModifier = this.config.resolversNonOptionalTypename.union
|
|
282
|
+
? ` & { __typename: "${unionMemberType}" }`
|
|
283
|
+
: '';
|
|
284
|
+
return `${unionMemberValue}${nonOptionalTypenameModifier}`;
|
|
281
285
|
});
|
|
282
286
|
res[typeName] = referencedTypes.map(type => `( ${type} )`).join(' | '); // Must wrap every union member in explicit "( )" to separate the members
|
|
283
287
|
}
|
|
@@ -780,3 +784,17 @@ function replacePlaceholder(pattern, typename) {
|
|
|
780
784
|
function hasPlaceholder(pattern) {
|
|
781
785
|
return pattern.includes('{T}');
|
|
782
786
|
}
|
|
787
|
+
function normalizeResolversNonOptionalTypename(input) {
|
|
788
|
+
const defaultConfig = {
|
|
789
|
+
union: false,
|
|
790
|
+
};
|
|
791
|
+
if (typeof input === 'boolean') {
|
|
792
|
+
return {
|
|
793
|
+
union: input,
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
return {
|
|
797
|
+
...defaultConfig,
|
|
798
|
+
...input,
|
|
799
|
+
};
|
|
800
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/visitor-plugin-common",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0-alpha-20230310110943-3acf07a7b",
|
|
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": "
|
|
9
|
+
"@graphql-codegen/plugin-helpers": "4.1.1-alpha-20230310110943-3acf07a7b",
|
|
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",
|
|
@@ -2,7 +2,7 @@ import { ApolloFederation } from '@graphql-codegen/plugin-helpers';
|
|
|
2
2
|
import { ASTNode, DirectiveDefinitionNode, EnumTypeDefinitionNode, FieldDefinitionNode, GraphQLNamedType, GraphQLSchema, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql';
|
|
3
3
|
import { BaseVisitor, BaseVisitorConvertOptions, ParsedConfig, RawConfig } from './base-visitor.cjs';
|
|
4
4
|
import { ParsedMapper } from './mappers.cjs';
|
|
5
|
-
import { AvoidOptionalsConfig, ConvertOptions, DeclarationKind, EnumValuesMap, NormalizedScalarsMap, ParsedEnumValuesMap } from './types.cjs';
|
|
5
|
+
import { AvoidOptionalsConfig, ConvertOptions, DeclarationKind, EnumValuesMap, NormalizedScalarsMap, ParsedEnumValuesMap, ResolversNonOptionalTypenameConfig } from './types.cjs';
|
|
6
6
|
import { DeclarationBlockConfig } from './utils.cjs';
|
|
7
7
|
import { OperationVariablesToObject } from './variables-to-object.cjs';
|
|
8
8
|
export interface ParsedResolversConfig extends ParsedConfig {
|
|
@@ -28,6 +28,7 @@ export interface ParsedResolversConfig extends ParsedConfig {
|
|
|
28
28
|
internalResolversPrefix: string;
|
|
29
29
|
onlyResolveTypeForInterfaces: boolean;
|
|
30
30
|
directiveResolverMappings: Record<string, string>;
|
|
31
|
+
resolversNonOptionalTypename: ResolversNonOptionalTypenameConfig;
|
|
31
32
|
}
|
|
32
33
|
export interface RawResolversConfig extends RawConfig {
|
|
33
34
|
/**
|
|
@@ -493,6 +494,11 @@ export interface RawResolversConfig extends RawConfig {
|
|
|
493
494
|
* @description Turning this flag to `true` will generate resolver signature that has only `resolveType` for interfaces, forcing developers to write inherited type resolvers in the type itself.
|
|
494
495
|
*/
|
|
495
496
|
onlyResolveTypeForInterfaces?: boolean;
|
|
497
|
+
/**
|
|
498
|
+
* @description Make __typename of resolver mappings non-optional
|
|
499
|
+
* @default false
|
|
500
|
+
*/
|
|
501
|
+
resolversNonOptionalTypename?: boolean | ResolversNonOptionalTypenameConfig;
|
|
496
502
|
/**
|
|
497
503
|
* @ignore
|
|
498
504
|
*/
|
|
@@ -2,7 +2,7 @@ import { ApolloFederation } from '@graphql-codegen/plugin-helpers';
|
|
|
2
2
|
import { ASTNode, DirectiveDefinitionNode, EnumTypeDefinitionNode, FieldDefinitionNode, GraphQLNamedType, GraphQLSchema, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql';
|
|
3
3
|
import { BaseVisitor, BaseVisitorConvertOptions, ParsedConfig, RawConfig } from './base-visitor.js';
|
|
4
4
|
import { ParsedMapper } from './mappers.js';
|
|
5
|
-
import { AvoidOptionalsConfig, ConvertOptions, DeclarationKind, EnumValuesMap, NormalizedScalarsMap, ParsedEnumValuesMap } from './types.js';
|
|
5
|
+
import { AvoidOptionalsConfig, ConvertOptions, DeclarationKind, EnumValuesMap, NormalizedScalarsMap, ParsedEnumValuesMap, ResolversNonOptionalTypenameConfig } from './types.js';
|
|
6
6
|
import { DeclarationBlockConfig } from './utils.js';
|
|
7
7
|
import { OperationVariablesToObject } from './variables-to-object.js';
|
|
8
8
|
export interface ParsedResolversConfig extends ParsedConfig {
|
|
@@ -28,6 +28,7 @@ export interface ParsedResolversConfig extends ParsedConfig {
|
|
|
28
28
|
internalResolversPrefix: string;
|
|
29
29
|
onlyResolveTypeForInterfaces: boolean;
|
|
30
30
|
directiveResolverMappings: Record<string, string>;
|
|
31
|
+
resolversNonOptionalTypename: ResolversNonOptionalTypenameConfig;
|
|
31
32
|
}
|
|
32
33
|
export interface RawResolversConfig extends RawConfig {
|
|
33
34
|
/**
|
|
@@ -493,6 +494,11 @@ export interface RawResolversConfig extends RawConfig {
|
|
|
493
494
|
* @description Turning this flag to `true` will generate resolver signature that has only `resolveType` for interfaces, forcing developers to write inherited type resolvers in the type itself.
|
|
494
495
|
*/
|
|
495
496
|
onlyResolveTypeForInterfaces?: boolean;
|
|
497
|
+
/**
|
|
498
|
+
* @description Make __typename of resolver mappings non-optional
|
|
499
|
+
* @default false
|
|
500
|
+
*/
|
|
501
|
+
resolversNonOptionalTypename?: boolean | ResolversNonOptionalTypenameConfig;
|
|
496
502
|
/**
|
|
497
503
|
* @ignore
|
|
498
504
|
*/
|
package/typings/types.d.cts
CHANGED