@graphql-hive/federation-gateway-audit
Version:
Audit tool for Apollo Federation Gateway
219 lines (207 loc) • 4.34 kB
JavaScript
import { c as createSubgraph, a as createTest, s as serve } from './testkit-BtZjdl4n.js';
import '@apollo/composition';
import 'graphql';
import 'fets';
import '@apollo/subgraph';
import 'graphql-yoga';
const books = [
{
__typename: "Book",
id: "1",
upc: "b1",
author: {
__typename: "Author",
id: "a1",
name: "Alice"
}
},
{
__typename: "Book",
id: "2",
upc: "b2",
author: {
__typename: "Author",
id: "a2",
name: "Bob"
}
},
{
__typename: "Book",
id: "3",
upc: "b3",
author: {
__typename: "Author",
id: "a3",
name: "Jack"
}
}
];
var a = createSubgraph("a", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])
type Query {
bookContainers: [BookContainer]
}
type BookContainer {
book: Book
}
type Book @key(fields: "upc") {
upc: ID!
}
`
),
resolvers: {
Query: {
bookContainers() {
return books.map((book) => ({ book: { upc: book.upc } }));
}
},
Book: {
__resolveReference(reference) {
if (reference != null) {
let book = books.find((book2) => book2.upc === reference.upc);
if (book != null && book.upc !== null) {
return {
__typename: "Book",
upc: book.upc
};
}
}
throw new Error("Invalid reference");
}
}
}
});
var b = createSubgraph("b", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])
type Book @key(fields: "id") @key(fields: "upc") {
id: ID!
upc: ID!
}
`
),
resolvers: {
Book: {
__resolveReference(reference) {
if (reference != null) {
let book;
if ("id" in reference) {
book = books.find((book2) => book2.id === reference.id);
}
if ("upc" in reference) {
book = books.find((book2) => book2.upc === reference.upc);
}
if (book != null) {
if (book.id === "3") {
return null;
}
return {
__typename: "Book",
id: book.id,
upc: book.upc
};
}
}
throw new Error("Invalid reference");
}
}
}
});
var c = createSubgraph("c", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])
type Book @key(fields: "id") {
id: ID!
author: Author
}
type Author {
id: ID!
name: String
}
`
),
resolvers: {
Book: {
__resolveReference(reference) {
if (reference != null) {
let book;
if ("id" in reference && reference.id !== null) {
book = books.find((book2) => book2.id === reference.id);
}
if ("upc" in reference && reference.upc !== null) {
book = books.find((book2) => book2.upc === reference.upc);
}
if (book != null) {
return {
__typename: "Book",
id: book.id,
author: {
__typename: "Author",
id: book.author.id,
name: book.author.name
}
};
}
}
throw new Error("Invalid reference");
}
}
}
});
var test = [
createTest(
/* GraphQL */
`
query {
bookContainers {
book {
upc
author {
name
}
}
}
}
`,
{
data: {
bookContainers: [
{
book: {
upc: "b1",
author: {
name: "Alice"
}
}
},
{
book: {
upc: "b2",
author: {
name: "Bob"
}
}
},
{
book: {
upc: "b3",
author: null
}
}
]
}
}
)
];
var index = serve("null-keys", [a, b, c], test);
export { index as default };