Conversation
|
👍 This looks like a great start. I think I would also vote for a generator that ties a bunch of those together. I typically like to create the model/type/mutations/resolvers all at the same time. |
|
That's a good point about tying them together -- I know Rails has hooks for |
|
My personal opinion on that would be to not automatically tie it in to a model generation, but rather wrap up the model generation in a "Higher Order" generator of sorts. Something like what I'm doing here: https://github.com/DNACodeStudios/react-rails-bootstrap/blob/master/lib/generators/graphql_resource_generator.rb That makes it more of an opt in API, rather than an opt out.
|
|
I know @theorygeek is working on goco-inc/graphql-activerecord--would that be the most appropriate place for Rails- and/or ActiveRecord-specific code? |
| # | ||
| # ``` | ||
| # - app/ | ||
| # - graphql/ |
| # | ||
| # Accept a `--graphiql` option which adds | ||
| # `graphiql-rails` to the Gemfile and mounts the | ||
| # engine in `routes.rb` (Should this be the default?) |
There was a problem hiding this comment.
Should this be default?
I'd think so. :) It seems like someone not wanting GraphiQL will likely be the exception and not the norm.
| # ``` | ||
| # | ||
| # Should we have a loader for the | ||
| # nearly-universal foreign key loader? |
There was a problem hiding this comment.
Should we have a loader for the nearly-universal foreign key loader?
👎 Personally I think any code that's "nearly-universal" should probably be in a gem rather than generated into the application codebase. Not sure whether graphql-ruby is the most appropriate gem or not--but maybe graphql-activerecord?
3dd7989 to
9158613
Compare
| # - query_type.rb | ||
| # - loaders/ | ||
| # - mutations/ | ||
| # - {app_name}_schema.rb |
There was a problem hiding this comment.
Is there a reason for prefixing with {app_name}_ besides the fact that otherwise we'd be conflicting with GraphQL::Schema?
There was a problem hiding this comment.
🤔 I did this in my project to avoid file picker conflicts with db/schema.rb. (I thought it was a real Ruby naming conflict, but looking back, I see that it's not.) Maybe that's not a good enough reason to do so!
There was a problem hiding this comment.
Personally I'm a fan of namespacing the schema name. Rails autoloading expects the filename to match the constant name, and having the app-wide constant for the schema simply be called Schema doesn't seem right to me. :/
| # | ||
| # ```ruby | ||
| # # config/routes.rb | ||
| # post "/graphql", to: "graphql_queries#create" |
There was a problem hiding this comment.
Why not just graphql#create?
There was a problem hiding this comment.
That could be good, then you could even do something like
resource :graphql, only: :createThere was a problem hiding this comment.
or maybe:
post "/graphql", to: "graphql#create"|
I pushed an install generator along the same lines as initially described, how do you think in played out? I still need to give it a run with |
1ac8405 to
bc75596
Compare
|
Thanks for you input along the way! I've tried to cover all the ground here. Does anyone want to take another look before I get to documenting it? Any reasons not to recommend this kind of setup? ps @dphaener Thanks for clarifying that. I think I'll start with some "simple" generators and then explore those options! |
| create_dir("app/graphql/types") | ||
| template("query_type.erb", "app/graphql/types/query_type.rb") | ||
| template("schema.erb", "app/graphql/#{schema_name.underscore}.rb") | ||
| template("graphqls_controller.erb", "app/controllers/graphqls_controller.rb") |
There was a problem hiding this comment.
Could this be graphql_controller.rb instead? Pluralized GraphQL feels a bit weird to me.
There was a problem hiding this comment.
Could be, then we'd have to switch:
- resource :graphql, only: :create
+ post "/graphql", to: "graphql#create"I guess it's not really a resource anyways, old habits die hard :P
There was a problem hiding this comment.
I wonder then if we should keep create as the action. Sounds a bit off, maybe something custom like execute?
There was a problem hiding this comment.
That's nice, I updated to post "/graphql", to: "graphql#execute"!
| case ambiguous_param | ||
| when String | ||
| if ambiguous_param.present? | ||
| JSON.parse(ambiguous_param) |
There was a problem hiding this comment.
This won't necessarily be a hash if ambiguous_param is "[]". We might want to deal with that?
There was a problem hiding this comment.
I guess we could assert that the parsed value is a Hash.
I just realized that this won't work on Rails 5 since ActionController::Parameters won't match when Hash 😖
I'll take a look at whether I can return the parameters as-is or whether they need to be to_unsafe_hashed.
There was a problem hiding this comment.
Oh, it worked ok with GraphiQL since it sends the body as a string. I'll improve the messaging a bit in case someone does send random input
| @@ -0,0 +1,5 @@ | |||
| <%= type_ruby_name %> = GraphQL::ObjectType.define do | |||
| name "<%= type_graphql_name %>" | |||
| <% if options.node %> interfaces [GraphQL::Relay::Node.interface] | |||
There was a problem hiding this comment.
We can use implements GraphQL::Relay::Node.interface now. 🎉
| <% end %><% if options[:batch] %> | ||
| # GraphQL::Batch setup: | ||
| lazy_resolve(Promise, :sync) | ||
| instrument(:query, GraphQL::Batch::Setup) |
There was a problem hiding this comment.
Edit: Moved discussion to GraphQL-Batch: Shopify/graphql-batch#49
… runs GraphiQL out of the box
|
I added a guide and yanked |
I think generators could make the Rails experience much better! We started chatting about them over at #511 . I've sketched out a few possibilities here, what do you think?
TODO: