menu

Apollo

A community of developers, designers and others who love Apollo and GraphQL. 🚀

Channels
# All channels
view-forward
# General
view-forward
# Apollo Angular
view-forward
# Announcements
view-forward
# Apollo Android
view-forward
# Apollo Client
view-forward
# Apollo iOS
view-forward
# Apollo Link
view-forward
# Apollo Link Rest
view-forward
# Local State
view-forward
# Apollo Studio
view-forward
# Apollo Server
view-forward
# Apollo Tooling
view-forward
# Contributing
view-forward
# Docs
view-forward
# Events
view-forward
# GraphQL Tools
view-forward
# Jobs
view-forward
# Random
view-forward
# React Apollo
view-forward
# Showcase
view-forward
# Subscriptions
view-forward
# Testing
view-forward
# Vue Apollo
view-forward
Team

How to resolve an array of IDs in Apollo Federation

December 7, 2019 at 4:26am

How to resolve an array of IDs in Apollo Federation

December 7, 2019 at 4:26am
Let's say I have an array of product entities with a key value of sku.
ie an Order consists of multiple products:
// sample order data
let orders = [
{
id: "order-1",
customer: "[email protected]",
lineItems: [{ sku: "widget:1"}, {sku:"widget:2"}] // array of product keys
},
{
id: "order-2",
customer: "[email protected]",
lineItems: [{sku: "widget:2"}] . // array of product keys
}
];
// sample product data
let products = [
{
id: "product-1",
title: "Widget 1",
sku: "widget:1",
price: 1200,
inStock: true
},
{
id: "product-2",
title: "Widget 2",
sku: "widget:2",
price: 500,
inStock: true
},
{
id: "product-3",
title: "Widget 3",
sku: "widget:3",
price: 2000,
inStock: false
}
];
// TypeDef Schema
type Order {
id: ID!
lineItems: [Product]
}
// extending product entity
extend type Product @key(fields: "sku") {
sku: String! @external
orders: [Order]
}
// Order resolver
Order: {
lineItems(order) {
order.lineItems.forEach(item => {
return { __typename: "Product", sku: item.sku}
})
}
},
// Product reference resolver
Product: {
__resolveReference(reference) {
return products.find(product => reference.sku === product.sku);
}
}
I'm having issues resolving the relationship when it is an array of SKUs. In theory, this makes sense to me because the lineItem resolver is passed in as the reference, yet I am unable to link up the reference SKU with product SKU. How would one resolve an array of objects?

January 21, 2020 at 7:49pm
Does anyone have any ideas on this topic? I am facing the same issue. Thanks!
  • reply
  • like

February 12, 2020 at 9:28am
I am also looking at similar. The only thing I can think of is to create a new type which is an array of objects and have that as the external reference. Feels a bit clunky. Any ideas?
  • reply
  • like

June 5, 2020 at 5:07pm
Hello, i'm also facing this issue, any ideas ??
Could you post an example of what you've done ? Thanks
  • reply
  • like