Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/PostgraphileNestedConnectorsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ module.exports = function PostGraphileNestedConnectorsPlugin(builder) {

return extend(build, {
pgNestedTableConnectorFields: {},
pgNestedTableConnect: async ({
nestedField,
connectorField,
input,
pgClient,
parentRow,
}) => {
pgNestedTableConnect: async (options) => {
const {
nestedField,
connectorField,
input,
pgClient,
parentRow
} = options;
const { foreignTable, keys, foreignKeys } = nestedField;
const { isNodeIdConnector, constraint } = connectorField;

Expand Down Expand Up @@ -104,12 +105,13 @@ module.exports = function PostGraphileNestedConnectorsPlugin(builder) {
)})`;
}
const select = foreignKeys.map((k) => sql.identifier(k.name));
const foreignTableIdentifier = sql.identifier(
foreignTable.namespace.name,
foreignTable.name,
)
const query = parentRow
? sql.query`
update ${sql.identifier(
foreignTable.namespace.name,
foreignTable.name,
)}
update ${foreignTableIdentifier}
set ${sql.join(
keys.map(
(k, i) =>
Expand All @@ -123,14 +125,16 @@ module.exports = function PostGraphileNestedConnectorsPlugin(builder) {
returning *`
: sql.query`
select ${sql.join(select, ', ')}
from ${sql.identifier(
foreignTable.namespace.name,
foreignTable.name,
)}
from ${foreignTableIdentifier}
where ${where}`;

const { text, values } = sql.compile(query);
const { rows } = await pgClient.query(text, values);
const errorMessage = 'parentRow' in options
? `Unable to ${parentRow ? 'update' : 'select'} parent row from `+
`${foreignTable.namespace.name}.${foreignTable.name}.`
: 'invalid connect keys';
if (!rows.length) throw new Error(errorMessage);
return rows[0];
},
});
Expand Down
8 changes: 0 additions & 8 deletions src/PostgraphileNestedMutationsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@ module.exports = function PostGraphileNestedMutationPlugin(builder) {
pgClient,
});

if (!row) {
throw new Error('invalid connect keys');
}

foreignKeys.forEach((k, idx) => {
output[inflection.column(keys[idx])] = row[k.name];
Expand Down Expand Up @@ -479,11 +476,6 @@ module.exports = function PostGraphileNestedMutationPlugin(builder) {
});

if (primaryKeys) {
if (!connectedRow) {
throw new Error(
'Unable to update/select parent row.',
);
}
const rowKeyValues = {};
primaryKeys.forEach((col) => {
rowKeyValues[col.name] = connectedRow[col.name];
Expand Down