@graphql-hive/federation-gateway-audit
Version:
Audit tool for Apollo Federation Gateway
281 lines (267 loc) • 5.98 kB
JavaScript
import { c as createSubgraph, a as createTest, s as serve } from './testkit-BtZjdl4n.js';
import { GraphQLError } from 'graphql';
import '@apollo/composition';
import 'fets';
import '@apollo/subgraph';
import 'graphql-yoga';
const products = [
{
id: "p1",
price: 699.99,
hasDiscount: true
}
];
var a = createSubgraph("a", {
typeDefs: (
/* GraphQL */
`
extend schema
type Product {
id: ID!
price: Float!
}
`
),
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 = createSubgraph("b", {
typeDefs: (
/* GraphQL */
`
extend schema
type Query {
product: Product
}
type Product {
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 = createSubgraph("c", {
typeDefs: (
/* GraphQL */
`
extend schema
type Product {
id: ID!
price: Float!
isExpensive: Boolean!
hasDiscount: Boolean!
isExpensiveWithDiscount: Boolean!
}
`
),
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 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 GraphQLError("Product.hasDiscount must be a number");
}
return product;
}
}
}
});
var d = createSubgraph("d", {
typeDefs: (
/* GraphQL */
`
extend schema
type Product {
id: ID!
isExpensive: Boolean!
canAfford: Boolean!
isExpensiveWithDiscount: Boolean!
canAffordWithDiscount: Boolean!
}
`
),
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 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 GraphQLError(
"Product.isExpensiveWithDiscount must be a boolean"
);
}
return product;
}
}
}
});
var test = [
createTest(
/* GraphQL */
`
query {
product {
canAfford
}
}
`,
{
data: {
product: {
canAfford: false
}
}
}
),
createTest(
/* GraphQL */
`
query {
product {
isExpensive
}
}
`,
{
data: {
product: {
isExpensive: true
}
}
}
),
createTest(
/* GraphQL */
`
query {
product {
isExpensive
canAfford
}
}
`,
{
data: {
product: {
isExpensive: true,
canAfford: false
}
}
}
),
createTest(
/* GraphQL */
`
query {
product {
canAffordWithDiscount
}
}
`,
{
data: {
product: {
canAffordWithDiscount: true
}
}
}
),
createTest(
/* GraphQL */
`
query {
product {
canAfford
canAffordWithDiscount
}
}
`,
{
data: {
product: {
canAfford: false,
canAffordWithDiscount: true
}
}
}
)
];
var index = serve("requires-requires", [a, b, c, d], test);
export { index as default };