Comments (7)
I have a similar issue that I'm a bit confused with - you can define an API endpoint namespace in your resource (self.endpoint_namespace = '/api/v2'
), but then when generating controllers for resources they are dumped into the root /app/controllers/
folder.
I am aware I can namespace the resource, e.g. Api::V2::Profile
, but then it requires a corresponding model with the namespace, whereas my model is simply Profile
. I know I can set it up this way without the generators, but it would be nice if the generator could somehow handle this case.
Is there a suggested path forward, or better way of going about namespacing the controller endpoints?
from graphiti.
@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 (/news_api/v1
routes to news_api
, with no namespacing).
We can improve the generators, and perhaps need a separate configuration option in graphiticfg.yml
- something like module_namespace
for this case.
from graphiti.
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
from graphiti.
@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.
from graphiti.
@richmolj
Regarding routes yes I did manually, but in relation to the resource namespace?
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 model
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?
from graphiti.
@danieldocki yep, though I would recommend the accessor for this:
class Api::V1::CategoryResource < ApplicationResource
self.model = Category
# ... code ...
end
from graphiti.
I solved this using a base V1::ApiResource
that all V1
resources extend (instead of root ApplicationResource
), which overrides the #infer_model
method.
# 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 Api
, in my case, since resources were only used in that context. 🤷♂
from graphiti.
Related Issues (20)
- sideposting - associating same newly created record with multiple records that are also being created
- Sideposting- includes don't show when test with Postman
- Polymorphic has_one and has_many doesn't respect custom polymorphic_name
- Data access questions HOT 1
- Missing next page link when the resource is a postgres table has json columns HOT 1
- Proposal to allow for overriding relationship getter HOT 1
- Record ID for authorization
- Guide - Outdated external link
- can not call method with args including keyword inside attribute/extra_attribute
- Problem in namespacing controllers HOT 2
- Template for tutorials are broken
- Parsing filter values with {{}} has unwanted side effects.
- Resources with circular relationships fail to load
- Can not call method with key args inside extra_attribute/attribute block HOT 3
- my_resource.data raises Graphiti::Errors::RecordNotFound in #create action after Graphiti::Resource#build(params) when the table is empty HOT 5
- Deep Query Filter HOT 1
- Not compatible with Rails 7.1 HOT 3
- The automated release is failing 🚨
- The automated release is failing 🚨
- ActiveRecord::ConnectionTimeoutError "all pooled connections were in use" after enabling concurrency HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphiti.