Moonpig Tech Blog
Published in

Moonpig Tech Blog

Our journey with GraphQL at Moonpig — Part 3

Why Federation

type Product {
id: Id
title: String
reviewScore: Number
}
type Query {
products: [Product]
}
type Product {
id: ID
title: String
}
type ProductReview {
productId: ID
reviewScore: Number
}
type Query {
products: [Product]
productReviews(productIds: [ID]): [ProductReview]
}
type Product @key(fields: “id”) {
id: ID
title: String
}
extend type Product @key(fields: “id”) {
id: ID @external
reviewScore: Number
}

Moving to federation

const gateway = new ApolloGateway({
serviceList: [
{ name: ‘accounts’, url: ‘http://localhost:4001' },
{ name: ‘products’, url: ‘http://localhost:4002' },
{ name: ‘reviews’, url: ‘http://localhost:4003' }
]
});
const server = new ApolloServer({ gateway });
const gateway = new ApolloGateway({
localServiceList: serviceDefinitions.map((sd) => {
return {
name: sd.name,
url: sd.url,
typeDefs: parse(sd.sdl),
};
}),
});

Custom transport

  • The willSendRequest hook lets you modify your gateway’s requests to the implementing service before they’re sent.
  • The didReceiveResponse hook lets you modify the implementing service’s responses before the gateway passes them along to the requesting client.
const gateway = new ApolloGateway({

buildService: (service) =>
new CustomRemoteGraphQLDataSource(service);
});

--

--

Learn about how we use technology, lean and agile practices to succeed in the online personalisation market whilst moving at supersonic speed.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store