You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a union type alias is defined outside the schema directory and used as a field type in a schema-exported type, the discriminatorFields config cannot match because:
The type is not in knownTypeNames (not exported from schema directory)
// src/gqlkit/schema/types.tsimporttype{ContentPart}from"../../lib/types";exporttypeMessage={parts: ContentPart[];};// Note: ContentPart is NOT re-exported from the schema directory
// gqlkit.config.tsexportdefaultdefineConfig({sourceDir: "src/gqlkit/schema",discriminatorFields: {ContentPart: "type",// user uses the original alias name},});
Expected
discriminatorFields config for ContentPart is applied. The union members are grouped by the type discriminator field, and code generation succeeds.
Actual
The following chain of events occurs:
ContentPart is not in knownTypeNames (not exported from schema dir)
flattenInlineUnionMembers looks up discriminatorFields.get("MessageParts") → not found (config key is ContentPart)
The inline objects have no inlineObjectHintName and no __typename:
error[UNNAMEABLE_UNION_MEMBER]: Inline object union member at index 0 cannot be named.
Use a named type (type alias or interface) for each union member, or add a '__typename'
property with a string literal type.
Workaround
The user can use the auto-generated name as the config key:
This is fragile and requires the user to know the internal naming convention.
Possible Solutions
A few approaches to consider (non-exhaustive):
Match by unionAliasName: flattenInlineUnionMembers could also check inlineUnion.unionAliasName against the discriminatorFields keys, not just the auto-generated name.
Summary
When a union type alias is defined outside the schema directory and used as a field type in a schema-exported type, the
discriminatorFieldsconfig cannot match because:knownTypeNames(not exported from schema directory)isKnownSchemaTyperequiresknownTypeNames{ParentType}{FieldPascalCase}convention, which differs from the original type alias nameflattenInlineUnionMembersfails to match →UNNAMEABLE_UNION_MEMBERerrorReproduction
Directory structure
Source files
Expected
discriminatorFieldsconfig forContentPartis applied. The union members are grouped by thetypediscriminator field, and code generation succeeds.Actual
The following chain of events occurs:
ContentPartis not inknownTypeNames(not exported from schema dir)field-type-resolver.tscannot resolveContentPart[]element type as a reference (the fix from fix(cli): resolve nullable union type alias as reference instead of inline union #227 requiresisKnownSchemaType, which checksknownTypeNames)collectInlineUnionsFromTypescollects it with context{ kind: "objectField", parentTypeName: "Message", fieldPath: ["parts"] }generateAutoTypeNameproducesMessageParts(notContentPart)flattenInlineUnionMemberslooks updiscriminatorFields.get("MessageParts")→ not found (config key isContentPart)inlineObjectHintNameand no__typename:Workaround
The user can use the auto-generated name as the config key:
This is fragile and requires the user to know the internal naming convention.
Possible Solutions
A few approaches to consider (non-exhaustive):
unionAliasName:flattenInlineUnionMemberscould also checkinlineUnion.unionAliasNameagainst thediscriminatorFieldskeys, not just the auto-generated name.knownTypeNames: Automatically include types transitively referenced by schema-exported types, so the nullable union alias fix from fix(cli): resolve nullable union type alias as reference instead of inline union #227 applies and the type resolves as a reference rather than being expanded inline.discriminatorFieldskeys must match either the schema-exported type name or the auto-generated name.