/
PgConnectionArgOrderBy.js
140 lines (137 loc) · 4.01 KB
/
PgConnectionArgOrderBy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// @flow
import isString from "lodash/isString";
import type { Plugin } from "graphile-build";
export default (function PgConnectionArgOrderBy(builder) {
builder.hook("init", (_, build) => {
const {
newWithHooks,
pgIntrospectionResultsByKind: introspectionResultsByKind,
graphql: { GraphQLEnumType },
inflection,
pgOmit: omit,
} = build;
introspectionResultsByKind.class
.filter(table => table.isSelectable && !omit(table, "order"))
.filter(table => !!table.namespace)
.forEach(table => {
const tableTypeName = inflection.tableType(table);
/* const TableOrderByType = */
newWithHooks(
GraphQLEnumType,
{
name: inflection.orderByType(tableTypeName),
description: `Methods to use when ordering \`${tableTypeName}\`.`,
values: {
NATURAL: {
value: {
alias: null,
specs: [],
},
},
},
},
{
pgIntrospection: table,
isPgRowSortEnum: true,
}
);
});
return _;
});
builder.hook(
"GraphQLObjectType:fields:field:args",
(args, build, context) => {
const {
extend,
getTypeByName,
pgGetGqlTypeByTypeIdAndModifier,
pgSql: sql,
graphql: { GraphQLList, GraphQLNonNull },
inflection,
pgOmit: omit,
} = build;
const {
scope: {
isPgFieldConnection,
isPgFieldSimpleCollection,
pgFieldIntrospection: table,
},
addArgDataGenerator,
Self,
field,
} = context;
const shouldAddOrderBy = isPgFieldConnection || isPgFieldSimpleCollection;
if (
!shouldAddOrderBy ||
!table ||
table.kind !== "class" ||
!table.namespace ||
!table.isSelectable ||
omit(table, "order")
) {
return args;
}
const TableType = pgGetGqlTypeByTypeIdAndModifier(table.type.id, null);
const tableTypeName = TableType.name;
const TableOrderByType = getTypeByName(
inflection.orderByType(tableTypeName)
);
const cursorPrefixFromOrderBy = orderBy => {
if (orderBy) {
let cursorPrefixes = [];
for (const item of orderBy) {
if (item.alias) {
cursorPrefixes.push(sql.literal(item.alias));
}
}
if (cursorPrefixes.length > 0) {
return cursorPrefixes;
}
}
return null;
};
addArgDataGenerator(function connectionOrderBy({ orderBy: rawOrderBy }) {
const orderBy = rawOrderBy
? Array.isArray(rawOrderBy)
? rawOrderBy
: [rawOrderBy]
: null;
return {
pgCursorPrefix: cursorPrefixFromOrderBy(orderBy),
pgQuery: queryBuilder => {
if (orderBy != null) {
orderBy.forEach(item => {
const { specs, unique } = item;
const orders =
Array.isArray(specs[0]) || specs.length === 0
? specs
: [specs];
orders.forEach(([col, ascending]) => {
const expr = isString(col)
? sql.fragment`${queryBuilder.getTableAlias()}.${sql.identifier(
col
)}`
: col;
queryBuilder.orderBy(expr, ascending);
});
if (unique) {
queryBuilder.setOrderIsUnique();
}
});
}
},
};
});
return extend(
args,
{
orderBy: {
description: `The method to use when ordering \`${tableTypeName}\`.`,
type: new GraphQLList(new GraphQLNonNull(TableOrderByType)),
},
},
`Adding 'orderBy' to field '${field.name}' of '${Self.name}'`
);
}
);
}: Plugin);