UNPKG

@graphql-hive/federation-gateway-audit

Version:
281 lines (267 loc) 5.98 kB
import { c as createSubgraph, a as createTest, s as serve } from './testkit-BtZjdl4n.js'; import { GraphQLError } from 'graphql'; import '@apollo/composition'; import 'fets'; import '@apollo/subgraph'; import 'graphql-yoga'; const products = [ { id: "p1", price: 699.99, hasDiscount: true } ]; var a = createSubgraph("a", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@inaccessible"] ) type Product @key(fields: "id") { id: ID! price: Float! @inaccessible } ` ), resolvers: { Product: { __resolveReference(key) { const product = products.find((product2) => product2.id === key.id); if (!product) { return null; } return { id: product.id, price: product.price }; } } } }); var b = createSubgraph("b", { typeDefs: ( /* GraphQL */ ` extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"]) type Query { product: Product } type Product @key(fields: "id") { id: ID! hasDiscount: Boolean! } ` ), resolvers: { Query: { product() { return { id: products[0].id, hasDiscount: products[0].hasDiscount }; } }, Product: { __resolveReference(key) { const product = products.find((product2) => product2.id === key.id); if (!product) { return null; } return { id: product.id, hasDiscount: product.hasDiscount }; } } } }); var c = createSubgraph("c", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@external", "@requires"] ) type Product @key(fields: "id") { id: ID! price: Float! @external isExpensive: Boolean! @requires(fields: "price") hasDiscount: Boolean! @external isExpensiveWithDiscount: Boolean! @requires(fields: "hasDiscount") } ` ), resolvers: { Product: { __resolveReference(key) { if (!products.find((product2) => product2.id === key.id)) { return null; } const product = { __typename: "Product", id: key.id }; if (typeof key.price === "number") { product.price = key.price; product.isExpensive = key.price > 500; } else if ("price" in key && typeof key.price !== "undefined") { return new GraphQLError("Product.price must be a number"); } if (typeof key.hasDiscount === "boolean") { product.hasDiscount = key.hasDiscount; product.isExpensiveWithDiscount = !key.hasDiscount; } else if ("hasDiscount" in key && typeof key.hasDiscount !== "undefined") { return new GraphQLError("Product.hasDiscount must be a number"); } return product; } } } }); var d = createSubgraph("d", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@external", "@requires"] ) type Product @key(fields: "id") { id: ID! isExpensive: Boolean! @external canAfford: Boolean! @requires(fields: "isExpensive") isExpensiveWithDiscount: Boolean! @external canAffordWithDiscount: Boolean! @requires(fields: "isExpensiveWithDiscount") } ` ), resolvers: { Product: { __resolveReference(key) { if (!products.find((product2) => product2.id === key.id)) { return null; } const product = { __typename: "Product", id: key.id }; if (typeof key.isExpensive === "boolean") { product.isExpensive = key.isExpensive; product.canAfford = !key.isExpensive; } else if ("isExpensive" in key && typeof key.isExpensive !== "undefined") { return new GraphQLError("Product.isExpensive must be a boolean"); } if (typeof key.isExpensiveWithDiscount === "boolean") { product.isExpensiveWithDiscount = key.isExpensiveWithDiscount; product.canAffordWithDiscount = !key.isExpensiveWithDiscount; } else if ("isExpensiveWithDiscount" in key && typeof key.isExpensiveWithDiscount !== "undefined") { return new GraphQLError( "Product.isExpensiveWithDiscount must be a boolean" ); } return product; } } } }); var test = [ createTest( /* GraphQL */ ` query { product { canAfford } } `, { data: { product: { canAfford: false } } } ), createTest( /* GraphQL */ ` query { product { isExpensive } } `, { data: { product: { isExpensive: true } } } ), createTest( /* GraphQL */ ` query { product { isExpensive canAfford } } `, { data: { product: { isExpensive: true, canAfford: false } } } ), createTest( /* GraphQL */ ` query { product { canAffordWithDiscount } } `, { data: { product: { canAffordWithDiscount: true } } } ), createTest( /* GraphQL */ ` query { product { canAfford canAffordWithDiscount } } `, { data: { product: { canAfford: false, canAffordWithDiscount: true } } } ) ]; var index = serve("requires-requires", [a, b, c, d], test); export { index as default };