UNPKG

@graphql-hive/federation-gateway-audit

Version:
375 lines (360 loc) 8.11 kB
'use strict'; var testkit = require('./testkit-CAf-AsDy.cjs'); var env = require('./env-Dj2M4ll3.cjs'); require('@apollo/composition'); require('graphql'); require('fets'); require('@apollo/subgraph'); require('graphql-yoga'); const products = [ { id: "p1", categories: ["c1", "c2"] }, { id: "p2", categories: ["c3", "c2"] } ]; const categories = [ { id: "c1", name: "Category 1", subCategories: ["c2"] }, { id: "c2", name: "Category 2", subCategories: ["c3"] }, { id: "c3", name: "Category 3", subCategories: ["c1"] } ]; var allProducts = testkit.createSubgraph("all-products", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable"] ) type Product @key(fields: "id") { id: ID! } ` ), resolvers: { Product: { __resolveReference(key) { if (env.shouldPunishForPoorPlans()) { throw new Error("You should be using the categories subgraph!"); } const product = products.find((p) => p.id === key.id); if (!product) { return null; } return { id: product.id }; } } } }); var category = testkit.createSubgraph("category", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable", "@external", "@provides"] ) type Query { products: [Product] @shareable @provides(fields: "categories { id name subCategories { id name } }") } type Product @key(fields: "id") { id: ID! categories: [Category] @external } type Category @key(fields: "id") { id: ID! """ This field is provided by Query.products and we deliberately don't resolve it. The test suite is about checking if @provides is used correctly. """ name: String subCategories: [Category] @external } ` ), resolvers: { Query: { products() { return products.map((p) => ({ id: p.id, categories: p.categories.map((catId) => { const cat = categories.find((c) => c.id === catId); if (!cat) { return null; } return { id: cat.id, name: cat.name, subCategories: cat.subCategories.map((subCatId) => { const subCat = categories.find((c) => c.id === subCatId); if (!subCat) { return null; } return { id: subCat.id, name: subCat.name }; }) }; }) })); } }, Product: { __resolveReference(key, context) { if (env.shouldPunishForPoorPlans()) { throw new Error("You should be using the categories subgraph!"); } const product = products.find((p) => p.id === key.id); if (!product) { return null; } return { id: product.id }; } }, Category: { __resolveReference(key, context) { if (env.shouldPunishForPoorPlans()) { throw new Error("You should be using the categories subgraph!"); } const cat = categories.find((c) => c.id === key.id); if (!cat) { return null; } return { id: cat.id // We explicitly don't resolve `name` here // name: cat.name, }; } } } }); var subcategories = testkit.createSubgraph("subcategories", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable"] ) type Product @key(fields: "id") { id: ID! categories: [Category] @shareable } type Category @key(fields: "id") { id: ID! subCategories: [Category] @shareable } ` ), resolvers: { Product: { __resolveReference(key, context) { if (env.shouldPunishForPoorPlans()) { throw new Error("You should be using the categories subgraph!"); } const product = products.find((p) => p.id === key.id); if (!product) { return null; } return { id: product.id, categories: product.categories }; }, categories(product, _a, context) { if (env.shouldPunishForPoorPlans()) { throw new Error("You should be using the categories subgraph!"); } return product.categories.map((catId) => { const cat = categories.find((c) => c.id === catId); if (!cat) { return null; } return { id: cat.id, subCategories: cat.subCategories }; }); } }, Category: { __resolveReference(key) { const cat = categories.find((c) => c.id === key.id); if (!cat) { return null; } return { id: cat.id, subCategories: cat.subCategories }; }, subCategories(category) { return category.subCategories.map((catId) => { const cat = categories.find((c) => c.id === catId); if (!cat) { return null; } return { id: cat.id, subCategories: cat.subCategories }; }); } } } }); var test = [ testkit.createTest( /* GraphQL */ ` query { products { id categories { id name } } } `, { data: { products: [ { id: "p1", categories: [ { id: "c1", name: "Category 1" }, { id: "c2", name: "Category 2" } ] }, { id: "p2", categories: [ { id: "c3", name: "Category 3" }, { id: "c2", name: "Category 2" } ] } ] } } ), testkit.createTest( /* GraphQL */ ` query { products { id categories { id name subCategories { id name } } } } `, { data: { products: [ { id: "p1", categories: [ { id: "c1", name: "Category 1", subCategories: [ { id: "c2", name: "Category 2" } ] }, { id: "c2", name: "Category 2", subCategories: [ { id: "c3", name: "Category 3" } ] } ] }, { id: "p2", categories: [ { id: "c3", name: "Category 3", subCategories: [ { id: "c1", name: "Category 1" } ] }, { id: "c2", name: "Category 2", subCategories: [ { id: "c3", name: "Category 3" } ] } ] } ] } } ) ]; var index = testkit.serve( "nested-provides", [allProducts, category, subcategories], test ); exports.default = index;