-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
166 lines (155 loc) · 5.33 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module.exports = function PgOrderByMultiColumnIndexPlugin(
builder,
{ orderByNullsLast }
) {
builder.hook("build", build => {
const pkg = require("./package.json");
// Check dependencies
if (!build.versions) {
throw new Error(
`Plugin ${pkg.name}@${pkg.version} requires graphile-build@^4.1.0 in order to check dependencies (current version: ${build.graphileBuildVersion})`
);
}
const depends = (name, range) => {
if (!build.hasVersion(name, range)) {
throw new Error(
`Plugin ${pkg.name}@${pkg.version} requires ${name}@${range} (${
build.versions[name]
? `current version: ${build.versions[name]}`
: "not found"
})`
);
}
};
depends("graphile-build-pg", "^4.1.0");
// Register this plugin
build.versions = build.extend(build.versions, { [pkg.name]: pkg.version });
return build;
});
builder.hook("inflection", inflection => {
return Object.assign(inflection, {
orderByMultiColumnIndexEnum(specs) {
const nullOrderingSuffix = (ascending, nullsFirst) => {
// Only include a null ordering suffix if it differs
// from the application default (specified using
// `graphileBuildOptions.orderByNullsLast`).
if (orderByNullsLast === true) {
// Defaults are ASC NULLS LAST and DESC NULLS LAST, so
// ASC NULLS FIRST and DESC NULLS FIRST need a suffix:
if (ascending === true && nullsFirst === true) {
return "-nulls-first";
} else if (ascending === false && nullsFirst === true) {
return "-nulls-first";
} else {
return "";
}
} else {
// Defaults are ASC NULLS LAST and DESC NULLS FIRST, so
// ASC NULLS FIRST and DESC NULLS LAST need a suffix:
if (ascending === true && nullsFirst === true) {
return "-nulls-first";
} else if (ascending === false && nullsFirst === false) {
return "-nulls-last";
} else {
return "";
}
}
};
return `${specs
.map(([attr, ascending, nullsFirst]) =>
this.constantCase(
`${this.orderByColumnEnum(attr, ascending)}${nullOrderingSuffix(
ascending,
nullsFirst
)}`
)
)
.join("__")}`;
},
});
});
builder.hook("GraphQLEnumType:values", (values, build, context) => {
const {
extend,
inflection,
pgIntrospectionResultsByKind: introspectionResultsByKind,
describePgEntity,
} = build;
const {
scope: { isPgRowSortEnum, pgIntrospection: table },
} = context;
if (!isPgRowSortEnum || !table || table.kind !== "class") {
return values;
}
return extend(
values,
introspectionResultsByKind.index
.filter(index => index.class.id === table.id)
.reduce((memo, index) => {
const attributes = index.attributeNums.map(nr =>
index.class.attributes.find(attr => attr.num === nr)
);
if (attributes.length <= 1 || attributes.includes(undefined)) {
// Not a multi-column index
return memo;
}
// Specs for scanning the index forward
const forwardSpecs = attributes.map((attr, idx) => [
attr,
index.attributePropertiesAsc[idx],
index.attributePropertiesNullsFirst[idx],
]);
// Specs for scanning the index backward (flip asc/desc and nulls first/last)
const backwardSpecs = attributes.map((attr, idx) => [
attr,
!index.attributePropertiesAsc[idx],
!index.attributePropertiesNullsFirst[idx],
]);
const forwardEnumName = inflection.orderByMultiColumnIndexEnum(
forwardSpecs
);
const backwardEnumName = inflection.orderByMultiColumnIndexEnum(
backwardSpecs
);
memo = extend(
memo,
{
[forwardEnumName]: {
value: {
alias: forwardEnumName.toLowerCase(),
specs: forwardSpecs.map(([attr, ascending, nullsFirst]) => [
attr.name,
ascending,
nullsFirst,
]),
},
},
},
`Adding multi-column index forward orderBy enum value for ${attributes
.map(attr => describePgEntity(attr))
.join(", ")}.`
);
memo = extend(
memo,
{
[backwardEnumName]: {
value: {
alias: backwardEnumName.toLowerCase(),
specs: backwardSpecs.map(([attr, ascending, nullsFirst]) => [
attr.name,
ascending,
nullsFirst,
]),
},
},
},
`Adding multi-column index backward orderBy enum value for ${attributes
.map(attr => describePgEntity(attr))
.join(", ")}.`
);
return memo;
}, {}),
`Adding multi-column index order values for table '${table.name}'`
);
});
};