UNPKG

@graphql-hive/federation-gateway-audit

Version:
191 lines (174 loc) 3.66 kB
'use strict'; var testkit = require('./testkit-CAf-AsDy.cjs'); require('@apollo/composition'); require('graphql'); require('fets'); require('@apollo/subgraph'); require('graphql-yoga'); const message = "Hello, Federation!"; const alpha = { __typename: "Alpha", id: "alpha-1", value: "alpha value" }; const beta = { __typename: "Beta", id: "beta-1", name: "beta name", details: "beta details" }; const gamma = { __typename: "Gamma", id: "gamma-1", label: "gamma label" }; const actions = [alpha, beta, gamma]; var a = testkit.createSubgraph("a", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.0" import: ["@key", "@shareable"] ) type Query { getResponse: Response } type Response @shareable { actions: [Action!]! message: String } union Action = Alpha | Beta | Gamma type Alpha @shareable { id: ID! value: String } type Beta { id: ID! name: String details: String } type Gamma { id: ID! label: String } ` ), resolvers: { Query: { getResponse: () => ({ message, actions }) } } }); var b = testkit.createSubgraph("b", { typeDefs: ( /* GraphQL */ ` extend schema @link( url: "https://specs.apollo.dev/federation/v2.0" import: ["@key", "@shareable"] ) type Response @shareable { actions: [Action!]! message: String } union Action = Alpha type Alpha @shareable { id: ID! value: String } ` ), resolvers: {} }); var test = [ // The query selects all three union members. `Beta` and `Gamma` exist only // in the primary subgraph and have no `@key`, so they can only be resolved // there. A correct gateway keeps every inline fragment and returns all three. testkit.createTest( /* GraphQL */ ` query { getResponse { message actions { __typename ... on Alpha { id value } ... on Beta { id name details } ... on Gamma { id label } } } } `, { data: { getResponse: { message, actions: [ { __typename: alpha.__typename, id: alpha.id, value: alpha.value }, { __typename: beta.__typename, id: beta.id, name: beta.name, details: beta.details }, { __typename: gamma.__typename, id: gamma.id, label: gamma.label } ] } } } ), // Selecting only the members missing from the secondary subgraph must still // resolve them from the primary subgraph. testkit.createTest( /* GraphQL */ ` query { getResponse { actions { __typename ... on Beta { name } ... on Gamma { label } } } } `, { data: { getResponse: { actions: [ { __typename: alpha.__typename }, { __typename: beta.__typename, name: beta.name }, { __typename: gamma.__typename, label: gamma.label } ] } } } ) ]; var index = testkit.serve("partial-union", [a, b], test); exports.default = index;