graphiti-api / vandal_ui

Ruby gem for installing Vandal
MIT License
9 stars 12 forks source link

How to deal with relative_url_root in Rails? #2

Open hrigu opened 5 years ago

hrigu commented 5 years ago

On the development environment, Vandal-UI works fine, because the relative_url_root is not set. But on Production, while clicking on the "submit" button, Vandal does not reflect the relative_url_root.

Example Instead of /api/v1/dienste it should produce /app/api/v1/dienste because the relative_url_root is "app". Is there a way to config vandal somehow?

richmolj commented 5 years ago

Vandal starts by grabbing your schema, and then hits URLs your schema defines:

{
  ...
  endpoints: {
    /foo_api/v1/bars: { ... }
  }
  ...
}

So check your schema and make it has the correct path. That path uses .endpoint_namespace configuration to derive those URLs, so if the path is not correct you may want to edit .endpoint_namespace

Let me know if that works for you

hrigu commented 5 years ago

With the manipulated endpoints in schema.json (e.g "/app/api/v1/dienste" instead of "/api/v1/dienste") the generated URLs in Vandal works. However, I have to manipulate the schema.json file manually, since the generation while running the specs fails. (even though the spec "generates a backwards-compatibel schema is green) Due some reason the "endpoints" element is rendered as empty element, when I change the self.enpoint_namespace in _applicationresource.rb

hrigu commented 5 years ago

I've digged deeper into the story. I can't make it work.

  1. I have a dynamically set endpoint_namespace in applcation_resource.rb dependent on the environment:

    • development: /api/v1 (relative_url_root is nil)
    • production: /app/api/v1 (relative_url_root is "/app"
  2. while running the schema#show action (/api/v1/vandal/schema.json on development resp. /app/api/v1/andal/schema.json on prod.) , the "endpoints" element is empty on prod. Reason: in Graphiti::Schema.rb#generate_endpoints the endpoints without context won't be saved:

  def generate_endpoints
      {}.tap do |endpoints|
        @resources.each do |r|
          r.endpoints.each do |e|
            actions = {}
            e[:actions].each do |a|
              next unless ctx = context_for(e[:full_path], a) # context_for("/app/api/v1/...) returns nil

Here the proc in lib/graphiti/railties which does return the context (e.g nil)

    def configure_endpoint_lookup
      Graphiti.config.context_for_endpoint = ->(path, action) {
        method = :GET
        case action
          when :show then path = "#{path}/1"
          when :create then method = :POST
          when :update
            path = "#{path}/1"
            method = :PUT
          when :destroy
            path = "#{path}/1"
            method = :DELETE
        end

        route = ::Rails.application.routes.recognize_path(path, method: method) rescue nil
        "#{route[:controller]}_controller".classify.safe_constantize if route
      }
    end

Any ideas?

richmolj commented 5 years ago

So the paths are recognized in development, but not production, correct?

Is the /app namespace applied by Rails, or something else? If something else, then Rails won't be able to recognize the path. IOW if you ran ::Rails.application.routes.recognize_path('/app/api/v1/foos, method: :get) in production, it seems you are getting nil. I can imagine this is the case if the /app namespace is being applied independent of Rails.