sangria-graphql / sangria

Scala GraphQL implementation
https://sangria-graphql.github.io
Apache License 2.0
1.96k stars 223 forks source link

Retrieve all data in one go, export flat result #286

Open nightscape opened 6 years ago

nightscape commented 6 years ago

I am considering writing similar to Join Monster (a GraphQL to SQL translation framework) using Sangria. The final goal would be to take one GraphQL query and transform it to a single SQL query which then returns all data in a table-like format (e.g. CSV or Excel). I've read the Sangria documentation and I think I might be running into two problems:

  1. AFAIU I could create partial SQL queries using the Deferred mechanism, but I'm not sure if I can create a Deferred that does not return a final result that conforms to the specified schema, but rather some representation of the query itself.
  2. I don't know if it's possible to create a flat, table-like export in the end, or if the assumption that the result mirrors the GraphQL query makes this impossible.

Would something like what I am planning be possible at all in Sangria? If so, can you give me any pointers how I could solve the above issues?

Thanks a lot!

OlegIlyenko commented 6 years ago

Hey Martin, this sounds great! 🙌 I think it's a great initiative!

Would something like what I am planning be possible at all in Sangria?

I'm pretty sure it should be possible to implement with sangria in one way or another. If I remember correctly, Join Monster is based on graphql-js which is comparable in functionality to sangria.

You probably will need to work directly with query AST in order prepare suitable SQL query for the while GraphQL query (or parts of it). I think "Query And Schema Analysis" section of the documentation can be helpful here. I think it is also worth a try to implement it as a query reducer.

I'm not sure deferred values would be helpful for SQL generation since they are mostly designed for value fetching. I would suggest to first try implementing it just with regular resolve functions and add deferred values later on if necessary.

I don't know if it's possible to create a flat, table-like export in the end, or if the assumption that the result mirrors the GraphQL query makes this impossible.

You can try to implement a custom result marshaller, but if it does not work, then I guess this needs to be implemented as an additional transformation step after query execution has produced some hierarchical results.

prurph commented 6 years ago

This sounds really cool! It seems like there would be a lot of use cases for the SQL translation to a single query independently of the table-based result as well.

I don't have a lot of experience with Sangria but if you were planning to open source this and looking for help I'd be interested to see if I could be of any use.

nightscape commented 6 years ago

I'm currently experimenting with taking the GraphQL AST and generating SQL from that directly. I've got a first working prototype, but it's bound to the workings of our DB. I'm still pondering how to make it generic enough to apply it for more/all DB schemas. Once I have something worth showing I'll update this issue.