@graphql-hive/federation-gateway-audit
Version:
Audit tool for Apollo Federation Gateway
459 lines (445 loc) • 8.28 kB
JavaScript
import { c as createSubgraph, a as createTest, s as serve } from './testkit-BtZjdl4n.js';
import '@apollo/composition';
import 'graphql';
import 'fets';
import '@apollo/subgraph';
import 'graphql-yoga';
const accounts = [
{
__typename: "Account",
id: "a1",
username: "a1-username"
}
];
const chats = [
{
__typename: "Chat",
id: "c1",
accountId: "a1",
text: "c1-text"
}
];
var a = createSubgraph("a", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable", "@external"]
)
type Query {
node(id: ID!): Node @shareable
account(id: String!): Account
}
interface Node {
id: ID!
}
type Account implements Node @key(fields: "id") {
id: ID!
username: String!
}
type Chat implements Node @key(fields: "id") {
id: ID! @external
account: Account!
}
`
),
resolvers: {
Query: {
node(_, { id }) {
const account = accounts.find((a) => a.id === id);
if (account) {
return {
__typename: "Account",
id: account.id,
username: account.username
};
}
const chat = chats.find((c) => c.id === id);
if (chat) {
return {
__typename: "Chat",
id: "never",
accountId: chat.accountId
};
}
},
account(_, { id }) {
const account = accounts.find((a) => a.id === id);
if (account) {
return {
__typename: "Account",
id: account.id,
username: account.username
};
}
return null;
}
},
Account: {
__resolveReference(key) {
const account = accounts.find((a) => a.id === key.id);
if (account) {
return {
__typename: "Account",
id: account.id,
username: account.username
};
}
return null;
}
},
Chat: {
__resolveReference(key) {
const chat = chats.find((c) => c.id === key.id);
if (chat) {
return {
__typename: "Chat",
id: chat.id,
accountId: chat.accountId
};
}
return null;
},
account(chat) {
const account = accounts.find((a) => a.id === chat.accountId);
if (account) {
return {
__typename: "Account",
id: account.id,
username: account.username
};
}
return null;
}
}
}
});
var b = createSubgraph("b", {
typeDefs: (
/* GraphQL */
`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable", "@external"]
)
type Query {
node(id: ID!): Node @shareable
chat(id: String!): Chat
}
interface Node {
id: ID!
}
type Account implements Node @key(fields: "id") {
id: ID! @external
chats: [Chat!]!
}
type Chat implements Node @key(fields: "id") {
id: ID!
text: String!
}
`
),
resolvers: {
Query: {
node(_, { id }) {
const account = accounts.find((a) => a.id === id);
if (account) {
return {
__typename: "Account",
id: "never",
chatIds: chats.filter((c) => c.accountId === account.id).map((c) => c.id)
};
}
const chat = chats.find((c) => c.id === id);
if (chat) {
return {
__typename: "Chat",
id: chat.id,
text: chat.text
};
}
return null;
},
chat(_, { id }) {
const chat = chats.find((c) => c.id === id);
if (chat) {
return {
__typename: "Chat",
id: chat.id,
text: chat.text
};
}
return null;
}
},
Account: {
__resolveReference(key) {
const account = accounts.find((a) => a.id === key.id);
if (account) {
return {
__typename: "Account",
id: account.id,
chatIds: chats.filter((c) => c.accountId === account.id).map((c) => c.id)
};
}
return null;
},
chats(account) {
return chats.filter((c) => account.chatIds.includes(c.id)).map((chat) => ({
__typename: "Chat",
id: chat.id,
text: chat.text
}));
}
},
Chat: {
__resolveReference(key) {
const chat = chats.find((c) => c.id === key.id);
if (chat) {
return {
__typename: "Chat",
id: chat.id,
text: chat.text
};
}
return null;
}
}
}
});
var test = [
createTest(
/* GraphQL */
`
query {
node(id: "a1") {
id
}
}
`,
{
errors: true,
data: null
}
),
createTest(
/* GraphQL */
`
query {
account: node(id: "a1") {
__typename
}
chat: node(id: "c1") {
__typename
}
}
`,
{
data: {
account: {
__typename: "Account"
},
chat: {
__typename: "Chat"
}
}
}
),
createTest(
/* GraphQL */
`
query {
account: node(id: "a1") {
... on Account {
id
username
}
}
chat: node(id: "c1") {
... on Chat {
id
text
}
}
}
`,
{
data: {
account: {
id: "a1",
username: "a1-username"
},
chat: {
id: "c1",
text: "c1-text"
}
}
}
),
createTest(
/* GraphQL */
`
query {
account: node(id: "a1") {
... on Chat {
id
}
}
chat: node(id: "c1") {
... on Account {
id
}
}
}
`,
{
data: {
account: {},
chat: {}
}
}
),
createTest(
/* GraphQL */
`
query {
account: node(id: "a1") {
__typename
... on Chat {
id
}
}
chat: node(id: "c1") {
__typename
... on Account {
id
}
}
}
`,
{
data: {
account: {
__typename: "Account"
},
chat: {
__typename: "Chat"
}
}
}
),
createTest(
/* GraphQL */
`
query {
account: node(id: "a1") {
id
... on Chat {
id
}
}
chat: node(id: "c1") {
__typename
... on Account {
id
}
}
}
`,
{
data: null,
errors: true
}
),
createTest(
/* GraphQL */
`
query {
chat(id: "c1") {
id
}
}
`,
{
data: {
chat: {
id: "c1"
}
}
}
),
createTest(
/* GraphQL */
`
query {
account(id: "a1") {
id
}
}
`,
{
data: {
account: {
id: "a1"
}
}
}
),
createTest(
/* GraphQL */
`
query {
chat(id: "c1") {
id
text
account {
id
}
}
}
`,
{
data: {
chat: {
id: "c1",
text: "c1-text",
account: {
id: "a1"
}
}
}
}
),
createTest(
/* GraphQL */
`
query {
account(id: "a1") {
id
username
chats {
id
}
}
}
`,
{
data: {
account: {
id: "a1",
username: "a1-username",
chats: [
{
id: "c1"
}
]
}
}
}
)
];
var index = serve("corrupted-supergraph-node-id", [a, b], test);
export { index as default };