Skip to content

Commit

Permalink
Merge pull request #4009 from bubba/patch-1
Browse files Browse the repository at this point in the history
Add example of using url_helpers in resolvers
  • Loading branch information
rmosolgo committed Apr 12, 2022
2 parents e91db31 + 6a1ada0 commit 170b402
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions guides/faq.md
Expand Up @@ -10,25 +10,62 @@ desc: How to do common tasks

Returning Route URLs
====================
With GraphQL there is less of a need to include resource URLs to other REST resources, however sometimes you want to use Rails routing to include a URL as one of your fields. A common use case would be to build HTML format URLs to render a link in your React UI. In that case you can add the Rails route helpers to the execution context as shown below.
With GraphQL there is less of a need to include resource URLs to other REST resources, however sometimes you want to use Rails routing to include a URL as one of your fields. A common use case would be to build HTML format URLs to render a link in your React UI. In that case you can pass the request to your context, so that the helpers are able to build full URLs based on the incoming host, port and protocol.

Example
-------
```ruby
class Types::UserType < Types::BaseObject
include ActionController::UrlFor
include Rails.application.routes.url_helpers
# Needed by ActionController::UrlFor to extract the host, port, protocol etc. from the current request
def request
context[:request]
end
# Needed by Rails.application.routes.url_helpers, it will then use the url_options defined by ActionController::UrlFor
def default_url_options
{}
end

field :profile_url, String, null: false
def profile_url
context[:routes].user_url(object)
user_url(object)
end
end

# Add the url helpers to `context`:
# In your GraphQL controller, add the request to `context`:
MySchema.execute(
params[:query],
variables: params[:variables],
context: {
routes: Rails.application.routes.url_helpers,
# ...
request: request
},
)
```

Returning ActiveStorage blob URLs
=================================
If you are using ActiveStorage and need to return a URL to an attachment blob, you will find that using `Rails.application.routes.url_helpers.rails_blob_url` alone will throw an exception since Rails won't know what host, port or protocol to use in it.
You can include `ActiveStorage::SetCurrent` in your GraphQL controller to pass on this information into your resolvers.

Example
=======

```ruby
class GraphqlController < ApplicationController
include ActiveStorage::SetCurrent
...
end

class Types::UserType < Types::BaseObject
field :picture_url, String, null: false
def picture_url
Rails.application.routes.url_helpers.rails_blob_url(
object.picture,
protocol: ActiveStorage::Current.url_options[:protocol],
host: ActiveStorage::Current.url_options[:host],
port: ActiveStorage::Current.url_options[:port]
)
end
end
```

0 comments on commit 170b402

Please sign in to comment.