在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):exAspArk/graphql-errors开源软件地址(OpenSource Url):https://github.com/exAspArk/graphql-errors开源编程语言(OpenSource Language):Ruby 98.5%开源软件介绍(OpenSource Introduction):graphql-errors
This gem provides a simple error handling for graphql-ruby. Highlights
UsageOnce you defined your GraphQL schema: Schema = GraphQL::Schema.define do
query QueryType
end You can add GraphQL::Errors.configure(Schema) do
rescue_from ActiveRecord::RecordNotFound do |exception|
nil
end
rescue_from ActiveRecord::RecordInvalid do |exception|
GraphQL::ExecutionError.new(exception.record.errors.full_messages.join("\n"))
end
# uses Module to handle several similar errors with single rescue_from
rescue_from MyNetworkErrors do |_|
GraphQL::ExecutionError.new("Don't mind, just retry the mutation")
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|
error = GraphQL::ExecutionError.new("Error found!")
firstError.path = context.path + ["myError"]
context.add_error(firstError)
end
end It'll handle exceptions raised from each resolver in the schema: QueryType = GraphQL::ObjectType.define do
name "Query"
field :post, PostType do
argument :id, !types.ID
resolve ->(obj, args, ctx) { Post.find(args['id']) } # <= will raise ActiveRecord::RecordNotFound
end
end
Schema.execute('query { post(id: "1") { title } }') # handles the error without failing the whole query
# => { data: { post: nil } } InstallationAdd this line to your application's Gemfile: gem 'graphql-errors' And then execute:
Or install it yourself as:
DevelopmentAfter checking out the repo, run To install this gem onto your local machine, run ContributingBug reports and pull requests are welcome on GitHub at https://github.com/exAspArk/graphql-errors. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct. LicenseThe gem is available as open source under the terms of the MIT License. Code of ConductEveryone interacting in the Graphql::Errors project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论