UNPKG

@graphql-hive/federation-gateway-audit

Version:
447 lines (428 loc) 7.64 kB
'use strict'; var testkit = require('./testkit-CAf-AsDy.cjs'); require('@apollo/composition'); require('graphql'); require('fets'); require('@apollo/subgraph'); require('graphql-yoga'); const ovens = [ { __typename: "Oven", id: "oven1", warranty: 1 }, { __typename: "Oven", id: "oven2", warranty: 2 } ]; const toasters = [ { __typename: "Toaster", id: "toaster1", warranty: 3 }, { __typename: "Toaster", id: "toaster2", warranty: 4 } ]; const products = [...ovens, ...toasters]; var a = testkit.createSubgraph("a", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.3" import: ["@shareable", "@key"] ) type Query { products: [Product] node(id: ID!): Node nodes: [Node] toasters: [Toaster] } union Product = Oven | Toaster interface Node { id: ID! } interface WithWarranty { warranty: Int } type Oven @key(fields: "id") { id: ID! } type Toaster implements Node & WithWarranty @key(fields: "id") { id: ID! warranty: Int } ` ), resolvers: { Query: { products() { return products.map((product) => { if (product.__typename === "Oven") { return { __typename: "Oven", id: product.id }; } else { return { __typename: "Toaster", id: product.id, warranty: product.warranty }; } }); }, node(_, { id }) { const product = products.find((p) => p.id === id); if (product?.__typename === "Oven") { return { __typename: "Oven", id: product.id }; } else if (product?.__typename === "Toaster") { return { __typename: "Toaster", id: product.id, warranty: product.warranty }; } return null; }, toasters() { return products.filter((product) => product.__typename === "Toaster").map((toaster) => ({ __typename: "Toaster", id: toaster.id, warranty: toaster.warranty })); }, nodes() { return products.filter((product) => product.__typename === "Toaster").map((toaster) => ({ __typename: "Toaster", id: toaster.id, warranty: toaster.warranty })); } }, Oven: { __resolveReference(key) { const oven = products.find((p) => p.id === key.id); if (oven?.__typename === "Oven") { return { __typename: "Oven", id: oven.id }; } return null; }, warranty() { throw new Error("Never"); } }, Toaster: { __resolveReference(key) { const toaster = products.find((p) => p.id === key.id); if (toaster?.__typename === "Toaster") { return { __typename: "Toaster", id: toaster.id, warranty: toaster.warranty }; } return null; } } } }); var b = testkit.createSubgraph("b", { typeDefs: ( /* GraphQL */ ` extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"]) interface Node { id: ID! } interface WithWarranty { warranty: Int } type Oven implements Node & WithWarranty @key(fields: "id") { id: ID! warranty: Int } ` ), resolvers: { Oven: { __resolveReference(key) { const oven = products.find((p) => p.id === key.id); if (oven?.__typename === "Oven") { return { __typename: "Oven", id: oven.id, warranty: 1 }; } return null; } } } }); var test = [ testkit.createTest( /* GraphQL */ ` query { products { ... on Node { id } } } `, { data: { products: [ { id: "oven1" }, { id: "oven2" }, { id: "toaster1" }, { id: "toaster2" } ] } } ), testkit.createTest( /* GraphQL */ ` query { nodes { ... on Toaster { warranty } ... on Oven { id } } } `, { data: { nodes: [ { warranty: 3 }, { warranty: 4 } ] } } ), testkit.createTest( /* GraphQL */ ` query { nodes { id } } `, { data: { nodes: [ { id: "toaster1" }, { id: "toaster2" } ] } } ), testkit.createTest( /* GraphQL */ ` query { nodes { ... on Node { id } } } `, { data: { nodes: [ { id: "toaster1" }, { id: "toaster2" } ] } } ), testkit.createTest( /* GraphQL */ ` query { nodes { ... on Node { ... on WithWarranty { warranty } } } } `, { data: { nodes: [ { warranty: 3 }, { warranty: 4 } ] } } ), testkit.createTest( /* GraphQL */ ` query { nodes { ... on Node { id ... on Node { id ... on WithWarranty { warranty } } } } } `, { data: { nodes: [ { id: "toaster1", warranty: 3 }, { id: "toaster2", warranty: 4 } ] } } ), testkit.createTest( /* GraphQL */ ` { toasters { ...ToasterFragment ...NodeFragment } } fragment ToasterFragment on Toaster { id } fragment NodeFragment on Node { id __typename } `, { data: { toasters: [ { id: "toaster1", __typename: "Toaster" }, { id: "toaster2", __typename: "Toaster" } ] } } ), testkit.createTest( /* GraphQL */ ` query { node(id: "oven1") { ... on Oven { warranty } } } `, { data: { node: null }, errors: true } ), testkit.createTest( /* GraphQL */ ` query { node(id: "oven1") { ... on Toaster { warranty } } } `, { data: { node: null }, errors: true } ), testkit.createTest( /* GraphQL */ ` query { node(id: "toaster1") { ... on Toaster { warranty } } } `, { data: { node: { warranty: 3 } } } ) ]; var index = testkit.serve("union-interface-distributed", [a, b], test); exports.default = index;