question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to access additional data across federated services

See original GitHub issue

Federation is great! I love it! This demo is also awesome, thanks! I do have one question about obtaining data from external services.

Let’s say I work on the Accounts team. We’re using Apollo Federation and have teams working on independent federated services. All of our teams are totally independent, we cannot request features from other teams. If we want to implement something, we have to do it on our own.

Our customers want a running total of the products that they have purchased. We have saved a list of upc codes locally for each purchase that a user has made. A user account record has the following data in our database:

{
  "name": "Jonathan Toews",
  "productsPurchased": [1, 2, 3, 2, 1]
}

So we are going to add a field that will resolve to the total dollars that a users has spent on all of the products:

type User @key(fields: "id") {
  id: ID!
  name: String
  username: String
  totalDollarsSpent: Float
}

However, when I get to the resolver I do not have enough information about the products that the user has purchased. I only have their UPC codes:

totalDollarsSpent: (user) =>
  user.productsPurchased
    .map(upc => /*Here I also need the price, but I don't have it*/)
    .reduce((total,product) => total + product.price, 0);
  • What would be the best solution for completing this resolver?
  • Is this a use case for federation?
  • Can this be handed through shared context?
  • -or- Would you just send a GraphQL request to the Product service for prices from the resolver?

I’m not sure this is even a case for federation, but I was thinking about all of the use cases for teams who work in complete isolation. Federation gracefully handles every other use case that came up, so I though this may be in the ballpark.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:16
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
jbaxleyiiicommented, Jun 6, 2019

@MoonTahoe (👋 Alex!) This is a really great question 👍

The tldr; is this isn’t supported right now 😭

The exciting answer is this is on the roadmap 🎉 This is what it would look like:

type User @key(fields: "id") {
  id: ID!
  name: String
  username: String
  productsPurchased: [Product] @internal
  totalDollarsSpent: Float @requires(fields: "productsPurchased { price }")
}

extend type Product @key(fields: "upc") {
  upc: String! @external
  price: Int @external
}

A couple things introduced here (that will come in multiple releases):

@internal directive. This will probably come first in the scheme of things. Internal will allow for hiding fields from the overall schema that the client can consume.

@requires support when used on a base type. This will come a little bit later because it complicates the query planner a good bit 😆. Ultimately the query plan would be something like this:

{
  me {
    totalDollarsSpent
  }
}

{
  me {
    id
    productsPurchased {
      upc
    }
  }
}

{
  ... on Product {
    price
  }
}

{
   ... on User {
     totalDollarsSpent
   }
}
{
  totalDollarsSpent: ({ productsPurchased }) =>
    productsPurchased.reduce(({ price }, prev) => price + prev, 0)
}
3reactions
wzalazarcommented, May 15, 2020

I have the same problem, but with a Mutation that requires some extra data for checking some stuff, I tried to find some pretty solutions in the blog and the documentation, but I didn’t find anything so far. In this case, we will decide to use graphql-request pointing to another graphql service. The question will be:

  • How would Apollo federation work with the mutations?
  • Are we missing something?

I will glad if someone Can add some inputs or experiences how we must resolve these situations

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Subscriptions with Your Federated Data Graph
First, this approach requires all Subscription fields to be defined in a single, decoupled subscription service, so the ownership of this ...
Read more >
Child service data access to other services with Apollo ...
Example of a federated query. In this query, event , allocatedTo , purchasedBy , and product are all types in other services.
Read more >
A Guide to Data Federation: Everything You Need to Know
A federated data model allows you to quickly pull the data you need when you need it. Accurate data: Data federation takes data...
Read more >
Using Subscriptions with Your Federated Data - YouTube
With this approach, the subscriptions service can use any of the types defined in the federated data graph as output types for the ......
Read more >
Implementing Federated GraphQL Microservices using ...
Implement a single distributed data graph across multiple services without ... a unified interface to access more data and services through a single...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found