-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Rails adoption process #511
Comments
One of the things we do in our controller is create a standard context object, which includes some things like an error and performance profiling accumulator, which gets flushed to the respective logging services (new relic, statsd, etc) at in an after filter. |
Very cool, seems like at a minimum, a generator could guide the new user in the right direction with something like: query_context = {
# You can access these values anywhere
# in GraphQL execution, so use them to
# scope the query or accumulate information.
# eg:
# current_user: current_user,
}
{Generated}Schema.execute(query_string, {
context: query_context,
variables: query_variables,
}) |
Here are a few ideas: Controller/Route setup
post '/graphql' => 'graphql#query'
GraphQL Specific folderSomething like this:
|
ah, yep, folder structure! ours looks something like
where |
|
Another option would be adding Sprockets-ready versions of apollo and Relay. I might be the only Sprockets user left on earth, and Relay might not even work with Sprockets, but I thought I'd mention it 😆 |
One thing I have done is make a base resolver class that handles typical use cases. I.E. when you know a collection is going to respond in the standard Rails way:
Gist of the base resolver here: https://gist.github.com/dphaener/94c9ca91b1bef6e8af37441cffd2f5f8 For me, this has removed a lot of boilerplate code. For instance: connection :todos do
type -> { TodoType.connection_type }
resolve TodoResolver.new.collection
end I can also use a different caller if I have a custom method: connection :completed_todos do
type -> { TodoType.connection_type }
resolve TodoResolver.new(:completed_todos).collection
end This will call |
I also have a whole bunch of generators that I use: https://github.com/DNACodeStudios/react-rails-bootstrap/tree/master/lib/generators |
@rmosolgo I think supporting relay right away could be overkill. |
🙅♂️ I don't know! |
relay is a very common case, but supporting relay-specific things ought to be an option when scaffolding a new project. |
Looks like Rails' webpacker could make integrating Rails + Relay / Apollo a lot easier. |
I'm trying to setup Relay for the graphql-ruby-demo app. I'm using the webpacker gem to get all the js webpack stuff running. My idea is to write a installer that installs Relay in a Rails project, similar to the React installer in webpacker. Here's the (huge) PR rmosolgo/graphql-ruby-demo#24 for the demo app on Relay, currently it has only React support since I've not yet started with the Relay installer. |
Sketched out a few specific generators at #521, I'd love to hear your opinions on them if you have any ! |
One random thing I thought I'd comment on: I would strongly prefer that the conventional/generated GraphQL folder name in Rails be A Google search for "rails graphql" yields much clearer results than a search for "rails graph". (E.g. by a newbie to a codebase that uses GraphQL wondering "what does this folder do...?") |
good point! I also use |
Do ppl suffix their type names with My current project puts types in the global namespace, so most of them are How do you handle this? |
In the first project I did, I used a I do have a project where I'm already using the I know that using namespaces is verbose, but I really like how explicit it is. I would rather see |
Thanks for sharing that comparison! I hadn't considered the possibility of conflicts inside The most-recently-suggested folder structure does not have a Another seriously verbose option is |
You know, the only reason I used the |
Not yet, but in general, I can't recommend adding to namespaces you don't "own" ! |
@xuorig and I had also went with However, by dropping As for |
+1 to the namespace-ownership issue mentioned above. But I think it also makes more sense conceptually to use |
One issue with Rails will expect to find those constants in files like
What should (I couldn't think of a good answer, so I ended up with the structure described in #521 😆 ) |
I think having it in |
|
@rmosolgo Yeah this is what we've been working on today, actually 😆 We did run into autoloading issues - and
@xuorig Yeah I'm wondering if putting it under models makes the most sense/solved the autoloading. |
A lot of people also have form objects / POROs in models too, and I think graphql models / types are just that too so 🤔 |
I also put business logic in |
Ehh I disagree. The types, mutations, etc. are a representation of your domain within the language of GraphQL's type system. Every type system is different and has different limitations and idioms, so simply calling it "your graph" feels too broad. It's your app's graph as viewed through the constraints and conventions of a GraphQL schema. |
@vergenzt Yeah fair enough. I'm still new at at these GraphQL shenanigans. I care more about some conventions existing than I do about what specifically they are. |
I think that's one case to put it in In a dream world, |
Thanks for the conversation here! I've merged some generators that will be released in 1.5.0 (next week 🤞 ). If you have another suggestion about Rails onboarding, please open an issue for it! |
I am running into the same issue OP has where the ID pass in is the global id and not the integer I would expect it to be. It seems like the mutations should not be responsible for translating global ids to database ids. How do you translate relational ids to database ids in a mutation without having to do the translation in the mutation? |
Currently, the first several steps of adding GraphQL to your Rails app involve copy-pasting lots of boilerplate code:
Then, expanding the schema also requires repetitive work: opening files and typing boilerplate code.
What could be done to improve this process?
If we were to provide an opinionated setup & organization strategy, what would it be?
Could we also improve the client-side experience? (Eg, configuring apollo-client to send Rails CSRF tokens?)
Rails generators could solve a lot of problems here. It's important to keep Rails as a totally optional dependency for this library.
The text was updated successfully, but these errors were encountered: