Skip to content

Commit de2b1e9

Browse files
committed
begin test
1 parent 6848cc3 commit de2b1e9

File tree

6 files changed

+312
-0
lines changed

6 files changed

+312
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ async function getTestCases(router: ReturnType<typeof createRouter>) {
4545
import("./test-suites/requires-with-argument/index.js"),
4646
import("./test-suites/keys-mashup/index.js"),
4747
import("./test-suites/null-keys/index.js"),
48+
import("./test-suites/i-will-name-you-later/index.js"),
4849
].map((i) => i.then((e) => e.default))
4950
);
5051

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { createSubgraph } from "../../subgraph.js";
2+
import { comments, posts } from "./data.js";
3+
4+
export default createSubgraph("a", {
5+
typeDefs: /* GraphQL */ `
6+
extend schema
7+
@link(
8+
url: "https://specs.apollo.dev/federation/v2.3"
9+
import: ["@key", "@external", "@requires"]
10+
)
11+
12+
type Query {
13+
feed: [Post]
14+
}
15+
16+
type Post @key(fields: "id") {
17+
id: ID!
18+
comments(limit: Int!): [Comment] @external
19+
}
20+
21+
type Comment @key(fields: "id") {
22+
id: ID!
23+
authorId: ID
24+
body: String!
25+
sameCommentOnOtherPosts: [Post] @external
26+
isCommentSpam: Boolean
27+
@requires(
28+
fields: "sameCommentOnOtherPosts { comments(limit: 50) { id } }"
29+
)
30+
}
31+
`,
32+
resolvers: {
33+
Query: {
34+
feed() {
35+
return posts.map((post) => ({ id: post.id }));
36+
},
37+
},
38+
Post: {
39+
__resolveReference(key: { id: string }) {
40+
const post = posts.find((post) => post.id === key.id);
41+
42+
if (!post) {
43+
return null;
44+
}
45+
46+
return {
47+
id: post.id,
48+
};
49+
},
50+
comments(post: { id: string }, { limit }: { limit: number }) {
51+
return comments
52+
.filter((comment) => comment.postId === post.id)
53+
.slice(0, limit)
54+
.map((c) => ({
55+
id: c.id,
56+
authorId: c.authorId,
57+
body: c.body,
58+
}));
59+
},
60+
},
61+
Comment: {
62+
__resolveReference(ref: {
63+
id: string;
64+
sameCommentOnOtherPosts?: { comments: { id: string }[] }[];
65+
}) {
66+
const comment = comments.find((c) => c.id === ref.id);
67+
68+
if (!comment) {
69+
return null;
70+
}
71+
72+
return {
73+
id: comment.id,
74+
authorId: comment.authorId,
75+
body: comment.body,
76+
isCommentSpam: ref.sameCommentOnOtherPosts?.length
77+
? ref.sameCommentOnOtherPosts?.every((p) =>
78+
// making sure that the ref contains the full "@requires"
79+
p.comments.every((c) => c.id)
80+
)
81+
: false,
82+
};
83+
},
84+
},
85+
},
86+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { createSubgraph } from "../../subgraph.js";
2+
import { authors, comments, posts } from "./data.js";
3+
4+
export default createSubgraph("b", {
5+
typeDefs: /* GraphQL */ `
6+
extend schema
7+
@link(
8+
url: "https://specs.apollo.dev/federation/v2.3"
9+
import: ["@key", "@external", "@requires"]
10+
)
11+
12+
type Post @key(fields: "id") {
13+
id: ID!
14+
author: Author @requires(fields: "comments(limit: 3) { authorId }")
15+
comments(limit: Int!): [Comment]
16+
}
17+
18+
type Comment @key(fields: "id") {
19+
id: ID!
20+
date: String
21+
authorId: ID @external
22+
sameCommentOnOtherPosts: [Post]
23+
}
24+
25+
type Author {
26+
id: ID!
27+
name: String
28+
}
29+
`,
30+
resolvers: {
31+
Post: {
32+
__resolveReference(key: {
33+
id: string;
34+
comments?: Array<{ authorId: string }>;
35+
}) {
36+
const post = posts.find((p) => p.id === key.id);
37+
38+
if (!post) {
39+
return null;
40+
}
41+
42+
if (key.comments) {
43+
if (key.comments.length !== 3) {
44+
throw new Error("Expected 3 comments");
45+
}
46+
47+
return {
48+
id: post.id,
49+
authorId: key.comments?.[2].authorId,
50+
};
51+
}
52+
53+
return {
54+
id: post.id,
55+
};
56+
},
57+
author(post: { authorId?: string }) {
58+
if (!post.authorId) {
59+
return null;
60+
}
61+
62+
const author = authors.find((a) => a.id === post.authorId);
63+
64+
if (!author) {
65+
return null;
66+
}
67+
68+
return {
69+
id: author.id,
70+
name: author.name,
71+
};
72+
},
73+
comments(post: { id: string }, { limit }: { limit: number }) {
74+
const coms = comments
75+
.filter((comment) => comment.postId === post.id)
76+
.slice(0, limit)
77+
.map((c) => ({
78+
id: c.id,
79+
}));
80+
81+
return coms;
82+
},
83+
},
84+
Comment: {
85+
__resolveReference(key: { id: string }) {
86+
const comment = comments.find((c) => c.id === key.id);
87+
88+
if (!comment) {
89+
return null;
90+
}
91+
92+
return {
93+
id: comment.id,
94+
};
95+
},
96+
sameCommentOnOtherPosts({ id: commentId }: { id: string }) {
97+
const comment = comments.find((c) => c.id === commentId);
98+
if (!comment) {
99+
return [];
100+
}
101+
return comments
102+
.filter((c) => c.body === comment.body && c.postId !== comment.postId)
103+
.map((c) => ({ id: c.postId }));
104+
},
105+
},
106+
},
107+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export const products = [
2+
{
3+
upc: "p1",
4+
name: "p-name-1",
5+
price: 11,
6+
weight: 1,
7+
category: {
8+
averagePrice: 11,
9+
},
10+
},
11+
{
12+
upc: "p2",
13+
name: "p-name-2",
14+
price: 22,
15+
weight: 2,
16+
category: {
17+
averagePrice: 22,
18+
},
19+
},
20+
];
21+
22+
export const posts = [
23+
{
24+
id: "p1",
25+
body: "p1-body",
26+
},
27+
{
28+
id: "p2",
29+
body: "p2-body",
30+
},
31+
];
32+
33+
export const comments = [
34+
{
35+
id: "c1",
36+
postId: "p1",
37+
authorId: "a2",
38+
body: "c1-body",
39+
},
40+
{
41+
id: "c2",
42+
postId: "p1",
43+
authorId: "a2",
44+
body: "c2-body",
45+
},
46+
{
47+
id: "c3",
48+
postId: "p1",
49+
authorId: "a2",
50+
body: "c3-body",
51+
},
52+
{
53+
id: "c4",
54+
postId: "p2",
55+
authorId: "a1",
56+
body: "c4-body",
57+
},
58+
{
59+
id: "c5",
60+
postId: "p2",
61+
authorId: "a1",
62+
body: "c5-body",
63+
},
64+
{
65+
id: "c6",
66+
postId: "p2",
67+
authorId: "a1",
68+
body: "c6-body",
69+
},
70+
];
71+
72+
export const authors = [
73+
{
74+
id: "a1",
75+
name: "a1-name",
76+
},
77+
{
78+
id: "a2",
79+
name: "a2-name",
80+
},
81+
];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { serve } from "../../supergraph.js";
2+
import a from "./a.subgraph.js";
3+
import b from "./b.subgraph.js";
4+
import test from "./test.js";
5+
6+
export default serve("i-will-name-you-later", [a, b], test);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { createTest } from "../../testkit.js";
2+
3+
export default [
4+
createTest(
5+
/* GraphQL */ `
6+
{
7+
feed {
8+
author {
9+
id
10+
}
11+
}
12+
}
13+
`,
14+
{
15+
data: {
16+
feed: [
17+
{
18+
author: {
19+
id: "a2",
20+
},
21+
},
22+
{
23+
author: {
24+
id: "a1",
25+
},
26+
},
27+
],
28+
},
29+
}
30+
),
31+
];

0 commit comments

Comments
 (0)