UNPKG

@graphql-hive/federation-gateway-audit

Version:
461 lines (446 loc) 8.37 kB
'use strict'; var testkit = require('./testkit-CAf-AsDy.cjs'); require('@apollo/composition'); require('graphql'); require('fets'); require('@apollo/subgraph'); require('graphql-yoga'); const accounts = [ { __typename: "Account", id: "a1", username: "a1-username" } ]; const chats = [ { __typename: "Chat", id: "c1", accountId: "a1", text: "c1-text" } ]; var a = testkit.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 = testkit.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 = [ testkit.createTest( /* GraphQL */ ` query { node(id: "a1") { id } } `, { errors: true, data: null } ), testkit.createTest( /* GraphQL */ ` query { account: node(id: "a1") { __typename } chat: node(id: "c1") { __typename } } `, { data: { account: { __typename: "Account" }, chat: { __typename: "Chat" } } } ), testkit.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" } } } ), testkit.createTest( /* GraphQL */ ` query { account: node(id: "a1") { ... on Chat { id } } chat: node(id: "c1") { ... on Account { id } } } `, { data: { account: {}, chat: {} } } ), testkit.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" } } } ), testkit.createTest( /* GraphQL */ ` query { account: node(id: "a1") { id ... on Chat { id } } chat: node(id: "c1") { __typename ... on Account { id } } } `, { data: null, errors: true } ), testkit.createTest( /* GraphQL */ ` query { chat(id: "c1") { id } } `, { data: { chat: { id: "c1" } } } ), testkit.createTest( /* GraphQL */ ` query { account(id: "a1") { id } } `, { data: { account: { id: "a1" } } } ), testkit.createTest( /* GraphQL */ ` query { chat(id: "c1") { id text account { id } } } `, { data: { chat: { id: "c1", text: "c1-text", account: { id: "a1" } } } } ), testkit.createTest( /* GraphQL */ ` query { account(id: "a1") { id username chats { id } } } `, { data: { account: { id: "a1", username: "a1-username", chats: [ { id: "c1" } ] } } } ) ]; var index = testkit.serve("corrupted-supergraph-node-id", [a, b], test); exports.default = index;