UNPKG

@graphql-hive/federation-gateway-audit

Version:
1,838 lines (1,793 loc) 36.8 kB
'use strict'; var testkit = require('./testkit-CAf-AsDy.cjs'); require('@apollo/composition'); require('graphql'); require('fets'); require('@apollo/subgraph'); require('graphql-yoga'); const users = [ { id: "u1", email: "u1@example.com", name: "u1-name" }, { id: "u2", email: "u2@example.com", name: "u2-name" }, { id: "u3", email: "u3@example.com", name: "u3-name" } ]; const books = [ { __typename: "Book", id: "p1", sku: "sku-1", title: "Book 1", createdBy: "u1", dimensions: { size: "small", weight: 0.5 }, publisher: { __typename: "Self", email: users[0].email }, hidden: false }, { __typename: "Book", id: "p3", sku: "sku-3", title: "Book 2", createdBy: "u2", dimensions: { size: "small", weight: 0.6 }, publisher: { __typename: "Agency", id: "a1" }, hidden: false } ]; const magazines = [ { __typename: "Magazine", id: "p2", sku: "sku-2", title: "Magazine 1", createdBy: "u1", dimensions: { size: "small", weight: 0.2 }, publisher: { __typename: "Agency", id: "a1" }, hidden: false }, { __typename: "Magazine", id: "p4", sku: "sku-4", title: "Magazine 2", createdBy: "u2", dimensions: { size: "small", weight: 0.3 }, publisher: { __typename: "Self", email: users[0].email }, hidden: true } ]; const products = [...books, ...magazines]; const reviews = [ { id: 1, body: "review 1", productId: "p1", score: 3 }, { id: 2, body: "review 2", productId: "p1", score: 4 }, { id: 3, body: "review 3", productId: "p2", score: 5 } ]; const agencies = [ { id: "a1", companyName: "Agency 1", email: { address: "a1@example.com" } } ]; var inventorySubgraph = testkit.createSubgraph("inventory", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable", "@external", "@requires"] ) interface Product { id: ID! dimensions: ProductDimension delivery(zip: String): DeliveryEstimates } type Book implements Product @key(fields: "id") { id: ID! dimensions: ProductDimension @external delivery(zip: String): DeliveryEstimates @requires(fields: "dimensions { size weight }") } type Magazine implements Product @key(fields: "id") { id: ID! dimensions: ProductDimension @external delivery(zip: String): DeliveryEstimates @requires(fields: "dimensions { size weight }") } type ProductDimension @shareable { size: String weight: Float } type DeliveryEstimates { estimatedDelivery: String fastestDelivery: String } ` ), resolvers: { Book: { __resolveReference(key) { const book = books.find((book2) => book2.id === key.id); if (!book) { return null; } return { __typename: book.__typename, id: book.id, dimensions: key.dimensions }; }, delivery({ dimensions }, { zip }) { if (!zip) { return null; } if (!dimensions) { return null; } if (dimensions.size === "small" && dimensions.weight < 1) { return { estimatedDelivery: "1 day", fastestDelivery: "same day" }; } if (dimensions.size === "large" && dimensions.weight >= 1) { return { estimatedDelivery: "3 days", fastestDelivery: "2 days" }; } return null; } }, Magazine: { __resolveReference(key) { const magazine = magazines.find((magazine2) => magazine2.id === key.id); if (!magazine) { return null; } return { __typename: magazine.__typename, id: magazine.id, dimensions: key.dimensions }; }, delivery({ dimensions }, { zip }) { if (!zip) { return null; } if (!dimensions) { return null; } if (dimensions.size === "small" && dimensions.weight < 1) { return { estimatedDelivery: "1 day", fastestDelivery: "same day" }; } if (dimensions.size === "large" && dimensions.weight >= 1) { return { estimatedDelivery: "3 days", fastestDelivery: "2 days" }; } return null; } } } }); var booksSubgraph = testkit.createSubgraph("books", { typeDefs: ( /* GraphQL */ ` schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable", "@external", "@requires"] ) { query: Query } type Book @key(fields: "id") { id: ID! title: String } type Query { books: [Book] } ` ), resolvers: { Query: { books() { return books.map((book) => ({ __typename: "Book", id: book.id, title: book.title })); } }, Book: { __resolveReference(key) { const book = books.find((book2) => book2.id === key.id); return book ? { __typename: "Book", id: book.id, title: book.title } : null; } } } }); var usersSubgraph = testkit.createSubgraph("users", { typeDefs: ( /* GraphQL */ ` type User @key(fields: "email") { email: ID! name: String totalProductsCreated: Int } ` ), resolvers: { User: { __resolveReference(key) { const user = users.find((user2) => user2.email === key.email); return user ? { __typename: "User", id: user.id, email: user.email, name: user.name } : null; }, totalProductsCreated(user) { return products.filter((p) => p.createdBy === user.id).length; } } } }); var productsSubgraph = testkit.createSubgraph("products", { typeDefs: ( /* GraphQL */ ` schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable", "@inaccessible", "@external"] ) { query: Query } type Query { products: [Product] similar(id: ID!): [Product] } interface Product { id: ID! sku: String dimensions: ProductDimension createdBy: User hidden: Boolean @inaccessible } interface Similar { similar: [Product] } type ProductDimension @shareable { size: String weight: Float } type Book implements Product & Similar @key(fields: "id") { id: ID! sku: String dimensions: ProductDimension @shareable createdBy: User similar: [Book] hidden: Boolean publisherType: PublisherType } type Magazine implements Product & Similar @key(fields: "id") { id: ID! sku: String dimensions: ProductDimension @shareable createdBy: User similar: [Magazine] hidden: Boolean publisherType: PublisherType } union PublisherType = Agency | Self type Agency { id: ID! @shareable } type Self { email: String } type User @key(fields: "email") { email: ID! totalProductsCreated: Int @shareable } ` ), resolvers: { Query: { products() { return products.map((p) => ({ __typename: p.__typename, id: p.id, sku: p.sku, dimensions: p.dimensions, createdBy: p.createdBy, hidden: p.hidden, publisherType: p.publisher })); }, similar(_, { id }) { const product = products.find((product2) => product2.id === id); if (!product) { return []; } const similar = products.filter( (p) => p.id !== product.id && p.__typename === product.__typename ); return similar.map((b) => ({ __typename: b.__typename, id: b.id, sku: b.sku, dimensions: b.dimensions, createdBy: b.createdBy, hidden: b.hidden, publisherType: b.publisher })); } }, Book: { __resolveReference(key) { const book = books.find((book2) => book2.id === key.id); return book ? { __typename: book.__typename, id: book.id, sku: book.sku, dimensions: book.dimensions, createdBy: book.createdBy, hidden: book.hidden, publisherType: book.publisher } : null; }, similar(book) { return books.filter((b) => b.id !== book.id).map((b) => ({ __typename: b.__typename, id: b.id, sku: b.sku, dimensions: b.dimensions, createdBy: b.createdBy, hidden: b.hidden, publisherType: b.publisher })); }, createdBy(book) { const user = users.find((user2) => user2.id === book.createdBy); return user ? { __typename: "User", id: user.id, email: user.email } : null; } }, Magazine: { __resolveReference(key) { const magazine = magazines.find((magazine2) => magazine2.id === key.id); return magazine ? { __typename: magazine.__typename, id: magazine.id, sku: magazine.sku, dimensions: magazine.dimensions, createdBy: magazine.createdBy, hidden: magazine.hidden, publisherType: magazine.publisher } : null; }, similar(magazine) { return magazines.filter((m) => m.id !== magazine.id).map((m) => ({ __typename: m.__typename, id: m.id, sku: m.sku, dimensions: m.dimensions, createdBy: m.createdBy, hidden: m.hidden, publisherType: m.publisher })); }, createdBy(magazine) { const user = users.find((user2) => user2.id === magazine.createdBy); return user ? { __typename: "User", id: user.id, email: user.email } : null; } }, User: { __resolveReference(key) { const user = users.find((user2) => user2.email === key.email); return user ? { __typename: "User", id: user.id, email: user.email } : null; }, totalProductsCreated(user) { return products.filter((product) => product.createdBy === user.id).length; } } } }); const Product = { __resolveReference(key) { const product = products.find((product2) => product2.id === key.id); return product ? { __typename: product.__typename, id: product.id, similar: key.similar } : null; }, reviews(product) { return reviews.filter((review) => review.productId === product.id).map((review) => { const product2 = products.find( (product3) => product3.id === review.productId ); return { __typename: "Review", id: review.id, body: review.body, product: product2 ? { __typename: product2.__typename, id: product2.id } : null }; }); }, reviewsCount(product) { return reviews.filter((review) => review.productId === product.id).length; }, reviewsScore(product) { const productReviews = reviews.filter( (review) => review.productId === product.id ); if (productReviews.length === 0) { return 0; } const sum = productReviews.reduce((acc, review) => acc + review.score, 0); return sum / productReviews.length; }, reviewsOfSimilar(product) { const similarProducts = products.filter( (p) => p.__typename === product.__typename && product.similar.id.includes(p.id) ); return similarProducts.flatMap((p) => reviews.filter((review) => review.productId === p.id)).map((review) => { const product2 = products.find( (product3) => product3.id === review.productId ); return { __typename: "Review", id: review.id, body: review.body, product: product2 ? { __typename: product2.__typename, id: product2.id } : null }; }); } }; var reviewsSubgraph = testkit.createSubgraph("reviews", { typeDefs: ( /* GraphQL */ ` schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable", "@requires", "@external"] ) { query: Query } type Book implements Product & Similar @key(fields: "id") { id: ID! reviewsCount: Int! reviewsScore: Float! @shareable reviews: [Review!]! similar: [Book] @external reviewsOfSimilar: [Review!]! @requires(fields: "similar { id }") } type Magazine implements Product & Similar @key(fields: "id") { id: ID! reviewsCount: Int! reviewsScore: Float! @shareable reviews: [Review!]! similar: [Magazine] @external reviewsOfSimilar: [Review!]! @requires(fields: "similar { id }") } interface Product { id: ID! reviewsCount: Int! reviewsScore: Float! reviews: [Review!]! } interface Similar { similar: [Product] } type Query { review(id: Int!): Review } type Review { id: Int! body: String! product: Product } ` ), resolvers: { Query: { review(_, { id }) { const review = reviews.find((review2) => review2.id === id); if (!review) { return null; } const product = products.find( (product2) => product2.id === review.productId ); return { __typename: "Review", id: review.id, body: review.body, product: product ? { __typename: product.__typename, id: product.id } : null }; } }, Book: Product, Magazine: Product } }); var magazinesSubgraph = testkit.createSubgraph("magazines", { typeDefs: ( /* GraphQL */ ` schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@key", "@shareable"] ) { query: Query } type Magazine @key(fields: "id") { id: ID! title: String } type Query { magazines: [Magazine] } ` ), resolvers: { Query: { magazines() { return magazines.map((magazine) => ({ __typename: magazine.__typename, id: magazine.id, title: magazine.title })); } }, Magazine: { __resolveReference(key) { const magazine = magazines.find((magazine2) => magazine2.id === key.id); return magazine ? { __typename: magazine.__typename, id: magazine.id, title: magazine.title } : null; } } } }); var agencySubgraph = testkit.createSubgraph("agency", { typeDefs: ( /* GraphQL */ ` extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"]) type Agency @key(fields: "id") { id: ID! companyName: String email: Email } type Email { address: String } type Group @key(fields: "id") { id: ID! name: String email: String } extend union PublisherType = Agency | Group ` ), resolvers: { Agency: { __resolveReference(key) { return agencies.find((a) => a.id === key.id); } }, Group: { __resolveReference(key) { return { id: key.id, name: "Group " + key.id, email: "group" + key.id + "@example.com" }; } } } }); var test = [ testkit.createTest( /* GraphQL */ ` query { products { id dimensions { size weight } } } `, { data: { products: [ { id: "p1", dimensions: { size: "small", weight: 0.5 } }, { id: "p3", dimensions: { size: "small", weight: 0.6 } }, { id: "p2", dimensions: { size: "small", weight: 0.2 } }, { id: "p4", dimensions: { size: "small", weight: 0.3 } } ] } } ), testkit.createTest( /* GraphQL */ ` query { similar(id: "p1") { id sku } } `, { data: { similar: [ { id: "p3", sku: "sku-3" } ] } } ), testkit.createTest( /* GraphQL */ ` query { book: similar(id: "p1") { id sku delivery(zip: "1234") { fastestDelivery estimatedDelivery } } magazine: similar(id: "p2") { id sku delivery(zip: "1234") { fastestDelivery estimatedDelivery } } } `, { data: { book: [ { id: "p3", sku: "sku-3", delivery: { fastestDelivery: "same day", estimatedDelivery: "1 day" } } ], magazine: [ { id: "p4", sku: "sku-4", delivery: { fastestDelivery: "same day", estimatedDelivery: "1 day" } } ] } } ), testkit.createTest( /* GraphQL */ ` { products { sku ... on Product { sku } ... on Book { sku } ... on Magazine { sku } ... on Similar { __typename type: __typename } } } `, { data: { products: [ { sku: "sku-1", __typename: "Book", type: "Book" }, { sku: "sku-3", __typename: "Book", type: "Book" }, { sku: "sku-2", __typename: "Magazine", type: "Magazine" }, { sku: "sku-4", __typename: "Magazine", type: "Magazine" } ] } } ), testkit.createTest( /* GraphQL */ ` { products { author: createdBy { email totalProductsCreated } ... on Magazine { title } } } `, { data: { products: [ { author: { email: "u1@example.com", totalProductsCreated: 2 } }, { author: { email: "u2@example.com", totalProductsCreated: 2 } }, { author: { email: "u1@example.com", totalProductsCreated: 2 }, title: "Magazine 1" }, { author: { email: "u2@example.com", totalProductsCreated: 2 }, title: "Magazine 2" } ] } } ), testkit.createTest( /* GraphQL */ ` { products { id reviews { product { sku ... on Magazine { title } ... on Book { reviewsCount } } } } } `, { data: { products: [ { id: "p1", reviews: [ { product: { sku: "sku-1", reviewsCount: 2 } }, { product: { sku: "sku-1", reviewsCount: 2 } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { sku: "sku-2", title: "Magazine 1" } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` { products { id reviews { id } } } `, { data: { products: [ { id: "p1", reviews: [ { id: 1 }, { id: 2 } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { id: 3 } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` query ($title: Boolean = true) { products { id reviews { product { id ... on Book @include(if: $title) { title } ... on Magazine { sku } } } } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1", title: "Book 1" } }, { product: { id: "p1", title: "Book 1" } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", sku: "sku-2" } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` query ($title: Boolean = false) { products { id reviews { product { id ... on Book @include(if: $title) { title } ... on Magazine { sku } } } } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1" } }, { product: { id: "p1" } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", sku: "sku-2" } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` query ($title: Boolean = false) { products { id reviews { product { id ... on Book @skip(if: $title) { title } ... on Magazine { sku } } } } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1", title: "Book 1" } }, { product: { id: "p1", title: "Book 1" } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", sku: "sku-2" } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` query ($title: Boolean = true) { products { id reviews { product { id ... on Book @skip(if: $title) { title } ... on Magazine { sku } } } } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1" } }, { product: { id: "p1" } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", sku: "sku-2" } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` query ($title: Boolean = true) { products { id reviews { product { id ... on Book @skip(if: $title) { title } ... on Book { sku } ... on Magazine { sku } } } } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1", sku: "sku-1" } }, { product: { id: "p1", sku: "sku-1" } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", sku: "sku-2" } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` query ($title: Boolean = false) { products { id reviews { product { id ... on Book @skip(if: $title) { title } ... on Book { sku } ... on Magazine { sku } } } } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1", title: "Book 1", sku: "sku-1" } }, { product: { id: "p1", title: "Book 1", sku: "sku-1" } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", sku: "sku-2" } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` query ($title: Boolean = true) { products { id reviews { product { id ... on Book @include(if: $title) { title ... on Book { sku } } ... on Magazine { sku } } } } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1", title: "Book 1", sku: "sku-1" } }, { product: { id: "p1", title: "Book 1", sku: "sku-1" } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", sku: "sku-2" } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` query ($title: Boolean = true) { products { id reviews { product { id ... on Book @include(if: $title) { title ... on Book @skip(if: $title) { sku } } ... on Magazine { sku } } } } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1", title: "Book 1" } }, { product: { id: "p1", title: "Book 1" } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", sku: "sku-2" } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` { products { id reviews { product { id ... on Magazine { publisherType { ...Publisher } } ... on Book { publisherType { ...Publisher } } } } } } fragment Publisher on PublisherType { ... on Agency { id companyName } ... on Self { email } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1", publisherType: { email: "u1@example.com" } } }, { product: { id: "p1", publisherType: { email: "u1@example.com" } } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", publisherType: { id: "a1", companyName: "Agency 1" } } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` { products { id reviews { product { id ... on Magazine { publisherType { ...Publisher } } ... on Book { publisherType { ...Publisher } } } } } } fragment Publisher on PublisherType { ... on Agency { id companyName } ... on Self { email } ... on Group { name } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1", publisherType: { email: "u1@example.com" } } }, { product: { id: "p1", publisherType: { email: "u1@example.com" } } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", publisherType: { id: "a1", companyName: "Agency 1" } } } ] }, { id: "p4", reviews: [] } ] } } ), testkit.createTest( /* GraphQL */ ` { products { id reviews { product { id ... on Magazine { publisherType { ...Publisher } } ... on Book { publisherType { ...Publisher } } } } } } fragment Publisher on PublisherType { ... on Agency { emailObj: email { address } } ... on Self { emailStr: email } ... on Group { emailStr: email } } `, { data: { products: [ { id: "p1", reviews: [ { product: { id: "p1", publisherType: { emailStr: "u1@example.com" } } }, { product: { id: "p1", publisherType: { emailStr: "u1@example.com" } } } ] }, { id: "p3", reviews: [] }, { id: "p2", reviews: [ { product: { id: "p2", publisherType: { emailObj: { address: "a1@example.com" } } } } ] }, { id: "p4", reviews: [] } ] } } ) ]; var index = testkit.serve( "abstract-types", [ agencySubgraph, inventorySubgraph, booksSubgraph, usersSubgraph, reviewsSubgraph, productsSubgraph, magazinesSubgraph ], test ); exports.default = index;