GraphQL Federation: How To Consume, Not Extend

119 views Asked by At

I have a service that returns Products that I can extend via GraphQL Federation in a service returning Reviews. The review service depends on the Products and this decoupling is just awesome!

I also have an order service that stores (among other things) OrderItems that have a productId:

type OrderItem @key(fields : ["id"]) {
    id: String
    amount: Int
    position: Int
    productId: String
}

Now I want the clients to be able to also directly select any fields from the Product being referenced by the productId:

orderItem(id: "123") {
    position
    amount
    product {
        name
        price
    }
}

If I wrote a normal resolver to add the product field, I'd have to duplicate the complete Product model (and update the order service whenever Product changes). I don't want that. I want to utilize Federation here, too.

I want the order service to extend (and thereby depend on) the Product, but only know about its id, while the product service mustn’t know anything about the order service. So this is an inversion of the dependencies as seen in the review service. I don’t want to add to the model from another service, I want the client to be able to consume it, without my service knowing about the details.

I assumed that it should be even easier this way around, but I’m just too blind to see what I need to do! I couldn’t find examples or tutorials or anything that do it this way around.

I would really appreciate your help!

1

There are 1 answers

0
rü- On BEST ANSWER

Stupid me! It actually is as simple as I had assumed, I've just been chasing an unrelated (albeit cryptic to me) error message.

I just have to add a resolver that returns an instance of my Product stub (i.e. containing only the id). The rest works like a charm!