Skip to content

Commit 38d03c2

Browse files
committed
local tests
1 parent 36ff96a commit 38d03c2

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import { buildSubgraphSchema } from '@apollo/subgraph';
2+
import { normalizedExecutor } from '@graphql-tools/executor';
3+
import { parse, print } from 'graphql';
4+
import { expect, it } from 'vitest';
5+
import { getStitchedSchemaFromLocalSchemas } from './getStitchedSchemaFromLocalSchemas';
6+
7+
const authors = [
8+
{
9+
id: 'a1',
10+
name: 'John',
11+
yearsOfExperience: 5,
12+
},
13+
{
14+
id: 'a2',
15+
name: 'Jane',
16+
yearsOfExperience: 20,
17+
},
18+
];
19+
20+
const posts = [
21+
{
22+
id: 'p1',
23+
body: 'p1-body',
24+
author: authors[0],
25+
},
26+
{
27+
id: 'p2',
28+
body: 'p2-body',
29+
author: authors[1],
30+
},
31+
];
32+
33+
const supergraph = await getStitchedSchemaFromLocalSchemas({
34+
localSchemas: {
35+
a: buildSubgraphSchema([
36+
{
37+
typeDefs: parse(/* GraphQL */ `
38+
extend schema
39+
@link(
40+
url: "https://specs.apollo.dev/federation/v2.3"
41+
import: ["@key", "@external", "@requires"]
42+
)
43+
44+
type Query {
45+
feed: [Post]
46+
}
47+
48+
type Post @key(fields: "id") {
49+
id: ID!
50+
byNovice: Boolean! @external
51+
byExpert: Boolean! @requires(fields: "byNovice")
52+
}
53+
54+
type Author @key(fields: "id") {
55+
id: ID!
56+
name: String!
57+
yearsOfExperience: Int!
58+
}
59+
`),
60+
resolvers: {
61+
Query: {
62+
feed() {
63+
return posts.map((post) => ({ id: post.id }));
64+
},
65+
},
66+
Post: {
67+
__resolveReference(ref: { id: string; byNovice?: boolean }) {
68+
const post = posts.find((post) => post.id === ref.id);
69+
if (!post) {
70+
return null;
71+
}
72+
if (ref.byNovice == null) {
73+
return {
74+
id: post.id,
75+
};
76+
}
77+
return {
78+
id: post.id,
79+
byNovice: ref.byNovice,
80+
};
81+
},
82+
byExpert(post: { byNovice: boolean }) {
83+
if (post.byNovice == null) {
84+
// ensuring requires is not skipped
85+
return null;
86+
}
87+
return !post.byNovice;
88+
},
89+
},
90+
Author: {
91+
__resolveReference(ref: { id: string }) {
92+
const author = authors.find((author) => author.id === ref.id);
93+
if (!author) {
94+
return null;
95+
}
96+
return {
97+
id: author.id,
98+
name: author.name,
99+
yearsOfExperience: author.yearsOfExperience,
100+
};
101+
},
102+
},
103+
},
104+
},
105+
]),
106+
b: buildSubgraphSchema([
107+
{
108+
typeDefs: parse(/* GraphQL */ `
109+
extend schema
110+
@link(
111+
url: "https://specs.apollo.dev/federation/v2.3"
112+
import: ["@key", "@external", "@requires"]
113+
)
114+
115+
type Post @key(fields: "id") {
116+
id: ID!
117+
author: Author!
118+
byNovice: Boolean! @requires(fields: "author { yearsOfExperience }")
119+
}
120+
121+
type Author @key(fields: "id") {
122+
id: ID!
123+
yearsOfExperience: Int! @external
124+
}
125+
`),
126+
resolvers: {
127+
Post: {
128+
__resolveReference(ref: {
129+
id: string;
130+
author?: { yearsOfExperience: number };
131+
}) {
132+
const post = posts.find((post) => post.id === ref.id);
133+
if (!post) {
134+
return null;
135+
}
136+
return {
137+
id: post.id,
138+
author: {
139+
id: post.author!.id,
140+
...ref.author,
141+
},
142+
};
143+
},
144+
byNovice(post: { author: { yearsOfExperience: number } }) {
145+
return post.author.yearsOfExperience < 10;
146+
},
147+
},
148+
},
149+
},
150+
]),
151+
},
152+
onSubgraphExecute(subgraph, executionRequest, result) {
153+
const query = print(executionRequest.document);
154+
console.log(query);
155+
// if (subgraph === 'a' && query.includes('_entities')) {
156+
// // debugger;
157+
// }
158+
},
159+
});
160+
161+
it('test query 0', { timeout: 1000 }, async () => {
162+
await expect(
163+
normalizedExecutor({
164+
schema: supergraph,
165+
document: parse(/* GraphQL */ `
166+
{
167+
feed {
168+
byNovice
169+
}
170+
}
171+
`),
172+
}),
173+
).resolves.toEqual({
174+
data: {
175+
feed: [
176+
{
177+
byNovice: true,
178+
},
179+
{
180+
byNovice: false,
181+
},
182+
],
183+
},
184+
});
185+
});
186+
187+
it('test query 1', { timeout: 1000 }, async () => {
188+
await expect(
189+
normalizedExecutor({
190+
schema: supergraph,
191+
document: parse(/* GraphQL */ `
192+
{
193+
feed {
194+
byExpert
195+
}
196+
}
197+
`),
198+
}),
199+
).resolves.toEqual({
200+
data: {
201+
feed: [
202+
{
203+
byExpert: false,
204+
},
205+
{
206+
byExpert: true,
207+
},
208+
],
209+
},
210+
});
211+
});

0 commit comments

Comments
 (0)