Skip to content

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

Closed
vegetabill opened this issue Sep 27, 2017 · 4 comments
Closed

how to return rails routes in resolver #992

vegetabill opened this issue Sep 27, 2017 · 4 comments

Comments

@vegetabill
Copy link
Contributor

vegetabill commented Sep 27, 2017

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 a profile_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:

Types::UserType = GraphQL::ObjectType.define do
  include Rails.application.routes.url_helpers
  field :profile_url, types.String do
      description 'web show url'
      resolve -> (user, args, ctx) { user_url(user) }
  end
end

I tried adding include Rails.application.routes.url_helpers in the define 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.

@robak86
Copy link

robak86 commented Sep 29, 2017

Not sure if it is the best answer, but I guess you can create reference to routes helper inside ctx.

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 },
)

@rmosolgo
Copy link
Owner

👍 to the solution above, of course, there's a "naughty" way to accomplish the same thing by reaching for the global constant inside resolve, eg:

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

@rmosolgo
Copy link
Owner

include ... not in scope during actual execution

this is such a good thing to have tried, I'm sorry it didn't work ... I regret using instance_eval 😖

A PR to the docs would be great :)

@vegetabill
Copy link
Contributor Author

Thanks for the info. I'll make a doc PR soon!

vegetabill added a commit to vegetabill/graphql-ruby that referenced this issue Oct 4, 2017
rmosolgo pushed a commit that referenced this issue Oct 10, 2017
added FAQ doc page to have tips such as the question in #992
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants