@@ -18,6 +18,7 @@ import {
1818 isInterfaceType ,
1919 isUnionType ,
2020 GraphQLInterfaceType ,
21+ GraphQLUnionType ,
2122} from 'graphql' ;
2223import { mapGraphQLTypeToProto , ProtoTypeInfo } from './type-mapper.js' ;
2324import { FieldNumberManager } from './field-numbering.js' ;
@@ -58,7 +59,7 @@ export interface MessageBuilderOptions {
5859export function buildMessageFromSelectionSet (
5960 messageName : string ,
6061 selectionSet : SelectionSetNode ,
61- parentType : GraphQLObjectType ,
62+ parentType : GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType ,
6263 typeInfo : TypeInfo ,
6364 options ?: MessageBuilderOptions ,
6465) : protobuf . Type {
@@ -71,7 +72,7 @@ export function buildMessageFromSelectionSet(
7172
7273 const collectFields = (
7374 selections : readonly SelectionNode [ ] ,
74- currentType : GraphQLObjectType | GraphQLInterfaceType ,
75+ currentType : GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType ,
7576 ) => {
7677 for ( const selection of selections ) {
7778 if ( selection . kind === 'Field' ) {
@@ -151,7 +152,7 @@ export function buildMessageFromSelectionSet(
151152function processFieldSelection (
152153 field : FieldNode ,
153154 message : protobuf . Type ,
154- parentType : GraphQLObjectType | GraphQLInterfaceType ,
155+ parentType : GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType ,
155156 typeInfo : TypeInfo ,
156157 options ?: MessageBuilderOptions ,
157158 fieldNumberManager ?: FieldNumberManager ,
@@ -171,6 +172,13 @@ function processFieldSelection(
171172 }
172173
173174 // Get the field definition from the parent type
175+ // Union types don't have fields directly, so skip field validation for them
176+ if ( isUnionType ( parentType ) ) {
177+ // Union types should only be processed through inline fragments
178+ // This shouldn't happen in normal GraphQL, but we'll handle it gracefully
179+ return ;
180+ }
181+
174182 const fieldDef = parentType . getFields ( ) [ fieldName ] ;
175183 if ( ! fieldDef ) {
176184 throw new Error (
@@ -188,9 +196,9 @@ function processFieldSelection(
188196 // Use simple name since message will be nested inside parent
189197 const nestedMessageName = upperFirst ( camelCase ( fieldName ) ) ;
190198
191- // For interfaces and unions, we use the base type to collect fields from inline fragments
192- // For object types, we process normally
193- const typeForSelection = isObjectType ( namedType ) ? namedType : ( namedType as any ) ;
199+ // For interfaces and unions, we use the type directly for processing
200+ // Union types will only work with inline fragments that specify concrete types
201+ const typeForSelection = namedType ;
194202
195203 const nestedMessage = buildMessageFromSelectionSet (
196204 nestedMessageName ,
@@ -300,13 +308,13 @@ function processFieldSelection(
300308function processInlineFragment (
301309 fragment : InlineFragmentNode ,
302310 message : protobuf . Type ,
303- parentType : GraphQLObjectType ,
311+ parentType : GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType ,
304312 typeInfo : TypeInfo ,
305313 options ?: MessageBuilderOptions ,
306314 fieldNumberManager ?: FieldNumberManager ,
307315) : void {
308316 // Determine the type for this inline fragment
309- let fragmentType : GraphQLObjectType ;
317+ let fragmentType : GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType ;
310318
311319 if ( fragment . typeCondition ) {
312320 // Type condition specified: ... on User
@@ -319,8 +327,8 @@ function processInlineFragment(
319327 }
320328
321329 const type = schema . getType ( typeName ) ;
322- if ( ! type || ! isObjectType ( type ) ) {
323- // Type not found or not an object type - skip
330+ if ( ! type || ! ( isObjectType ( type ) || isInterfaceType ( type ) || isUnionType ( type ) ) ) {
331+ // Type not found or not a supported type - skip
324332 return ;
325333 }
326334
@@ -352,7 +360,7 @@ function processInlineFragment(
352360function processFragmentSpread (
353361 spread : FragmentSpreadNode ,
354362 message : protobuf . Type ,
355- parentType : GraphQLObjectType ,
363+ parentType : GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType ,
356364 typeInfo : TypeInfo ,
357365 options ?: MessageBuilderOptions ,
358366 fieldNumberManager ?: FieldNumberManager ,
0 commit comments