-
Notifications
You must be signed in to change notification settings - Fork 138
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
Generators do not handle namespaces correctly #138
Comments
I have a similar issue that I'm a bit confused with - you can define an API endpoint namespace in your resource ( I am aware I can namespace the resource, e.g. Is there a suggested path forward, or better way of going about namespacing the controller endpoints? |
@geeosh I think there's an improvement to the generators we can make. That said, the "endpoint namespace" is somewhat orthagonal - I personally route to my APIs by URL prefix ( We can improve the generators, and perhaps need a separate configuration option in |
I'm also having this problem, can you work around it? is this method that deals with this? https://github.com/graphiti-api/graphiti/blob/master/lib/graphiti/resource/configuration.rb#L48 |
@danieldocki it's not really an issue with Graphiti itself, but the code the generators generate. If you used the generators and then manually fixed routes and factories, or wrote the code manually, it should work. We should certainly improve the generators though. |
@richmolj namespace :api, constraints: { format: :jsonapi } do
namespace :v1 do
resources :categories, except: %i[new edit]
end
end class Api::V1::CategoryResource < ApplicationResource
attribute :name, :string
attribute :created_at, :datetime, writable: false
attribute :updated_at, :datetime, writable: false
end irb(main):003:0> Api::V1::CategoryResource.all.data
Traceback (most recent call last):
1: from (irb):3
Graphiti::Errors::ModelNotFound (Graphiti::Errors::ModelNotFound) to make it work I override the method class Api::V1::CategoryResource < ApplicationResource
attribute :name, :string
attribute :created_at, :datetime, writable: false
attribute :updated_at, :datetime, writable: false
def model
Category
end
end Is it okay to do this? |
@danieldocki yep, though I would recommend the accessor for this: class Api::V1::CategoryResource < ApplicationResource
self.model = Category
# ... code ...
end |
I solved this using a base # resources/v1/api_resource.rb
class V1::ApiResource < ApplicationResource
self.abstract_class = true
def self.infer_model
name&.split('::').last&.gsub("Resource", "")&.safe_constantize
end
end
# resources/v1/category_resource.rb
class V1::CategoryResource < V1::ApiResource
# self.model = Category # <-- not needed
attribute :name, :string
attribute :created_at, :datetime, writable: false
attribute :updated_at, :datetime, writable: false
end FWIW, I didn't namespace with |
When using the generator it creates invalid ruby code for specs and bad routes. For example:
Generates specs with fixtures names
:local/user
and a route tolocal_users
instead oflocal/users
.The text was updated successfully, but these errors were encountered: