@graphql-hive/federation-gateway-audit
Version:
Audit tool for Apollo Federation Gateway
174 lines (162 loc) • 3.07 kB
JavaScript
;
var testkit = require('./testkit-CAf-AsDy.cjs');
require('@apollo/composition');
require('graphql');
require('fets');
require('@apollo/subgraph');
require('graphql-yoga');
const products = [
{
id: "1",
name: "name-1",
price: 100
},
{
id: "2",
name: "name-2",
price: 200
}
];
var price = testkit.createSubgraph("price", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external"]
)
# Composition will fail if it's not an extension (I have no idea why this is the case)
extend type Product @key(fields: "id") {
id: ID! @external
price: Float
}
type Query {
cheapestProduct: Product
}
`
),
resolvers: {
Product: {
__resolveReference(key) {
const product = products.find((product2) => product2.id === key.id);
if (!product) {
return null;
}
return {
id: product.id,
price: product.price
};
}
},
Query: {
cheapestProduct() {
let cheapest = null;
for (const product of products) {
if (cheapest) {
if (product.price < cheapest.price) {
cheapest = product;
}
} else {
cheapest = product;
}
}
return cheapest;
}
}
}
});
var product = testkit.createSubgraph("product", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external"]
)
type Product @key(fields: "id") {
id: ID!
name: String!
}
type Query {
products: [Product!]!
}
`
),
resolvers: {
Product: {
__resolveReference(key) {
const product = products.find((product2) => product2.id === key.id);
if (!product) {
return null;
}
return {
id: product.id,
name: product.name
};
}
},
Query: {
products() {
return products.map((p) => ({
id: p.id,
name: p.name
}));
}
}
}
});
var test = [
testkit.createTest(
/* GraphQL */
`
query {
cheapestProduct {
id
price
name
}
}
`,
{
data: {
cheapestProduct: {
id: "1",
price: 100,
name: "name-1"
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
products {
name
price
id
}
}
`,
{
data: {
products: [
{
name: "name-1",
price: 100,
id: "1"
},
{
name: "name-2",
price: 200,
id: "2"
}
]
}
}
)
];
var index = testkit.serve("mysterious-external", [price, product], test);
exports.default = index;