Skip to content

Commit 6307c1f

Browse files
committed
Test everything in both ends
1 parent 683c072 commit 6307c1f

File tree

1 file changed

+91
-120
lines changed

1 file changed

+91
-120
lines changed

packages/runtime/tests/demand-control.test.ts

Lines changed: 91 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('Demand Control', () => {
2929
demandControlConfig: Parameters<typeof useDemandControl>[0] = {
3030
includeExtensionMetadata: true,
3131
},
32+
introspectFromEndpoint = false,
3233
) {
3334
const subgraphServer = createYoga({
3435
schema: subgraph,
@@ -43,7 +44,7 @@ describe('Demand Control', () => {
4344
proxy: {
4445
endpoint: 'http://upstream/graphql',
4546
},
46-
schema: subgraph,
47+
schema: introspectFromEndpoint ? undefined : subgraph,
4748
plugins,
4849
})
4950
: createGatewayRuntime({
@@ -475,25 +476,14 @@ describe('Demand Control', () => {
475476
},
476477
},
477478
});
478-
await using booksServer = createYoga({
479-
schema: booksSubgraph,
480-
});
481-
await using gateway = createGatewayRuntime({
482-
supergraph: await composeLocalSchemasWithApollo([
483-
{
484-
name: 'books',
485-
schema: booksSubgraph,
486-
url: 'http://books/graphql',
487-
},
488-
]),
489-
plugins: () => [
490-
// @ts-expect-error TODO: MeshFetch is not compatible with @whatwg-node/server fetch
491-
useCustomFetch(booksServer.fetch),
492-
useDemandControl({
493-
maxCost: 3,
494-
}),
495-
],
496-
});
479+
await using gateway = createTestGateway(
480+
mode,
481+
booksSubgraph,
482+
{
483+
maxCost: 3,
484+
},
485+
true,
486+
);
497487
const query = /* GraphQL */ `
498488
query BookQuery {
499489
book(id: 1) {
@@ -518,10 +508,8 @@ describe('Demand Control', () => {
518508
body: JSON.stringify({ query }),
519509
});
520510
const result = await response.json();
521-
expect(result).toEqual({
522-
data: {
523-
book: null,
524-
},
511+
expect(result.data?.book).toBeFalsy();
512+
expect(result).toMatchObject({
525513
errors: [
526514
{
527515
message:
@@ -533,13 +521,6 @@ describe('Demand Control', () => {
533521
max: 3,
534522
},
535523
},
536-
locations: [
537-
{
538-
line: 3,
539-
column: 13,
540-
},
541-
],
542-
path: ['book'],
543524
},
544525
],
545526
});
@@ -590,25 +571,9 @@ describe('Demand Control', () => {
590571
},
591572
},
592573
});
593-
await using booksServer = createYoga({
594-
schema: booksSubgraph,
595-
});
596-
await using gateway = createGatewayRuntime({
597-
supergraph: await composeLocalSchemasWithApollo([
598-
{
599-
name: 'books',
600-
schema: booksSubgraph,
601-
url: 'http://books/graphql',
602-
},
603-
]),
604-
plugins: () => [
605-
// @ts-expect-error TODO: MeshFetch is not compatible with @whatwg-node/server fetch
606-
useCustomFetch(booksServer.fetch),
607-
useDemandControl({
608-
includeExtensionMetadata: true,
609-
listSize: 5,
610-
}),
611-
],
574+
await using gateway = createTestGateway(mode, booksSubgraph, {
575+
includeExtensionMetadata: true,
576+
listSize: 5,
612577
});
613578
const query = /* GraphQL */ `
614579
query BestsellersQuery {
@@ -897,86 +862,92 @@ describe('Demand Control', () => {
897862
* 1 Query (0) + 1 book object (1) + 1 author object (1) + 1 publisher object (1) + 1 address object (5) = 8 total cost
898863
*/
899864
// Skipped in proxy mode because the proxy does not support custom directive aliasing (e.g., @cost as @myCost).
900-
it.skipIf(mode === 'proxy')('@cost in object but aliased as @myCost', async () => {
901-
const booksSubgraph = buildSubgraphSchema({
902-
typeDefs: parse(/* GraphQL */ `
903-
type Query {
904-
book(id: ID): Book
905-
}
865+
it.skipIf(mode === 'proxy')(
866+
'@cost in object but aliased as @myCost',
867+
async () => {
868+
const booksSubgraph = buildSubgraphSchema({
869+
typeDefs: parse(/* GraphQL */ `
870+
type Query {
871+
book(id: ID): Book
872+
}
906873
907-
type Book {
908-
title: String
909-
author: Author
910-
publisher: Publisher
911-
}
874+
type Book {
875+
title: String
876+
author: Author
877+
publisher: Publisher
878+
}
912879
913-
type Author {
914-
name: String
915-
}
880+
type Author {
881+
name: String
882+
}
916883
917-
type Publisher {
918-
name: String
919-
address: Address
920-
}
884+
type Publisher {
885+
name: String
886+
address: Address
887+
}
921888
922-
type Address @myCost(weight: 5) {
923-
zipCode: Int!
924-
}
925-
extend schema
926-
@link(
927-
url: "https://specs.apollo.dev/federation/v2.9"
928-
import: [{ name: "@cost", as: "@myCost" }]
929-
) {
930-
query: Query
931-
}
932-
`),
933-
resolvers: {
934-
Query: {
935-
book: (_root, { id }) => {
936-
if (id === '1') {
937-
return book;
938-
}
939-
throw new Error('Book not found');
889+
type Address @myCost(weight: 5) {
890+
zipCode: Int!
891+
}
892+
extend schema
893+
@link(
894+
url: "https://specs.apollo.dev/federation/v2.9"
895+
import: [{ name: "@cost", as: "@myCost" }]
896+
) {
897+
query: Query
898+
}
899+
`),
900+
resolvers: {
901+
Query: {
902+
book: (_root, { id }) => {
903+
if (id === '1') {
904+
return book;
905+
}
906+
throw new Error('Book not found');
907+
},
940908
},
941909
},
942-
},
943-
});
944-
await using gateway = createTestGateway(mode, booksSubgraph);
945-
const query = /* GraphQL */ `
946-
query BookQuery {
947-
book(id: 1) {
948-
title
949-
author {
950-
name
951-
}
952-
publisher {
953-
name
954-
address {
955-
zipCode
910+
});
911+
await using gateway = createTestGateway(mode, booksSubgraph);
912+
const query = /* GraphQL */ `
913+
query BookQuery {
914+
book(id: 1) {
915+
title
916+
author {
917+
name
918+
}
919+
publisher {
920+
name
921+
address {
922+
zipCode
923+
}
956924
}
957925
}
958926
}
959-
}
960-
`;
961-
const response = await gateway.fetch('http://localhost:4000/graphql', {
962-
method: 'POST',
963-
headers: {
964-
'Content-Type': 'application/json',
965-
},
966-
body: JSON.stringify({ query }),
967-
});
968-
const result = await response.json();
969-
expect(result).toEqual({
970-
data: {
971-
book,
972-
},
973-
extensions: {
974-
cost: {
975-
estimated: 8,
927+
`;
928+
const response = await gateway.fetch(
929+
'http://localhost:4000/graphql',
930+
{
931+
method: 'POST',
932+
headers: {
933+
'Content-Type': 'application/json',
934+
},
935+
body: JSON.stringify({ query }),
976936
},
977-
},
978-
});
979-
});
937+
);
938+
const result = await response.json();
939+
expect(result).toEqual({
940+
data: {
941+
book,
942+
},
943+
extensions: {
944+
cost: {
945+
estimated: 8,
946+
},
947+
},
948+
});
949+
},
950+
);
980951

981952
it('returns cost even if it does not hit the subgraph', async () => {
982953
const subgraph = buildSubgraphSchema({

0 commit comments

Comments
 (0)