BowlingX/ra-postgraphile

Delete by nodeId doesn't work

K-Markopoulos opened this issue ยท 6 comments

I have a relation table that doesn't have an id column and I'm trying to delete a record by nodeId with custom mutation. I get the following error:

Variable "$input" of type "Delete[RELATION]Input!" used in position expecting type "Delete[RELATION]ByNodeIdInput!"

Seems like that Delete${resourceTypename}Input doesn't match with the input of ${deleteResourceName} as the latter includes the ByNodeId. Also, the variables object for this mutation (input: { id }) is incompatible with DeleteByNodeId which expects input { nodeId }.

Code of interest:

case DELETE: {
return {
variables: {
input: {
id: mapType(primaryKeyType, (params as DeleteParams).id),
},
},
query: gql`
mutation ${deleteResourceName}($input: Delete${resourceTypename}Input!) {
${deleteResourceName}(input: $input) {

Is this a bug or is there another way to delete a record when the table doesn't have id column? I tried some changes. but I wanted to make sure I'm not doing something wrong before I open a PR.

The following changes resolve the issue, but I'm not sure how to test them for other cases.

diff --git a/src/buildQuery.ts b/src/buildQuery.ts
index b21081d..9eddc24 100644
--- a/src/buildQuery.ts
+++ b/src/buildQuery.ts
@@ -243,12 +243,15 @@ export const buildQuery = (introspectionResults: IntrospectionResult, factory: F
     case DELETE: {
       return {
         variables: {
-          input: {
-            id: mapType(primaryKeyType, (params as DeleteParams).id),
-          },
+          input: mapInputToVariables(
+            params,
+            typeMap[`${capitalize(deleteResourceName)}Input`],
+            type,
+            typeMapConfiguration
+          ),
         },
         query: gql`
-          mutation ${deleteResourceName}($input: Delete${resourceTypename}Input!) {
+          mutation ${deleteResourceName}($input: ${capitalize(deleteResourceName)}Input!) {
             ${deleteResourceName}(input: $input) {
             ${singleLowerResourceName} {
             ${createQueryFromType(

Hi :) sorry for the late reply. This is indeed a bug; It actually affects more queries. I will Push a fix and add tests for this case. Thank you!

๐ŸŽ‰ This issue has been resolved in version 5.0.0 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€

I reworked the non-id primary key logic and removed nodeId from the current implementation; Instead I alias the pimary key name to create a consistent behaviour for all queries. I released it under the next tag in npm; Please try it out if it works for your usecase.

Thanks for the fix! It works fine for non-id primary keys, but unfortunately it doesn't work when primary key consists of 2 columns, like relationship tables.
The queries are now using only the first key, but all keys are required.

Yes, indeed that might be a problem; I will check if I can implement something for this case; Not all features are supported then, but at least fetch one update etc should work.

๐ŸŽ‰ This issue has been resolved in version 5.0.0 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€