Netflix/dgs-federation-example

Documentation: a common federation use case for Child Field type

dwvd opened this issue · 0 comments

dwvd commented

I have gone through the documentation of Show and Reviews and understand that it could be federated with following code.


@DgsEntityFetcher(name = "Show")
    public Show movie(Map<String, Object> values) {
        return new Show((String) values.get("id"), null);
    }

    @DgsData(parentType = "Show", field = "reviews")
    public List<Review> reviewsFetcher(DgsDataFetchingEnvironment dataFetchingEnvironment)  {
        Show show = dataFetchingEnvironment.getSource();
        return reviews.get(show.getId());
    }

But my problem is that not enough documentation is provided to understand how leverage the federation if entity is child element like below schema:-

type User
{
    id:String
    name :String
    product:Product
}
type Product @key(fields: "id") @extends{
id : String @external
}

As you see type product is part of User and I am trying to make it work with following code, but it is not working. I tried to implement this in multiple ways but it is not working. If you have any sample code or documentation that would be helpful.

@DgsEntityFetcher(name = "product")
    public Product getProduct(Map<String, Object> values) {
        return new Product((String) values.get("id"));
    }

    @DgsData(parentType = "Query", field = "getUser")
    public DataFetcherResult<Object> getProduct(@InputArgument UserRequest userRequest, DataFetchingEnvironment dataFetchingEnvironment) {
        log.info(userRequest +":-"+ dataFetchingEnvironment.toString());
        UserResponse userResponse = userService.search(userRequest);
        Product product = dataFetchingEnvironment.getSource();
        log.info("product: " + product);
//        product.getId(userResponse.get);

        return DataFetcherResult.newResult().data(userResponse).build();
    }