Unable to use @requires directive on interface implementations

We are trying to use the @requires directive to request the fields we need from a different graph to compute an additional field to add to that type. This works as expected for all of our use cases, except for where we are trying to use @requires on an interface type (where we need fields from the subtypes).

For example, if we have:

    extend type Product @key(fields: "uuid") {
        uuid: Uuid! @external
        discounts: [Discount]! @external
        description: String! @requires(fields: "discounts { details }") 
    }
    
    extend interface Discount {
        details: String @external
    }

    extend type DollarOff implements Discount {
        details: String @external
        amount: Int @external
    }

   { other implementations of Discount }

is it possible to require the amount field from DollarOff?

Hello! From the schema you posted, looks like Discount is an interface with only one field details. Since Discount is an interface, all the types that implement Discount HAVE to provide an implementation for details. So, you are able to do description: String! @requires(fields: "discounts { details }") without any issue.

However, amount is a field on one implementation of Discount. It is not guaranteed that all the implementations of Discount has the amount field. So, the query planner cannot build a plan and resolve that field.

If all the implementations of Discount have the amount field defined, you can move that field to Discount interface and you should have no problem requiring that field on the description field on the Product type. If not, you can change the fields in all the implementations of Discount to have a common (something like discountAmount) and move the discountAmount field up to the interface.

Hope this helps!

1 Like

On a related note, increasing the flexibility of using interfaces across subgraphs is top-of-mind for Apollo, and we’ll discuss it as part of our roadmap in the federation webinar on 24 June.

1 Like