Skip to content

Commit

Permalink
handle not found errors. #268
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 10, 2019
1 parent 9059b8d commit 0098e94
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
20 changes: 19 additions & 1 deletion app/graphql/lupo_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,23 @@ class LupoSchema < GraphQL::Schema
max_depth 5

# mutation(Types::MutationType)
query(Types::QueryType)
query(QueryType)
end

GraphQL::Errors.configure(LupoSchema) do
rescue_from ActiveRecord::RecordNotFound do |exception|
GraphQL::ExecutionError.new("Record not found")
end

rescue_from ActiveRecord::RecordInvalid do |exception|
GraphQL::ExecutionError.new(exception.record.errors.full_messages.join("\n"))
end

# rescue_from StandardError do |exception|
# GraphQL::ExecutionError.new("Please try to execute the query for this field later")
# end

# rescue_from CustomError do |exception, object, arguments, context|
# nil
# end
end
68 changes: 36 additions & 32 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,21 @@ def funders(query: nil, first: nil)
end

def funder(id:)
Funder.find_by_id(id).first
result = Funder.find_by_id(id).first
fail ActiveRecord::RecordNotFound if result.nil?

result
end

field :researcher, ResearcherType, null: false do
argument :id, ID, required: true
end

def researcher(id:)
Researcher.find_by_id(id).first
result = Researcher.find_by_id(id).first
fail ActiveRecord::RecordNotFound if result.nil?

result
end

field :organizations, [OrganizationType], null: false do
Expand All @@ -97,7 +103,10 @@ def organizations(query: nil, first: nil)
end

def organization(id:)
Organization.find_by_id(id).first
result = Organization.find_by_id(id).first
fail ActiveRecord::RecordNotFound if result.nil?

result
end

field :datasets, [DatasetType], null: false do
Expand All @@ -114,8 +123,7 @@ def datasets(query: nil, first: nil)
end

def dataset(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :publications, [PublicationType], null: false do
Expand All @@ -132,8 +140,7 @@ def publications(query: nil, first: nil)
end

def publication(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :audiovisuals, [AudiovisualType], null: false do
Expand All @@ -150,8 +157,7 @@ def audiovisuals(query: nil, first: nil)
end

def audiovisual(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :collections, [CollectionType], null: false do
Expand All @@ -168,8 +174,7 @@ def collections(query: nil, first: nil)
end

def collection(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :data_papers, [DataPaperType], null: false do
Expand All @@ -186,8 +191,7 @@ def data_papers(query: nil, first: nil)
end

def data_paper(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :events, [EventType], null: false do
Expand All @@ -204,8 +208,7 @@ def events(query: nil, first: nil)
end

def event(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :images, [ImageType], null: false do
Expand All @@ -222,8 +225,7 @@ def images(query: nil, first: nil)
end

def image(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :interactive_resources, [InteractiveResourceType], null: false do
Expand All @@ -240,8 +242,7 @@ def interactive_resources(query: nil, first: nil)
end

def interactive_resource(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :models, [ModelType], null: false do
Expand All @@ -258,8 +259,7 @@ def models(query: nil, first: nil)
end

def model(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :physical_objects, [PhysicalObjectType], null: false do
Expand All @@ -276,8 +276,7 @@ def physical_objects(query: nil, first: nil)
end

def physical_object(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :services, [ServiceType], null: false do
Expand All @@ -294,8 +293,7 @@ def services(query: nil, first: nil)
end

def service(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :softwares, [SoftwareType], null: false do
Expand All @@ -312,8 +310,7 @@ def softwares(query: nil, first: nil)
end

def software(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :sounds, [SoundType], null: false do
Expand All @@ -330,8 +327,7 @@ def sounds(query: nil, first: nil)
end

def sound(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :workflows, [WorkflowType], null: false do
Expand All @@ -348,8 +344,7 @@ def workflows(query: nil, first: nil)
end

def workflow(id:)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
set_doi(id)
end

field :others, [OtherType], null: false do
Expand All @@ -366,8 +361,17 @@ def others(query: nil, first: nil)
end

def other(id:)
set_doi(id)
end

def set_doi(id)
doi = doi_from_url(id)
Doi.find_by_id(doi).first
fail ActiveRecord::RecordNotFound if doi.nil?

result = Doi.find_by_id(doi).first
fail ActiveRecord::RecordNotFound if result.nil?

result
end

def doi_from_url(url)
Expand Down

0 comments on commit 0098e94

Please sign in to comment.