Skip to content

Commit b54a04e

Browse files
committed
prepare coinflict fixes
1 parent 6bb230a commit b54a04e

File tree

2 files changed

+219
-211
lines changed

2 files changed

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

0 commit comments

Comments
 (0)