@graphql-hive/federation-gateway-audit
Version:
Audit tool for Apollo Federation Gateway
283 lines (268 loc) • 6.06 kB
JavaScript
'use strict';
var testkit = require('./testkit-CAf-AsDy.cjs');
var graphql = require('graphql');
require('@apollo/composition');
require('fets');
require('@apollo/subgraph');
require('graphql-yoga');
const products = [
{
id: "p1",
price: 699.99,
hasDiscount: true
}
];
var a = testkit.createSubgraph("a", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@inaccessible"]
)
type Product @key(fields: "id") {
id: ID!
price: Float! @inaccessible
}
`
),
resolvers: {
Product: {
__resolveReference(key) {
const product = products.find((product2) => product2.id === key.id);
if (!product) {
return null;
}
return {
id: product.id,
price: product.price
};
}
}
}
});
var b = testkit.createSubgraph("b", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])
type Query {
product: Product
}
type Product @key(fields: "id") {
id: ID!
hasDiscount: Boolean!
}
`
),
resolvers: {
Query: {
product() {
return {
id: products[0].id,
hasDiscount: products[0].hasDiscount
};
}
},
Product: {
__resolveReference(key) {
const product = products.find((product2) => product2.id === key.id);
if (!product) {
return null;
}
return {
id: product.id,
hasDiscount: product.hasDiscount
};
}
}
}
});
var c = testkit.createSubgraph("c", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external", "@requires"]
)
type Product @key(fields: "id") {
id: ID!
price: Float! @external
isExpensive: Boolean! @requires(fields: "price")
hasDiscount: Boolean! @external
isExpensiveWithDiscount: Boolean! @requires(fields: "hasDiscount")
}
`
),
resolvers: {
Product: {
__resolveReference(key) {
if (!products.find((product2) => product2.id === key.id)) {
return null;
}
const product = {
__typename: "Product",
id: key.id
};
if (typeof key.price === "number") {
product.price = key.price;
product.isExpensive = key.price > 500;
} else if ("price" in key && typeof key.price !== "undefined") {
return new graphql.GraphQLError("Product.price must be a number");
}
if (typeof key.hasDiscount === "boolean") {
product.hasDiscount = key.hasDiscount;
product.isExpensiveWithDiscount = !key.hasDiscount;
} else if ("hasDiscount" in key && typeof key.hasDiscount !== "undefined") {
return new graphql.GraphQLError("Product.hasDiscount must be a number");
}
return product;
}
}
}
});
var d = testkit.createSubgraph("d", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external", "@requires"]
)
type Product @key(fields: "id") {
id: ID!
isExpensive: Boolean! @external
canAfford: Boolean! @requires(fields: "isExpensive")
isExpensiveWithDiscount: Boolean! @external
canAffordWithDiscount: Boolean!
@requires(fields: "isExpensiveWithDiscount")
}
`
),
resolvers: {
Product: {
__resolveReference(key) {
if (!products.find((product2) => product2.id === key.id)) {
return null;
}
const product = {
__typename: "Product",
id: key.id
};
if (typeof key.isExpensive === "boolean") {
product.isExpensive = key.isExpensive;
product.canAfford = !key.isExpensive;
} else if ("isExpensive" in key && typeof key.isExpensive !== "undefined") {
return new graphql.GraphQLError("Product.isExpensive must be a boolean");
}
if (typeof key.isExpensiveWithDiscount === "boolean") {
product.isExpensiveWithDiscount = key.isExpensiveWithDiscount;
product.canAffordWithDiscount = !key.isExpensiveWithDiscount;
} else if ("isExpensiveWithDiscount" in key && typeof key.isExpensiveWithDiscount !== "undefined") {
return new graphql.GraphQLError(
"Product.isExpensiveWithDiscount must be a boolean"
);
}
return product;
}
}
}
});
var test = [
testkit.createTest(
/* GraphQL */
`
query {
product {
canAfford
}
}
`,
{
data: {
product: {
canAfford: false
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
product {
isExpensive
}
}
`,
{
data: {
product: {
isExpensive: true
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
product {
isExpensive
canAfford
}
}
`,
{
data: {
product: {
isExpensive: true,
canAfford: false
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
product {
canAffordWithDiscount
}
}
`,
{
data: {
product: {
canAffordWithDiscount: true
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
product {
canAfford
canAffordWithDiscount
}
}
`,
{
data: {
product: {
canAfford: false,
canAffordWithDiscount: true
}
}
}
)
];
var index = testkit.serve("requires-requires", [a, b, c, d], test);
exports.default = index;