UNPKG

@graphql-hive/federation-gateway-audit

Version:
172 lines (161 loc) 3.05 kB
import { c as createSubgraph, a as createTest, s as serve } from './testkit-BtZjdl4n.js'; import '@apollo/composition'; import 'graphql'; import 'fets'; import '@apollo/subgraph'; import 'graphql-yoga'; const products = [ { id: "1", name: "name-1", price: 100 }, { id: "2", name: "name-2", price: 200 } ]; var price = createSubgraph("price", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@external"] ) # Composition will fail if it's not an extension (I have no idea why this is the case) extend type Product @key(fields: "id") { id: ID! @external price: Float } type Query { cheapestProduct: Product } ` ), resolvers: { Product: { __resolveReference(key) { const product = products.find((product2) => product2.id === key.id); if (!product) { return null; } return { id: product.id, price: product.price }; } }, Query: { cheapestProduct() { let cheapest = null; for (const product of products) { if (cheapest) { if (product.price < cheapest.price) { cheapest = product; } } else { cheapest = product; } } return cheapest; } } } }); var product = createSubgraph("product", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@external"] ) type Product @key(fields: "id") { id: ID! name: String! } type Query { products: [Product!]! } ` ), resolvers: { Product: { __resolveReference(key) { const product = products.find((product2) => product2.id === key.id); if (!product) { return null; } return { id: product.id, name: product.name }; } }, Query: { products() { return products.map((p) => ({ id: p.id, name: p.name })); } } } }); var test = [ createTest( /* GraphQL */ ` query { cheapestProduct { id price name } } `, { data: { cheapestProduct: { id: "1", price: 100, name: "name-1" } } } ), createTest( /* GraphQL */ ` query { products { name price id } } `, { data: { products: [ { name: "name-1", price: 100, id: "1" }, { name: "name-2", price: 200, id: "2" } ] } } ) ]; var index = serve("mysterious-external", [price, product], test); export { index as default };