-
Notifications
You must be signed in to change notification settings - Fork 1.4k
how to return rails routes in resolver #992
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
Comments
Not sure if it is the best answer, but I guess you can create reference to routes helper inside Types::UserType = GraphQL::ObjectType.define do
field :profile_url, types.String do
description 'web show url'
resolve -> (user, args, ctx) { ctx[:routes].user_url(user) }
end
end
MySchema.execute(
params[:query],
variables: params[:variables],
context: { routes: Rails.application.routes.url_helpers },
) |
👍 to the solution above, of course, there's a "naughty" way to accomplish the same thing by reaching for the global constant inside resolve -> (user, args, ctx) { Rails.application.routes.url_helpers.user_url(user) } It's probably not a better way of doing it, but it makes the relationship a bit clearer :P |
this is such a good thing to have tried, I'm sorry it didn't work ... I regret using A PR to the docs would be great :) |
Thanks for the info. I'll make a doc PR soon! |
added FAQ doc page to have tips such as the question in #992
Uh oh!
There was an error while loading. Please reload this page.
Hey there it's Bill from Goodreads. I remember you from various issues. This is a really awesome gem I'm trying out.
My question is I want to expose route attributes, e.g. I want
UserType
to have aprofile_url
that is the web URL for viewing a user's profile either in a browser of if they have a native app installed, it will deep link into that.What is the best way to do this? I naively tried this:
I tried adding
include Rails.application.routes.url_helpers
in thedefine
body of the type, but that is not in scope during actual execution. Whatever the right way to do it is, having an example in the docs would be great and I'd be happy to add one as a PR as long as it's the way you think it would best be done.The text was updated successfully, but these errors were encountered: