-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Support for custom fields configured using SQL queries #16
Comments
Need to think about how magic parameters will work: https://datasette.readthedocs.io/en/stable/sql_queries.html#magic-parameters |
Initial implementation just needs to support read-only canned queries with their parameters turned into GraphQL arguments. |
I'll implement |
Here's a tricky aspect of canned queries: how do we know what columns will be returned so we can define them in the schema? And how do we know the types of those columns? We may have to return them all as strings. Running the query with "limit 0" is enough to get back the column names: Column types may not be possible to obtain in advance. Might have to default to string and allow people to override those in plugin configuration somehow. |
It would be neat if you could optionally specify that certain columns returned by a canned query are actually IDs that correspond to primary keys in other tables, to enable foreign key nested expansions. |
Maybe this isn't about existing canned queries at all: maybe it's about being able to define custom names fields in terms of SQL queries, with type information and explicit arguments and nested SQL definitions for nested fields. All this could go in the plugin configuration. |
It would also be good if these could support cursor adds pagination. This is a feature missing from Datasette canned queries at the moment. It requires a defined sort order. |
For the problem of detecting what types a SQL query will return, I may have a partial solution:
So basically the trick is to run This appears to work for queries that select from a real table, but weirdly it can't identify the type of literal values that were included in the select. |
Using |
No, even the
|
I can assume type of |
An operation that concatenated text onto a value from a real column also resulted in a type that was not detected correctly:
|
Columns without types can be tricky: the won't benefit from type coercion so queries like |
Configuration concept: plugins:
datasette-graphql:
databases:
github:
fields:
recent_releases:
sql: |-
select * from releases order by created_at desc
fields:
repo:
pk_for_table: repos
contributors:
row_type: users
sql: |-
select * from users
where id in (
select user_id from commits
where release_id=:id limit 5 This would setup a That nested |
This is pretty complex stuff! I'm not convinced by the names of things (especially Maybe there's a |
I'm not going to try and get existing canned queries working with this, because of the need for custom syntax to define field types and nested fields. |
The easiest way to implement this would be to use Generic JSON, as seen in #53. I could get this feature running really quickly with that. The downside is that it wouldn't have the ability for the GraphQL query author to select exactly which columns came back. That seems like a shame - but the amount of work required to introspect the SQL query and figure out the columns isn't trivial. Alternatively... let the configuration author chose. By default use Generic JSON, but if they take the time to define the columns that are returned by the query they get a real GraphQL schema out of it. Could look something like this: plugins:
datasette-graphql:
databases:
github:
queries:
releases_since:
sql: |-
select id, title, created_at from releases where created_at > :created_since
fields:
id: integer
title: text
created_at: text |
Given this configuration format it would still be nice to be able to define nested fields, potentially by optionally referencing existing tables. |
This should be designed at the same time as mutations support in #47 since they will likely work in a very similar way. |
Datasette canned queries could work really well with this plugin, especially in conjunction with the ability to restrict direct access to tables in #4.
Andy Ingram on Twitter says: https://twitter.com/andrewingram/status/1289624490037530625
By turning off direct access to tables and instead using canned queries, developers can use this plugin to design the exact GraphQL mechanism that they want. Canned query arguments will turn into GraphQL arguments.
Writable canned queries could even be used to define GraphQL mutations.
The text was updated successfully, but these errors were encountered: