QuestionQ50

VerifiedCustomize Commerce features

An Adobe Commerce Architect is developing a new GraphQL API mutation to modify how configurable products are added to the cart. The mutation accepts a configurable product ID. If the specified product has only one variant, the mutation must add that variant to the cart and return a non-nullable Cart type. If the configurable product has more variants, the mutation must return a non-nullable ConfigurableProduct type.

The mutation declaration is as follows:

Question Image

How should the Adobe Commerce Architect declare this mutation’s output?

Explanation

A GraphQL union represents a value that can be one of several object types without requiring those types to share fields. AddToCartOutput must therefore be a union of ConfigurableProduct and Cart, with a @typeResolver that determines which concrete type is returned. The existing non-null AddToCartOutput! mutation return type ensures that one of those results is returned.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!