UNPKG

@graphql-hive/federation-gateway-audit

Version:
209 lines (196 loc) 4.45 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 = [ { upc: "p1", name: "p-name-1", price: 11, weight: 1, category: { averagePrice: 11 } }, { upc: "p2", name: "p-name-2", price: 22, weight: 2, category: { averagePrice: 22 } } ]; var a = testkit.createSubgraph("a", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@external", "@requires"] ) type Product @key(fields: "upc") { upc: String! weight: Int @external price(currency: String!): Int @external shippingEstimate: Int @requires( fields: """ price(currency: "USD") weight """ ) shippingEstimateEUR: Int @requires( fields: """ price(currency: "EUR") weight """ ) category: Category @external isExpensiveCategory: Boolean @requires( fields: """ category { averagePrice(currency: "USD") } """ ) } type Category @external { averagePrice(currency: String!): Int } ` ), resolvers: { Product: { __resolveReference(key) { const product = products.find((p) => p.upc === key.upc); if (!product) { return null; } if ("weight" in key && "price" in key) { return { upc: product.upc, weight: key.weight, price: key.price, category: product.category }; } return { upc: product.upc, category: product.category }; }, shippingEstimate(product) { const value = product.price * product.weight * 10; return value; }, shippingEstimateEUR(product) { const value = product.price * product.weight * 10; return value; }, isExpensiveCategory(product) { return product.category.averagePrice > 11; } } } }); var b = testkit.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) { 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, args) { if (args.currency === "EUR") return parent.price * 2; if (args.currency === "USD") return parent.price; throw new Error("Unsupported currency " + args.currency); } } } }); var test = [ testkit.createTest( /* GraphQL */ ` query { products { upc name shippingEstimate shippingEstimateEUR isExpensiveCategory } } `, { data: { products: [ { upc: "p1", name: "p-name-1", shippingEstimate: 110, // 11 * 1 * 10 shippingEstimateEUR: 220, // (11 * 2) * 1 * 10 isExpensiveCategory: false }, { upc: "p2", name: "p-name-2", shippingEstimate: 440, // 22 * 2 * 10 shippingEstimateEUR: 880, // (22 * 2) * 2 * 10 isExpensiveCategory: true } ] } } ) ]; var index = testkit.serve("requires-with-argument-conflict", [a, b], test); exports.default = index;