Skip to content

Commit 77265de

Browse files
committed
actual test
1 parent 43c3841 commit 77265de

File tree

4 files changed

+71
-175
lines changed

4 files changed

+71
-175
lines changed

src/test-suites/i-will-name-you-later/a.subgraph.ts

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createSubgraph } from "../../subgraph.js";
2-
import { comments, posts } from "./data.js";
2+
import { authors, posts } from "./data.js";
33

44
export default createSubgraph("a", {
55
typeDefs: /* GraphQL */ `
@@ -15,13 +15,14 @@ export default createSubgraph("a", {
1515
1616
type Post @key(fields: "id") {
1717
id: ID!
18-
isJohn: Boolean @external
19-
isNotJohn: Boolean @requires(fields: "isJohn")
18+
byNovice: Boolean! @external
19+
byExpert: Boolean! @requires(fields: "byNovice")
2020
}
2121
2222
type Author @key(fields: "id") {
2323
id: ID!
2424
name: String!
25+
yearsOfExperience: Int!
2526
}
2627
`,
2728
resolvers: {
@@ -31,49 +32,39 @@ export default createSubgraph("a", {
3132
},
3233
},
3334
Post: {
34-
__resolveReference(key: { id: string }) {
35-
const post = posts.find((post) => post.id === key.id);
36-
35+
__resolveReference(ref: { id: string; byNovice?: boolean }) {
36+
const post = posts.find((post) => post.id === ref.id);
3737
if (!post) {
3838
return null;
3939
}
40-
40+
if (ref.byNovice == null) {
41+
return {
42+
id: post.id,
43+
};
44+
}
4145
return {
4246
id: post.id,
47+
byNovice: ref.byNovice,
4348
};
4449
},
45-
comments(post: { id: string }, { limit }: { limit: number }) {
46-
return comments
47-
.filter((comment) => comment.postId === post.id)
48-
.slice(0, limit)
49-
.map((c) => ({
50-
id: c.id,
51-
authorId: c.authorId,
52-
body: c.body,
53-
}));
50+
byExpert(post: { byNovice: boolean }) {
51+
if (post.byNovice == null) {
52+
// ensuring requires is not skipped
53+
return null;
54+
}
55+
return !post.byNovice;
5456
},
5557
},
56-
Comment: {
57-
__resolveReference(ref: {
58-
id: string;
59-
sameCommentOnOtherPosts?: { comments: { id: string }[] }[];
60-
}) {
61-
const comment = comments.find((c) => c.id === ref.id);
62-
63-
if (!comment) {
58+
Author: {
59+
__resolveReference(ref: { id: string }) {
60+
const author = authors.find((author) => author.id === ref.id);
61+
if (!author) {
6462
return null;
6563
}
66-
6764
return {
68-
id: comment.id,
69-
authorId: comment.authorId,
70-
body: comment.body,
71-
isCommentSpam: ref.sameCommentOnOtherPosts?.length
72-
? ref.sameCommentOnOtherPosts?.every((p) =>
73-
// making sure that the ref contains the full "@requires"
74-
p.comments.every((c) => c.id)
75-
)
76-
: false,
65+
id: author.id,
66+
name: author.name,
67+
yearsOfExperience: author.yearsOfExperience,
7768
};
7869
},
7970
},

src/test-suites/i-will-name-you-later/b.subgraph.ts

Lines changed: 13 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createSubgraph } from "../../subgraph.js";
2-
import { authors, comments, posts } from "./data.js";
2+
import { posts } from "./data.js";
33

44
export default createSubgraph("b", {
55
typeDefs: /* GraphQL */ `
@@ -11,89 +11,35 @@ export default createSubgraph("b", {
1111
1212
type Post @key(fields: "id") {
1313
id: ID!
14-
author: Author
15-
isJohn: Boolean @requires(fields: "author { name }")
14+
author: Author!
15+
byNovice: Boolean! @requires(fields: "author { yearsOfExperience }")
1616
}
1717
1818
type Author @key(fields: "id") {
1919
id: ID!
20-
name: String! @external
20+
yearsOfExperience: Int! @external
2121
}
2222
`,
2323
resolvers: {
2424
Post: {
25-
__resolveReference(key: {
25+
__resolveReference(ref: {
2626
id: string;
27-
comments?: Array<{ authorId: string }>;
27+
author?: { yearsOfExperience: number };
2828
}) {
29-
const post = posts.find((p) => p.id === key.id);
30-
29+
const post = posts.find((post) => post.id === ref.id);
3130
if (!post) {
3231
return null;
3332
}
34-
35-
if (key.comments) {
36-
if (key.comments.length !== 3) {
37-
throw new Error("Expected 3 comments");
38-
}
39-
40-
return {
41-
id: post.id,
42-
authorId: key.comments?.[2].authorId,
43-
};
44-
}
45-
4633
return {
4734
id: post.id,
35+
author: {
36+
id: post.author.id,
37+
...ref.author,
38+
},
4839
};
4940
},
50-
author(post: { authorId?: string }) {
51-
if (!post.authorId) {
52-
return null;
53-
}
54-
55-
const author = authors.find((a) => a.id === post.authorId);
56-
57-
if (!author) {
58-
return null;
59-
}
60-
61-
return {
62-
id: author.id,
63-
name: author.name,
64-
};
65-
},
66-
comments(post: { id: string }, { limit }: { limit: number }) {
67-
const coms = comments
68-
.filter((comment) => comment.postId === post.id)
69-
.slice(0, limit)
70-
.map((c) => ({
71-
id: c.id,
72-
}));
73-
74-
return coms;
75-
},
76-
},
77-
Comment: {
78-
__resolveReference(key: { id: string }) {
79-
const comment = comments.find((c) => c.id === key.id);
80-
81-
if (!comment) {
82-
return null;
83-
}
84-
85-
return {
86-
id: comment.id,
87-
};
88-
},
89-
sameCommentOnOtherPosts({ id: commentId }: { id: string }) {
90-
const comment = comments.find((c) => c.id === commentId);
91-
if (!comment) {
92-
return [];
93-
}
94-
return comments
95-
.filter((c) => c.body === comment.body && c.postId !== comment.postId)
96-
.map((c) => ({ id: c.postId }));
41+
byNovice(post: { author: { yearsOfExperience: number } }) {
42+
return post.author.yearsOfExperience < 10;
9743
},
9844
},
9945
},
Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,25 @@
1-
export const products = [
1+
export const authors = [
22
{
3-
upc: "p1",
4-
name: "p-name-1",
5-
price: 11,
6-
weight: 1,
7-
category: {
8-
averagePrice: 11,
9-
},
3+
id: "a1",
4+
name: "John",
5+
yearsOfExperience: 5,
106
},
117
{
12-
upc: "p2",
13-
name: "p-name-2",
14-
price: 22,
15-
weight: 2,
16-
category: {
17-
averagePrice: 22,
18-
},
8+
id: "a2",
9+
name: "Jane",
10+
yearsOfExperience: 20,
1911
},
2012
];
2113

2214
export const posts = [
2315
{
2416
id: "p1",
2517
body: "p1-body",
18+
author: authors[0],
2619
},
2720
{
2821
id: "p2",
2922
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",
23+
author: authors[1],
8024
},
8125
];

src/test-suites/i-will-name-you-later/test.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,39 @@ export default [
55
/* GraphQL */ `
66
{
77
feed {
8-
author {
9-
id
10-
}
8+
byNovice
119
}
1210
}
1311
`,
1412
{
1513
data: {
1614
feed: [
1715
{
18-
author: {
19-
id: "a2",
20-
},
16+
byNovice: true,
2117
},
2218
{
23-
author: {
24-
id: "a1",
25-
},
19+
byNovice: false,
20+
},
21+
],
22+
},
23+
}
24+
),
25+
createTest(
26+
/* GraphQL */ `
27+
{
28+
feed {
29+
byExpert
30+
}
31+
}
32+
`,
33+
{
34+
data: {
35+
feed: [
36+
{
37+
byExpert: false,
38+
},
39+
{
40+
byExpert: true,
2641
},
2742
],
2843
},

0 commit comments

Comments
 (0)