UNPKG

@graphql-hive/federation-gateway-audit

Version:
291 lines (274 loc) 5.6 kB
'use strict'; var testkit = require('./testkit-CAf-AsDy.cjs'); require('@apollo/composition'); require('graphql'); require('fets'); require('@apollo/subgraph'); require('graphql-yoga'); const products = [ { id: "p1", pid: "p1-pid", categoryId: "c1" }, { id: "p2", pid: "p2-pid", categoryId: "c2" }, { id: "p3", pid: "p3-pid", categoryId: "c1" } ]; const categories = [ { id: "c1", name: "c1-name", details: { products: products.filter((p) => p.categoryId === "c1").length } }, { id: "c2", name: "c2-name", details: { products: products.filter((p) => p.categoryId === "c2").length } } ]; var a = testkit.createSubgraph("a", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@external", "@shareable"] ) type Query { products: [Product!]! } type Product @key(fields: "id") @key(fields: "id pid") { id: ID! pid: ID! category: Category @shareable } type Category @key(fields: "id") { id: ID! name: String! @shareable } ` ), resolvers: { Query: { products() { return products.map((p) => ({ id: p.id, pid: p.pid, categoryId: p.categoryId })); } }, Product: { __resolveReference(key) { const product = products.find( (p) => p.id === key.id && ("pid" in key ? p.pid === key.pid : true) ); if (!product) { return null; } return { id: product.id, pid: product.pid, categoryId: product.categoryId }; }, category(product) { const category = categories.find((c) => c.id === product.categoryId); if (!category) { return null; } return { id: category.id, name: category.name }; } }, Category: { __resolveReference(key) { const category = categories.find((c) => c.id === key.id); if (!category) { return null; } return { id: category.id, name: category.name }; } } } }); var b = testkit.createSubgraph("b", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable"] ) type Product @key(fields: "id pid") { id: ID! pid: ID! category: Category @shareable } type Category @key(fields: "id") { id: ID! name: String! @shareable } ` ), resolvers: { Product: { __resolveReference(key) { const product = products.find( (p) => p.id === key.id && p.pid === key.pid ); if (!product) { return null; } return { id: product.id, pid: product.pid, categoryId: product.categoryId }; }, category(product) { const category = categories.find((c) => c.id === product.categoryId); if (!category) { return null; } return { id: category.id, name: category.name }; } }, Category: { __resolveReference(key) { const category = categories.find((c) => c.id === key.id); if (!category) { return null; } return { id: category.id, name: category.name }; } } } }); var c = testkit.createSubgraph("c", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable"] ) type Category { details: CategoryDetails } type Product @key(fields: "id pid") { id: ID! pid: ID! category: Category @shareable } type CategoryDetails { products: Int } ` ), resolvers: { Product: { __resolveReference(key) { const product = products.find( (p) => p.id === key.id && p.pid === key.pid ); if (!product) { return null; } return { id: product.id, pid: product.pid, categoryId: product.categoryId }; }, category(product) { const category = categories.find((c) => c.id === product.categoryId); if (!category) { return null; } return { details: category.details }; } } } }); var test = [ testkit.createTest( /* GraphQL */ ` query { products { id category { id details { products } } } } `, { data: { products: [ { id: "p1", category: { id: "c1", details: { products: 2 } } }, { id: "p2", category: { id: "c2", details: { products: 1 } } }, { id: "p3", category: { id: "c1", details: { products: 2 } } } ] } } ) ]; var index = testkit.serve("parent-entity-call", [a, b, c], test); exports.default = index;