-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add automatic RecordInvalid handling in GraphQL
- Loading branch information
1 parent
910e033
commit 4a950a5
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Errors | ||
module ActiveRecord | ||
class RecordInvalid | ||
def self.graphql_error(error) | ||
record = error.record | ||
errors = record.errors.map do |attribute, message| | ||
{ | ||
code: 'ValidationError', | ||
message: record.errors.full_message(attribute, message), | ||
path: ['attributes', attribute.to_s.camelize(:lower)] | ||
} | ||
end | ||
|
||
{ | ||
errors: errors | ||
} | ||
end | ||
end | ||
end | ||
end |