Skip to content

Commit b95ed70

Browse files
davidmwhynotmacrozone
authored andcommitted
support SortOrderInput for typegraphql
1 parent 381f5eb commit b95ed70

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

packages/dataprovider/src/buildVariables/buildOrderBy.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IntrospectionInputObjectType } from "graphql";
1+
import { IntrospectionInputObjectType, IntrospectionInputValue } from "graphql";
22
import set from "lodash/set";
33
import type { GetListParams } from ".";
44
import { BuildVariablesContext } from "./types";
@@ -15,6 +15,28 @@ function getOrderType({
1515
) as IntrospectionInputObjectType;
1616
}
1717

18+
const getFieldType = (
19+
context: BuildVariablesContext,
20+
orderType: IntrospectionInputObjectType,
21+
fieldParts: string[],
22+
): IntrospectionInputValue | null => {
23+
if (fieldParts.length === 0) return null;
24+
25+
const [currentField, ...remainingFields] = fieldParts;
26+
const fieldType = orderType.inputFields.find((f) => f.name === currentField);
27+
28+
if (!fieldType) return null;
29+
if (remainingFields.length === 0) return fieldType;
30+
if (fieldType.type.kind !== "INPUT_OBJECT") return null;
31+
32+
const name = fieldType.type.name;
33+
const nextOrderType = context.introspectionResults.types.find(
34+
(t) => t.name === name,
35+
) as IntrospectionInputObjectType;
36+
37+
return getFieldType(context, nextOrderType, remainingFields);
38+
};
39+
1840
export const buildOrderBy = (
1941
sort: GetListParams["sort"],
2042
context: BuildVariablesContext,
@@ -33,6 +55,20 @@ export const buildOrderBy = (
3355
}
3456
const selector = {};
3557

58+
if (context.options.queryDialect === "typegraphql") {
59+
const fieldType = getFieldType(context, orderType, fieldParts);
60+
if (!fieldType) return null;
61+
62+
if (
63+
fieldType.type.kind === "INPUT_OBJECT" &&
64+
fieldType.type.name === "SortOrderInput"
65+
) {
66+
set(selector, field, { sort: sort.order === "ASC" ? "asc" : "desc" });
67+
68+
return [selector];
69+
}
70+
}
71+
3672
if (context.options.queryDialect === "pothos-prisma") {
3773
set(selector, field, sort.order === "ASC" ? "Asc" : "Desc");
3874
} else {

0 commit comments

Comments
 (0)