Open lukaseder opened 4 years ago
This is essentially what projects like Hasura or PostGraphile do (for PostgreSQL specifically). They introspect the whole PostgreSQL schema and build from it:
Maybe those can be useful inspirations for such a feature?
I also recently stumbled upon this which seems like a PoC for something really powerful based on jOOQ: https://github.com/shaposhnyk/graphql-jooq
Thanks for your comment, @mdemierre. I've seen a few jOOQ based POCs already. It's a very promising approach, especially because all the jOOQ goodies (the many SPIs and SQL transformation capabilities) can be leveraged
Wow! this can be another game changer like multiset and json function :)
I have a working implementation of this, unfortunately I can't release it as it's not open source
But I wouldn't be opposed to rewriting it all a second time. There aren't a ton of folks that have worked with GraphQL AST and codegen stuff.
There are two approaches you can take here:
The benefit to this is that it doesn't require generating a schema, which is a non-trivial task. But you don't have any information about the target types being resolved.
To do this, you need to define a mapping between GraphQL identifiers and SQL operations, plus GraphQL arguments and relational algebra arguments.
I wrote a guide on how to do this using Apache Calcite's RelBuilder
class as the query builder (you could swap out jOOQ's DSL.***
methods trivially) here:
This is a bit more involved, but I've done that here:
https://github.com/GavinRay97/GraphQLCalcite
Which again could be converted to jOOQ, though with a bit more effort
But I wouldn't be opposed to rewriting it all a second time.
I don't think anyone would be opposed to this! π
The benefit to this is that it doesn't require generating a schema, which is a non-trivial task. But you don't have any information about the target types being resolved.
jOOQ can work with any type of schema source via org.jooq.Meta
, including parsed and interpreted DDL, XML, JDBC DatabaseMetaData
, etc.
I would love to see the option to convert GraphQL request data to SQL
I know its not the first prototype like this but i've also written a program that translates GraphQL queries to SQL using jOOQ. Maybe people here are interested? Feel free to check it out :) (My goal was to not generate the GraphQL schema but instead create a middle layer that links GraphQL to SQL) https://github.com/lukassailer/byos
@lukassailer I saw your comment yesterday, but there is a lot to say/unpack here so it took me a while to compose a response.
I read your thesis -- how lucky of you to work with the renowned Torsten Grust! I'm a big fan of his publications and educational content around database internals. Quite a nice fellow, too =)
For some background context, I've spent the last ~5 years doing work with programmatic GQL schema generation and query interpretation/query compilation from GQL -> [Some IR] -> SQL.
I've spent most of this time at Hasura, where I wrote the translation layers for a number of DB's (MySQL/Maria, Oracle, Trino/Presto/Athena).
There was one point I wanted to raise around your thesis:
"GraphQL servers with GraphQL to SQL mapping, such as Hasura, generate the whole GraphQL schema, thus removing the ability to customize it almost entirely. These tools may reveal too much of the database to API users. Furthermore, their opinionated nature may clash with legacy projects that have specific demands, for example, in authentication/authorization."
I don't intend this to be a discussion about Hasura, but as far as the above is concerned, this isn't quite correct.
Also, you bring up JoinMonster in your thesis. There is one pretty interesting thing about JoinMonster that I didn't see discussed, and it's that it uses a novel approach to querying for nested data/relationships.
On the topic of the actual SQL generated, there's one thing I'd share with you. It's about the debacle of compiling queries for DB dialects that don't fully support correlated subqueries or LATERAL
/CROSS APPLY
.
Practical examples of this being Presto/Trino/Athena.
The final piece I wanted to touch on was the pros/cons of doing automatic schema generation versus manual schema definitions.
I have a working proof of concept for taking an org.jooq.Meta
and deriving an executable GraphQL schema from it.
I haven't uploaded the code yet, but I'll do it tonight or tomorrow and post here.
I'm interested in eventually getting this upstreamed into jOOQ if possible, so it'd be good to get @lukaseder thoughts on the implementation once I share it here.
As an aside, are you looking for work? π If you wanted to work on this sort of thing for a living, Hasura would be a solid place to do it!
Also, let me know if you ever want to chat more about this topic. I'm very passionate about the topic and am always interested in hearing other people's perspectives and experiences.
You can reach me at (lastname).(firstname)97 at (the google mail), or on Twitter at https://twitter.com/GavinRayDev
I'm interested in eventually getting this upstreamed into jOOQ if possible, so it'd be good to get @lukaseder thoughts on the implementation once I share it here.
Always interested in use-cases like these and how to make them easier to implement on top of jOOQ (e.g. your probably related feature requests for better dynamic SQL construction APIs?). I'm not sure about GraphQL, still. Making jOOQ GraphQL aware seems like a bottomless pit, but again, helping GraphQL APIs build on top of jOOQ is definitely a great thing.
Just bumped into this and wanted to chime in that weβre also building a solution that generates implementation code for a GraphQL Schema using jOOQ queries given a mapping configuration in the form of GraphQL directives.
With the recent investments in SQL/JSON, it should be possible to implement a simple GraphQL to SQL translator that can produce SQL queries from GraphQL specifications.
As shown in this Stack Overflow question, a GraphQL query like this:
Could be translated to a SQL query like this:
This uses correlated subqueries. An approach using
LEFT JOIN
andGROUP BY
is also possible, and perhaps other options exist, e.g. using the SQL standard<JSON array constructor by query>
.This task will be divided in subtasks as follows:
See also: https://stackoverflow.com/a/61428689/521799