Skip to content

Commit a5ecfbd

Browse files
committed
Release 0.13.38
1 parent e3da21b commit a5ecfbd

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pave",
33
"type": "module",
4-
"version": "0.13.37",
4+
"version": "0.13.38",
55
"author": "Casey Foster <c@sey.me>",
66
"license": "MIT",
77
"repository": {

src/get-query-cost.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@ export const getQueryCost = ({ context, path = [], query, schema, type }) => {
2727
else if (type.arrayOf) nextType = type.arrayOf;
2828
else if (type.oneOf) {
2929
cost += Math.max(
30-
...Object.entries(type.oneOf).map(([name, type]) => {
31-
const onField = `_on_${name}`;
32-
return getQueryCost({
33-
context,
34-
path: [...path, onField],
35-
query: query[onField] ?? {},
36-
schema,
37-
type
38-
});
39-
})
30+
...Object.entries(query).map(([alias, _query]) =>
31+
alias.startsWith('_on_')
32+
? getQueryCost({
33+
context,
34+
path: [...path, alias],
35+
query: _query,
36+
schema,
37+
type: type.oneOf[alias.slice(4)]
38+
})
39+
: 0
40+
)
4041
);
4142
} else if (type.object) {
4243
for (const alias in query) {
44+
if (alias === '$' || alias === '_') continue;
45+
4346
const _query = query[alias];
44-
const _type = type.object[_query._ ?? alias];
47+
const _type = type.object[_query._ ?? alias] ?? type.defaultType;
4548
cost += getQueryCost({
4649
context,
4750
path: [...path, alias],

src/get-query-cost.test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export default () => {
2020
return size * cost;
2121
},
2222
type: {
23-
object: { a: { cost: 1 }, b: { cost: 2 }, c: { cost: 3 } }
23+
object: { a: { cost: 1 }, b: { cost: 2 }, c: { cost: 3 } },
24+
defaultType: { cost: 4 }
2425
}
2526
},
2627
oneOf: {
@@ -43,7 +44,13 @@ export default () => {
4344
foo: {}, // 5
4445
bar: {}, // 10
4546
bar2: { _: 'bar' }, // 10
46-
baz: { $: { size: 10 }, a: {}, b: {}, c: {} }, // 60
47+
baz: {
48+
$: { size: 10 },
49+
a: {}, // 1
50+
b: {}, // 2
51+
c: {}, // 3
52+
d: {} // 4
53+
}, // 10 * 10 = 100
4754
oneOf: {
4855
_on_SuperExpensive: { doot: {} },
4956
_on_MediumExpensive: { ding: {} }
@@ -53,8 +60,8 @@ export default () => {
5360
_on_MediumExpensive: { ding: {} },
5461
_on_Cheap: { dong: {} }
5562
} // 50
56-
} // 235 * 3 = 705
63+
} // 275 * 3 = 825
5764
}),
58-
705
65+
825
5966
);
6067
};

0 commit comments

Comments
 (0)