@graphql-hive/federation-gateway-audit
Version:
Audit tool for Apollo Federation Gateway
373 lines (359 loc) • 8.09 kB
JavaScript
import { c as createSubgraph, a as createTest, s as serve } from './testkit-BtZjdl4n.js';
import { s as shouldPunishForPoorPlans } from './env-j1f_SZtG.js';
import '@apollo/composition';
import 'graphql';
import 'fets';
import '@apollo/subgraph';
import 'graphql-yoga';
const products = [
{
id: "p1",
categories: ["c1", "c2"]
},
{
id: "p2",
categories: ["c3", "c2"]
}
];
const categories = [
{
id: "c1",
name: "Category 1",
subCategories: ["c2"]
},
{
id: "c2",
name: "Category 2",
subCategories: ["c3"]
},
{
id: "c3",
name: "Category 3",
subCategories: ["c1"]
}
];
var allProducts = createSubgraph("all-products", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable"]
)
type Product @key(fields: "id") {
id: ID!
}
`
),
resolvers: {
Product: {
__resolveReference(key) {
if (shouldPunishForPoorPlans()) {
throw new Error("You should be using the categories subgraph!");
}
const product = products.find((p) => p.id === key.id);
if (!product) {
return null;
}
return {
id: product.id
};
}
}
}
});
var category = createSubgraph("category", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable", "@external", "@provides"]
)
type Query {
products: [Product]
@shareable
@provides(fields: "categories { id name subCategories { id name } }")
}
type Product @key(fields: "id") {
id: ID!
categories: [Category] @external
}
type Category @key(fields: "id") {
id: ID!
"""
This field is provided by Query.products
and we deliberately don't resolve it.
The test suite is about checking if @provides is used correctly.
"""
name: String
subCategories: [Category] @external
}
`
),
resolvers: {
Query: {
products() {
return products.map((p) => ({
id: p.id,
categories: p.categories.map((catId) => {
const cat = categories.find((c) => c.id === catId);
if (!cat) {
return null;
}
return {
id: cat.id,
name: cat.name,
subCategories: cat.subCategories.map((subCatId) => {
const subCat = categories.find((c) => c.id === subCatId);
if (!subCat) {
return null;
}
return {
id: subCat.id,
name: subCat.name
};
})
};
})
}));
}
},
Product: {
__resolveReference(key, context) {
if (shouldPunishForPoorPlans()) {
throw new Error("You should be using the categories subgraph!");
}
const product = products.find((p) => p.id === key.id);
if (!product) {
return null;
}
return {
id: product.id
};
}
},
Category: {
__resolveReference(key, context) {
if (shouldPunishForPoorPlans()) {
throw new Error("You should be using the categories subgraph!");
}
const cat = categories.find((c) => c.id === key.id);
if (!cat) {
return null;
}
return {
id: cat.id
// We explicitly don't resolve `name` here
// name: cat.name,
};
}
}
}
});
var subcategories = createSubgraph("subcategories", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable"]
)
type Product @key(fields: "id") {
id: ID!
categories: [Category] @shareable
}
type Category @key(fields: "id") {
id: ID!
subCategories: [Category] @shareable
}
`
),
resolvers: {
Product: {
__resolveReference(key, context) {
if (shouldPunishForPoorPlans()) {
throw new Error("You should be using the categories subgraph!");
}
const product = products.find((p) => p.id === key.id);
if (!product) {
return null;
}
return {
id: product.id,
categories: product.categories
};
},
categories(product, _a, context) {
if (shouldPunishForPoorPlans()) {
throw new Error("You should be using the categories subgraph!");
}
return product.categories.map((catId) => {
const cat = categories.find((c) => c.id === catId);
if (!cat) {
return null;
}
return {
id: cat.id,
subCategories: cat.subCategories
};
});
}
},
Category: {
__resolveReference(key) {
const cat = categories.find((c) => c.id === key.id);
if (!cat) {
return null;
}
return {
id: cat.id,
subCategories: cat.subCategories
};
},
subCategories(category) {
return category.subCategories.map((catId) => {
const cat = categories.find((c) => c.id === catId);
if (!cat) {
return null;
}
return {
id: cat.id,
subCategories: cat.subCategories
};
});
}
}
}
});
var test = [
createTest(
/* GraphQL */
`
query {
products {
id
categories {
id
name
}
}
}
`,
{
data: {
products: [
{
id: "p1",
categories: [
{
id: "c1",
name: "Category 1"
},
{
id: "c2",
name: "Category 2"
}
]
},
{
id: "p2",
categories: [
{
id: "c3",
name: "Category 3"
},
{
id: "c2",
name: "Category 2"
}
]
}
]
}
}
),
createTest(
/* GraphQL */
`
query {
products {
id
categories {
id
name
subCategories {
id
name
}
}
}
}
`,
{
data: {
products: [
{
id: "p1",
categories: [
{
id: "c1",
name: "Category 1",
subCategories: [
{
id: "c2",
name: "Category 2"
}
]
},
{
id: "c2",
name: "Category 2",
subCategories: [
{
id: "c3",
name: "Category 3"
}
]
}
]
},
{
id: "p2",
categories: [
{
id: "c3",
name: "Category 3",
subCategories: [
{
id: "c1",
name: "Category 1"
}
]
},
{
id: "c2",
name: "Category 2",
subCategories: [
{
id: "c3",
name: "Category 3"
}
]
}
]
}
]
}
}
)
];
var index = serve(
"nested-provides",
[allProducts, category, subcategories],
test
);
export { index as default };