Skip to content
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

my_resource.data raises Graphiti::Errors::RecordNotFound in #create action after Graphiti::Resource#build(params) when the table is empty #458

Open
dima4p opened this issue Nov 15, 2023 · 5 comments

Comments

@dima4p
Copy link

dima4p commented Nov 15, 2023

`When the table has no record returns @scope.resolve an empty Array.
In my controller there is

# POST /user_absences
def create
  @user_absence = UserAbsenceResource.build(params)
  authorize @user_absence.data

  if @user_absence.save
    render jsonapi: @user_absence, status: 201
  else
    render jsonapi_errors: @user_absence
  end
end

and I get

 Graphiti::Errors::RecordNotFound:
   Specified Record Not Found

in the case.

@dima4p dima4p changed the title my_resource.data raises Graphiti::Errors::RecordNotFound in #create action after Graphiti::Resource#build(params) when the tabel is empty my_resource.data raises Graphiti::Errors::RecordNotFound in #create action after Graphiti::Resource#build(params) when the table is empty Nov 15, 2023
@dima4p
Copy link
Author

dima4p commented Nov 15, 2023

Additionally is my_resource.data always nil after Graphiti::Resource#build.
Of course in the case I set raise_on_missing: false here

@factyy
Copy link
Contributor

factyy commented Nov 20, 2023

@dima4p , from our experience I can tell that it's not a good way to rely on resource.data when the model is not yet saved. If you need to check authorization on the model instance creation step, there is a separate gem for it (assuming you are using ActionPolicy).

@dima4p
Copy link
Author

dima4p commented Nov 20, 2023

@dima4p , from our experience I can tell that it's not a good way to rely on resource.data when the model is not yet saved. If you need to check authorization on the model instance creation step, there is a separate gem for it (assuming you are using ActionPolicy).

We are using Pundit. I think, that it is important to have the ability to use Graphiti::Resource#build#data to use the new model for authorization.
Now I have to keep in controller the method "#{singular_table_name}_params" and create an additional instance of Model with #new in order to be able to authorize it.

@factyy
Copy link
Contributor

factyy commented Nov 20, 2023

@dima4p , if you are using Graphiti under the hood, you can hook into model creation via the before_save callback where you will have full access to the newly-built (but not yet saved) model

@factyy
Copy link
Contributor

factyy commented Nov 20, 2023

@dima4p , one more thing: keep in mind that with JSON:API you have the ability to sidepost multiple resources in one request and thus you will have to either parse the whole resource tree yourself or rely on Graphiti with the approach I mentioned above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants