Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Resolve an External Entity with Apollo Federation

    Alex BanksAlex Banks

    Federated services can also resolve external entities using references. When a service resolves an external entity, it only needs to return a reference to that entity. These references are resolved by the service that defined the entity with a special resolver called __resolveReference.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: 0:00 I'm here looking at the code base in the lift service and let's say we wanted to add a trail access field to the lift type. This would allow our users to list all of the trails that are accessed by a specific lift.

    0:11 We'll need to add a lift resolver key here and a trail access resolver to that. The parent object is going to be the lift.

    0:21 Each lift record has an array of related trail references, so I'm going to go ahead and map over those. In each reference is a trail ID. I'm going to go ahead and add a type name so that we all know that we're resolving trail types.

    0:37 I want to return the ID or the reference for each trail. We need to restart the gateway. This means I can create a query for all lifts and find the connected trails that live off of each lift.

    0:53 In the results here, I can see the ID, the reference to each trail. What if I asked for more info about the trail? The lift service cannot provide that info, so we get an error. What I have to do is allow the trail service to resolve trail types through a reference.

    1:14 I can do this within the trail service. All I need to do is add an additional resolver to the trail type. This resolver is called resolve reference. It can be used to look up trail objects based on their reference, the trail ID.

    1:29 This resolver will receive the reference and I can use it to find the trail. If any external service returns the trail, we can resolve the rest of that trail data here and allow our users to ask for any fields on the trail that they want.

    1:48 In this case, the lift service is going to return the reference ID field and the trail service is going to do the rest. We can see here that our query is working, so we are getting the related trails.

    2:04 Let's take a look at the query plan. The query plan shows us how the gateway orchestrates this request. It's going to send downstream requests in sequence.

    2:14 First, it sends the all lifts query. We can see here that we're only resolving the ID of the trails accessed by each lift. The next request is sent to the trail service. We find the trails by their reference, the ID, and resolve any additional information about each trail.

    2:34 Finally, the results are flattened and combined into a single response.