Open danjvarela opened 1 year ago
So far, the only workaround that works for me is overriding the endpoint_namespace
and model
of Api::V1::NoteResource
like so:
class Api::V1::NoteResource < ApplicationResource
self.endpoint_namespace = ''
self.model = Note
attribute :content, :string
attribute :created_at, :datetime, writable: false
end
Hi,
I used the same workaround ! But i did self.endpoint_namespace = '' in ApplicationRessource that inherit from Graphiti::Resource. Then, all my resource inherit from ApplicationResource so i don't need to override endpoint_namespace in every resources.
Also, you don't need to override self.model in every resource. In ApplicationResource again, override the infer_model method with something like this:
# Extends our Graphiti needs
class << self
def infer_model
name&.split('::')&.last&.sub("Resource", "")&.safe_constantize
end
end
You may need to override others to make it work !
What I hope to achieve
Have routes mapped to namespaced controllers (
Api::V1::NotesController
) much like the one below:Attempted solution:
rails new sample-graphiti-api --api -m https://www.graphiti.dev/template
<ENTER>
if asked for the namespace (default is/api/v1
)rails generate model Note content:string
rails generate graphiti:resource note content:string created_at:datetime
(rails generate graphiti:resource api/v1/note content:string created_at:datetime
doesn't work so we do it manually)app/resources/note_resource.rb
toapp/resources/api/v1/note_resource.rb
and changeNoteResource
toApi::V1::NoteResource
Move
app/controllers/notes_controller.rb
toapp/controllers/api/v1/note_resource.rb
and changeNotesController
toApi::V1::NotesController
. Also, change all occurrences ofNoteResource
toApi::V1::NoteResource
. File should look like this:GET /api/v1/notes
. An error occurs:Graphiti::Errors::InvalidEndpoint ( Api::V1::NoteResource cannot be called directly from endpoint /api/v1/notes#index!
):