crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Handle columns named `node_id`

Open eddiew opened this issue 9 months ago • 4 comments

Summary

Adding a column named node_id to any table causes postgraphile to emit an error like this, and prevents schema generation:

Error occurred during watch schema generation: Error: Expected an output type named 'ExecutionStep', but that type was not successfully constructed; typically this is because it ended up with no fields.

I assume that there isn't a way to have it work with columns named node_id, since this is part of global object identification spec.

Steps to reproduce

Create a column named node_id

Expected results

Postgraphile should probably show a more helpful error message.

Actual results

See above

Additional context

N/A

Possible Solution

eddiew avatar Apr 12 '25 00:04 eddiew

create table node_id (id serial primary key, node_id int);
insert into node_id (node_id) values (null), (1), (2);
{
  allNodeIds {
    nodes {
      id
      nodeId
    }
  }
}
{
  "data": {
    "allNodeIds": {
      "nodes": [
        {
          "id": "WyJub2RlX2lkcyIsMV0=",
          "nodeId": null
        },
        {
          "id": "WyJub2RlX2lkcyIsMl0=",
          "nodeId": 1
        },
        {
          "id": "WyJub2RlX2lkcyIsM10=",
          "nodeId": 2
        }
      ]
    }
  },
  "extensions": {
    "number": -1
  }
}

benjie avatar Aug 25 '25 17:08 benjie

Okay if you're not using the Relay preset then node_id column name will conflict with the default nodeId: ID field. It'll give you a conflict error though:

  graphile-build:warn Recoverable error occurred: Error: A naming conflict has occurred - two entities have tried to define the same key 'nodeId'.

  The first entity was:

    Adding id field to NodeId type from AddNodeInterfaceToQueryPlugin

  The second entity was:

    Adding 'node_id' attribute field to GraphQL type 'NodeId' (representing PgCodec 'nodeId').
  Details: https://err.red/pnc

benjie avatar Aug 25 '25 17:08 benjie

We should add a diagnostic for this to tell people to either:

  1. Use the relay preset (such that nodeId: ID! becomes id: ID! instead)
  2. Rename the column in the database
  3. Rename the column in GraphQL using smart tags
  4. Rename all node_id columns by overriding the _attributeName inflector
  5. Disable NodePlugin so nodeId: ID! isn't added in the first place

I don't see this as a bug; this is an expected behavior (you can't have two things called nodeId for the same type).

benjie avatar Aug 25 '25 17:08 benjie

I wasn't able to find how you managed to get it to give the error you listed.

benjie avatar Aug 25 '25 17:08 benjie