@graphql-hive/federation-gateway-audit
Version:
Audit tool for Apollo Federation Gateway
684 lines (666 loc) • 12.3 kB
JavaScript
'use strict';
var testkit = require('./testkit-CAf-AsDy.cjs');
require('@apollo/composition');
require('graphql');
require('fets');
require('@apollo/subgraph');
require('graphql-yoga');
const users = [
{
id: "u1",
username: "u-username-1",
name: "u-name-1"
},
{
id: "u2",
username: "u-username-2",
name: "u-name-2"
}
];
const products$1 = [
{
upc: "p1",
name: "p-name-1",
price: 11,
weight: 1
},
{
upc: "p2",
name: "p-name-2",
price: 22,
weight: 2
}
];
const inStock = [products$1[0].upc];
const reviews$1 = [
{
id: "r1",
body: "r-body-1",
authorId: users[0].id,
productUpc: products$1[0].upc
},
{
id: "r2",
body: "r-body-2",
authorId: users[0].id,
productUpc: products$1[1].upc
}
];
var accounts = testkit.createSubgraph("accounts", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable"]
)
type Query {
me: User
}
type User @key(fields: "id") {
id: ID!
name: String
username: String @shareable
}
`
),
resolvers: {
Query: {
me() {
return {
id: users[0].id,
name: users[0].name,
username: users[0].username
};
}
},
User: {
__resolveReference(key) {
const user = users.find((u) => u.id === key.id);
if (!user) {
return null;
}
return {
id: user.id,
name: user.name,
username: user.username
};
}
}
}
});
var inventory = testkit.createSubgraph("inventory", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external", "@requires"]
)
type Product @key(fields: "upc") {
upc: String!
weight: Int @external
price: Int @external
inStock: Boolean
shippingEstimate: Int @requires(fields: "price weight")
shippingEstimateTag: String @requires(fields: "price weight")
}
`
),
resolvers: {
Product: {
__resolveReference(key) {
const product = products$1.find((p) => p.upc === key.upc);
if (!product) {
return null;
}
if ("weight" in key && "price" in key) {
return {
upc: product.upc,
weight: key.weight,
price: key.price
};
}
return {
upc: product.upc
};
},
shippingEstimate(product) {
return product.price * product.weight * 10;
},
shippingEstimateTag(product) {
return `#${product.upc}#${product.price * product.weight * 10}#`;
},
inStock(product) {
return inStock.includes(product.upc);
}
}
}
});
var products = testkit.createSubgraph("products", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])
type Query {
products: [Product]
}
type Product @key(fields: "upc") {
upc: String!
name: String
price: Int
weight: Int
}
`
),
resolvers: {
Query: {
products() {
return products$1.map((p) => ({
upc: p.upc,
name: p.name,
price: p.price,
weight: p.weight
}));
}
},
Product: {
__resolveReference(key) {
const product = products$1.find((p) => p.upc === key.upc);
if (!product) {
return null;
}
return {
upc: product.upc,
name: product.name,
price: product.price,
weight: product.weight
};
}
}
}
});
var reviews = testkit.createSubgraph("reviews", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external", "@provides"]
)
type Review @key(fields: "id") {
id: ID!
body: String
author: User @provides(fields: "username")
product: Product
}
type User @key(fields: "id") {
id: ID!
username: String @external
reviews: [Review]
}
type Product @key(fields: "upc") {
upc: String!
reviews: [Review]
}
`
),
resolvers: {
Review: {
__resolveReference(key) {
const review = reviews$1.find((r) => r.id === key.id);
if (!review) {
return null;
}
return {
id: review.id,
body: review.body,
authorId: review.authorId,
productUpc: review.productUpc
};
},
author(review) {
const user = users.find((u) => u.id === review.authorId);
if (!user) {
return null;
}
return {
id: user.id,
username: user.username
};
},
product(review) {
const product = products$1.find((p) => p.upc === review.productUpc);
if (!product) {
return null;
}
return {
upc: product.upc,
reviews: reviews$1.filter((r) => r.productUpc === product.upc).map((r) => ({
id: r.id,
body: r.body,
authorId: r.authorId,
productUpc: r.productUpc
}))
};
}
},
User: {
__resolveReference(key) {
const user = users.find((u) => u.id === key.id);
if (!user) {
return null;
}
return {
id: user.id,
username: user.username
};
},
reviews(user) {
return reviews$1.filter((r) => r.authorId === user.id);
}
},
Product: {
__resolveReference(key) {
return {
upc: key.upc
};
},
reviews(product) {
return reviews$1.filter((r) => r.productUpc === product.upc);
}
}
}
});
var test = [
testkit.createTest(
/* GraphQL */
`
query {
me {
id
}
}
`,
{
data: {
me: {
id: "u1"
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
me {
id
reviews {
id
}
}
}
`,
{
data: {
me: {
id: "u1",
reviews: [
{
id: "r1"
},
{
id: "r2"
}
]
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
me {
reviews {
id
author {
id
username
}
product {
inStock
}
}
}
}
`,
{
data: {
me: {
reviews: [
{
id: "r1",
author: {
id: "u1",
username: "u-username-1"
},
product: {
inStock: true
}
},
{
id: "r2",
author: {
id: "u1",
username: "u-username-1"
},
product: {
inStock: false
}
}
]
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
products {
name
}
}
`,
{
data: {
products: [
{
name: "p-name-1"
},
{
name: "p-name-2"
}
]
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
products {
price
}
}
`,
{
data: {
products: [
{
price: 11
},
{
price: 22
}
]
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
products {
shippingEstimate
}
}
`,
{
data: {
products: [
{
shippingEstimate: 110
},
{
shippingEstimate: 440
}
]
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
products {
shippingEstimate
weight
price
}
}
`,
{
data: {
products: [
{
shippingEstimate: 110,
weight: 1,
price: 11
},
{
shippingEstimate: 440,
weight: 2,
price: 22
}
]
}
}
),
testkit.createTest(
/* GraphQL */
`
{
products {
reviews {
id
author {
username
}
product {
name
shippingEstimate
}
}
}
}
`,
{
data: {
products: [
{
reviews: [
{
id: "r1",
author: {
username: "u-username-1"
},
product: {
name: "p-name-1",
shippingEstimate: 110
}
}
]
},
{
reviews: [
{
id: "r2",
author: {
username: "u-username-1"
},
product: {
name: "p-name-2",
shippingEstimate: 440
}
}
]
}
]
}
}
),
testkit.createTest(
/* GraphQL */
`
{
me {
reviews {
product {
reviews {
id
}
}
}
}
}
`,
{
data: {
me: {
reviews: [
{
product: {
reviews: [
{
id: "r1"
}
]
}
},
{
product: {
reviews: [
{
id: "r2"
}
]
}
}
]
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
me {
reviews {
product {
inStock
}
}
}
}
`,
{
data: {
me: {
reviews: [
{
product: {
inStock: true
}
},
{
product: {
inStock: false
}
}
]
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
me {
reviews {
product {
shippingEstimate
}
}
}
}
`,
{
data: {
me: {
reviews: [
{
product: {
shippingEstimate: 110
}
},
{
product: {
shippingEstimate: 440
}
}
]
}
}
}
),
testkit.createTest(
/* GraphQL */
`
query {
me {
reviews {
product {
shippingEstimate
shippingEstimateTag
}
}
}
}
`,
{
data: {
me: {
reviews: [
{
product: {
shippingEstimate: 110,
shippingEstimateTag: "#p1#110#"
}
},
{
product: {
shippingEstimate: 440,
shippingEstimateTag: "#p2#440#"
}
}
]
}
}
}
)
];
var index = testkit.serve(
"simple-requires-provides",
[accounts, inventory, products, reviews],
test
);
exports.default = index;