Just a thought, but any desire to make it easy to create a GraphQL::ObjectType w/ associations automatically from an AR model?
I know people wouldn't automatically have a 1-1 between GraphQL types and AR models, but is something this project would aspire to do?
Hi, I think it's a very interesting process and I'm doing it for my own API. But I don't think it belongs in this project. Basically, my goal here is to make a backend-agnostic GraphQL implementation, so I don't want to add an ActiveRecord dependency.
I hope someday, someone (maybe me?) will make a graphql-rails (or graphql-active-record) gem that includes this feature!
I was playing around with this idea as well.
This gem https://github.com/voormedia/rails-erd can introspect ActiveRecord models generate Entity-Relationship Diagrams. While the goal is not to generate diagrams, I believe it may be possible for someone to leverage parts of this gem to automatically generate GraphQL::ObjectType(s)
Figured I would post this here and maybe someone will think it's an interesting idea to play around with.
Also I do agree with @rmosolgo about it not belonging in this particular gem.
I started playing around with a few similar ideas a few months ago (before even being aware of GraphQL) and built a library that lets you specify a dependency graph for your ActiveRecord::Associations. It then serializes the result of a query using that dependency graph into json. https://github.com/IFTTT/kashmir
We use it at IFTTT in a few endpoints, and it has been working really well.
Maybe it would make sense to port some of the code in Kashmir and try to create a GraphQL implementation for ActiveRecord.
Yeah, it wouldn't be to hard. This is throw away code that I used to scaffold up something working from a large model set.
THIS IS SLOPPY!
https://gist.github.com/jaunkst/ca1733fb07c931a069e4ef0b6fbfc9c0
Things to consider is PostgreSQL types, and mapping data types in general.
Also will probably need to load part of the environment up for model dependencies or require them yourself.
All the type declarations are wrapped in a proc since it would require smarts about circular relationships.
I was able to get about 30 of 34 types generated, the others are due to polymorphic relationships which I haven't thought about.
Hey everyone, shall we actually move this to a new gem? I think generating graphql models would probably be a good idea, since it makes everything quit DRY. And tbh making schema's is a pain. Who wants to help to contribute? Maybe start a slack/gitter to discuss how to move this forward?
TLDR: Graph (API) != relational
I'm not sure I agree that generating the schema is a good idea.
I don't believe you should be modeling your API after your database schema (not even if you're doing rest). Your storage constraints aren't necessarily your clients constraints, neither should your client domain mirror your storage domain.
It's not uncommon to see GraphQL schemas with deeply nested entities. That, in my experience, just makes your clients brittle and more complex than they have to.
Model also does not necessarily == relational either. I know I started this thread saying AR model and I know Ruby (outside of ROM) doesn't really pursue the data mapper pattern much. I suppose if you did have a pure model, then you could almost use graphql-ruby out of the box and this is all a moot point.
This project is trying to do this https://github.com/goco-inc/graphql-activerecord
Is this done already? I dont like typing :) thats why i love rails :)
Most helpful comment
TLDR: Graph (API) != relational
I'm not sure I agree that generating the schema is a good idea.
I don't believe you should be modeling your API after your database schema (not even if you're doing rest). Your storage constraints aren't necessarily your clients constraints, neither should your client domain mirror your storage domain.
It's not uncommon to see GraphQL schemas with deeply nested entities. That, in my experience, just makes your clients brittle and more complex than they have to.