Tuql: Automatically create a GraphQL server from a SQLite database
github.com
github.com
First of all, your GraphQL schema is basically append-only due to older clients that may remain in the wild. So you don't want to expose implementation details that may change.
Second, you want to write client code that handles mutations. This is easier if the data the client receives is organized in a UI-centric way. I'll give you a simple example that came up at work recently: a single conceptual category (a "user account") that, due to implementation details, was spread across two different tables with different columns. Because the GraphQL schema in this case mapped each table to its own GraphQL type, somebody was then able to write client code that only handled one type and not the other, causing an inconsistent UI.
I would suggest thinking carefully about your GraphQL schema, treating it as an API, and not auto-generating it. Of course, you want it to be convenient to construct, just not fully automatic and thoughtless.
That is certainly not a common use of web based APIs, to have an application that is read only every client, but the reason I came to this thread was because I’ve considered SQLite for this idea in the past.
I have been met with a lot of resistance around this notion for some reason.
To be honest, I fought the same argument with OpenAPI (Swagger) too.
API developers seemingly just want to chuck their schema over the wall and walk away
I definitely suffer from that problem when trying to work out what a good API for frontend would be, and frontend people often have their own blind spots which makes working together to find something actually good a tricky process.
Then again, getting REST/GraphSQL/API-of-choice designs 'actually good' is hardly a trivial problem at the best of times, so how much of this is developer biases and how much inherent difficulty isn't something I'd be confident trying to estimate.
If you create those UI-centric models when exposing data through your GraphQL schemas, you just moved the modelling work elsewhere but haven't actually facilitated anything, and it's still centralized. At this point you're better off embracing the 'BFF' architecture and skipping GraphQL altogether.
There may be a useful middle ground (for example, ensuring a single User type), but it's a slippery slope to stand on.
A problem that is common is that we send these JSON blobs over the wire that aren't purpose fit. So, with GraphQL, we can construct a query graph that makes the JSON blobs slimmer and more purpose fit, by constructing the queries to return the data we desire for some particular business reasons we need to represent in the UI, for instance.
I got the argument with mutations need to match things more closely, but I'd argue you can cross compose mutations w/ resolvers too.
I think this is what they're getting at. Just throwing the database schema over the wall via GraphQL isn't that much better than REST, and takes zero advantage of abilty to use revolvers to construct purpose built queries
What is the defacto "hobbyist tinker-er" standard for "I want to deploy this Docker thing into the cloud and play with it on the Internet"? There's got to be some one-click deploy solution where it goes to a Github repo, picks up a Helm chart, deploys it to a k8s cluster or something?
I say 'intending' because I've not tried it rather than to imply any informed opinion about where they are in the process of achieving said goal.
Demo here: https://datasette.simonwillison.net/simonwillisonblog
GraphQL demo here: https://datasette.simonwillison.net/graphql?query=%7B%0A%20%...
To deploy a SQLite database to Vercel with a GraphQL API:
brew install datasette
datasette install datasette-publish-vercel
datasette publish vercel mydatabase.db \
--project my-new-vercel-project-name \
--install datasette-graphql
Here's a tutorial on how to get data into that SQLite database in the first place: https://datasette.io/tutorials/clean-dataHere's an example [1] multi-page site with Next.js and Seafowl.
[0] https://seafowl.io/docs/getting-started/introduction
[1] https://github.com/splitgraph/madatdata/tree/main/examples/r...
The biggest feature I can see that's missing is pagination - it looks like this doesn't have a way to retrieve e.g. ten results, then pass a next token to get back the next set.
Here's how I implemented pagination in my similar datasette-graphql plugin (which also gives you a GraphQL API for an existing SQLite database): https://github.com/simonw/datasette-graphql#pagination
Is it naming relations a plural word a common thing in practice?
I thought best-practice was to name relations either singular (as each tuple represents one entry) or uninflected (still singular for most words), specially when you're not a fluent speaker of the language being used to name the relations of the database.
Plurals are often irregular for commonly used words, and the fact that this requires a external dependency ( https://github.com/plurals/pluralize ) to cover for some "common plurals" is telling that supporting this feature is a complex thing indeed - that would not be required in the first place with singular everywhere.
> Is it naming relations a plural word a common thing in practice?
It's unfortunately what rails ActiveRecord does, and other solutions inspired by Rails.
If one keep the domain models and tables in a consistent language (ie translate everything to English) - it is somewhat consistent, and the code reads somewhat pleasantly like a DSL.
In practice I find that business logic/data modelling needs to/should be done in local language and then this becomes a bit of an annoyance and point of friction.
https://guides.rubyonrails.org/active_record_basics.html#nam...
That’s great if so. I haven’t looked at lines of code in any other graphql library but I’d guess there’d be far more.
Also, does the generated schema include the primary keys? Otherwise caching in the frontend might turn out to be difficult.
Yup, this is the way I've seen it implemented everywhere so far.
Edit: roles via jwt token https://hasura.io/docs/latest/auth/authentication/jwt/
I originally wrote this to speed up prototyping / development projects, I'd never recommend shipping this anywhere near production.