@@ -67,7 +67,7 @@ export function validateFixtureInput(
6767 errors . push ( `Cannot validate ${ responseKey } : missing parent type information` ) ;
6868 } else {
6969 const typenameResponseKey = typenameResponseKeyStack [ typenameResponseKeyStack . length - 1 ] ;
70- if ( isValueExpectedForType ( currentValue , parentType , typenameResponseKey ) ) {
70+ if ( isValueExpectedForType ( currentValue , parentType , schema , typenameResponseKey ) ) {
7171 errors . push ( `Missing expected fixture data for ${ responseKey } ` ) ;
7272 }
7373 }
@@ -253,28 +253,23 @@ function processNestedArrays(
253253 * Determines if a fixture value is expected for a given parent type based on its __typename.
254254 *
255255 * @param fixtureValue - The fixture value to check
256- * @param parentType - The parent type from typeInfo (concrete type if inside inline fragment, abstract if on union/interface)
256+ * @param parentType - The parent type from typeInfo
257+ * @param schema - The GraphQL schema to resolve possible types for abstract types
257258 * @param typenameKey - The response key for the __typename field (supports aliases like `type: __typename`)
258259 * @returns True if the value is expected for the parent type, false otherwise
259260 *
260261 * @remarks
261- * When the parent type is abstract (union/interface), all values are expected.
262- * When the parent type is concrete (inside an inline fragment), only values
262+ * When the parent type is abstract (union/interface), checks if the value's __typename
263+ * is one of the possible types for that abstract type.
264+ * When the parent type is concrete (e.g., inside `... on ConcreteType`), only values
263265 * whose __typename matches the concrete type are expected.
264266 */
265267function isValueExpectedForType (
266268 fixtureValue : any ,
267269 parentType : GraphQLCompositeType ,
270+ schema : GraphQLSchema ,
268271 typenameKey ?: string
269272) : boolean {
270- // If parent type is abstract (union/interface), all values are expected.
271- // This means we're validating a field selected directly on the abstract type (e.g., __typename on a union),
272- // so it should be present on all values regardless of their concrete type.
273- if ( isAbstractType ( parentType ) ) {
274- return true ;
275- }
276-
277- // Parent is a concrete type - check if fixture value's __typename matches
278273 // If __typename wasn't selected in the query, we can't discriminate, so expect all values
279274 if ( ! typenameKey ) {
280275 return true ;
@@ -286,6 +281,12 @@ function isValueExpectedForType(
286281 return true ;
287282 }
288283
289- // Only expect the value for this type if its __typename matches the parent type
284+ // If parent type is abstract (union/interface), check if the value's type is one of the possible types
285+ if ( isAbstractType ( parentType ) ) {
286+ const possibleTypes = schema . getPossibleTypes ( parentType ) ;
287+ return possibleTypes . some ( type => type . name === valueTypename ) ;
288+ }
289+
290+ // Parent is a concrete type - check if fixture value's __typename matches
290291 return valueTypename === parentType . name ;
291292}
0 commit comments