simonw / datasette-graphql

Datasette plugin providing an automatic GraphQL API for your SQLite databases
https://datasette-graphql-demo.datasette.io/
Apache License 2.0
100 stars 6 forks source link

Idea: tablename_get(pk=value) for looking up individual records by primary key #11

Closed simonw closed 4 years ago

simonw commented 4 years ago

e.g. for the users table you could do this:

{
  users_single(id=234234) {
    login
  }
}

It would be the table name with _single as the suffix.

simonw commented 4 years ago

Usual problem: what if there's a table called users_single? You could access single rows from that using users_single_single - but if there was also a table called users you would be unable to access single rows from THAT table.

I think I can ignore this edge-case. Redesign your database!

simonw commented 4 years ago

Maybe users_first instead? Basically the same mechanism as users_list but it gives you back a single row that's the first item from the query, rather than making you nest through the nodes field.

simonw commented 4 years ago

So it should take the same arguments as users_list - but for convenience it should also take arguments for the primary key columns so you can do a direct lookup using them if you know them.

simonw commented 4 years ago

I have a working prototype now (without the primary key options) which looks like this:

{
  repos_first(search:"sqlite-utils") {
    name
    network_count

I'm not sold on the _first name - here are some other options:

{
  repos_single(search:"sqlite-utils") {
    name
    network_count
{
  repos_one(search:"sqlite-utils") {
    name
    network_count
{
  repos_get(search:"sqlite-utils") {
    name
    network_count
simonw commented 4 years ago

I'm leaning towards repos_get now.

simonw commented 4 years ago

I want to do something neat with the potential clash between these primary key arguments and the existing search: etc arguments - which I also need for handing columns that have a _list prefix and could clash with #27.

Simplest option: add _ suffixes until the argument name no longer clashes with an existing argument.