@graphql-hive/federation-gateway-audit
Version:
Audit tool for Apollo Federation Gateway
61 lines (54 loc) • 1.45 kB
text/typescript
import { createSubgraph } from "../../subgraph.js";
import { products } from "./data.js";
export default createSubgraph("b", {
typeDefs: /* GraphQL */ `
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])
type Query {
products: [Product]
}
type Product @key(fields: "upc") {
upc: String!
name: String
price(currency: String!): Int
weight: Int
category: Category
}
type Category {
averagePrice(currency: String!): Int
}
`,
resolvers: {
Query: {
products() {
return products.map((p) => ({
upc: p.upc,
name: p.name,
price: p.price,
weight: p.weight,
category: p.category,
}));
},
},
Product: {
__resolveReference(key: { upc: string }) {
const product = products.find((p) => p.upc === key.upc);
if (!product) {
return null;
}
return {
upc: product.upc,
name: product.name,
price: product.price,
weight: product.weight,
category: product.category,
};
},
price(parent: { price: number }, args: { currency: string }) {
if (args.currency === "EUR") return parent.price * 2;
if (args.currency === "USD") return parent.price;
throw new Error("Unsupported currency " + args.currency);
},
},
},
});