Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier api to get pk columns of table #2233

Open
6 tasks
p-bakker opened this issue Nov 7, 2024 · 2 comments
Open
6 tasks

Easier api to get pk columns of table #2233

p-bakker opened this issue Nov 7, 2024 · 2 comments

Comments

@p-bakker
Copy link

p-bakker commented Nov 7, 2024

Feature description

Determining the (name of) the pk columns of a table can be done inside plugins, but requires digging into introspection details

So, this request if to have an easy api to get the pk columns (.getPKAttributes()?)

From my limited knowledge of Postgraphile and the comments on discord, it seem that that at least in one scenario one can have a reference to a codec and in other scenarios a pgClass and in both cases it should be possible to get the (name of the) pk column(s)

Motivating example

See https://discord.com/channels/489127045289476126/1296863704364879902/1298276538726481992

Breaking changes

Don't think so

Supporting development

I [tick all that apply]:

  • am interested in building this feature myself
  • am interested in collaborating on building this feature
  • am willing to help testing this feature before it's released
  • am willing to write a test-driven test suite for this feature (before it exists)
  • am a Graphile sponsor ❤️
  • have an active support or consultancy contract with Graphile
@github-project-automation github-project-automation bot moved this to 🌳 Triage in V5.0.0 Nov 7, 2024
@benjie
Copy link
Member

benjie commented Nov 7, 2024

Currently the best option is something like: pgClass.getConstraints().find(c => c.contype === 'p')?.getAttributes()?.map(a => a.attname)

The place to add this, if you wanted to contribute, would be in the pg-introspection module.

@benjie benjie removed this from V5.0.0 Nov 7, 2024
@benjie
Copy link
Member

benjie commented Nov 7, 2024

Oh that's for the gather phase where you're looking at introspection and creating codecs/resources.

If you're in the schema phase turning codecs/resources into GraphQL things then:

  • a codec doesn't have a primary key, it's just a description of a data type (that type could be shared by tables, function return types, etc)
  • from a resource you can do resource.uniques.find(u => u.isPrimary)?.attributes
  • it's quite common to look for the non-function resource that relates to a codec, though it's not guaranteed that there will be exactly one of these: there could be none, or there could be many (but that's extremely rare!). Typically that would be pgRegistry.pgResources.filter(r => r.codec === codec && !r.parameters) and then checks that the set contains exactly one entry, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants