4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GraphQL-Rubyのunauthorized_objectの処理をTypeに移す ほか

Last updated at Posted at 2018-12-14

unauthorized_objectについて

GraphQL-Rubyにはresolveしたオブジェクトへの参照権限があるのかのチェック、ない場合の処理を実装することができます

以下のようにType側に実装し、

class Types::SomeType < Types::BaseObject
  ...
  def self.authorized?(object, context)
    super && object.published?
  end
end

Schemaに定義したunauthorized_objectでエラーハンドリングが行なえます

class Schema < GraphQL::Schema
  ...
  def self.unauthorized_object(error)
    # raise or return some object
  end
end

Type側でエラーハンドリングをしたい

できればunauthorizedの処理はType側にまとめたいところです
unauthorized_objectに渡るerrortypeを持っているので
以下のように実装することでType側に処理を移すことができます。

# Schema
def self.unauthorized_object(error)
  if error.type.respond_to? :unauthorized_object
    error.type.unauthorized_object(error)
  end
end

# SomeType
def self.unauthorized_object(error)
  # エラーハンドリング
end

データをマスクする

error.objectでresolveしたオブジェクトを取得することができるので公開したくないフィールドを空文字やnilに返ることができます

def self.unauthorized_object(error)
  object = error.object
  object.some_hidden_content = nil
  object
end
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?