Skip to content

How to create a custom schema directive for permission? #4707

Answered by rmosolgo
gbrlsnchs asked this question in Q&A
Discussion options

You must be logged in to vote

Hi! Great question. After setting up directive configuration, you can implement authorized? methods which check the given context against the schema member's permission configuration. For example, in a type:

class MyType < GraphQL::Schema::Object
  def self.authorized?(obj, ctx)
    if (dir = self.directives.find { |d| d.is_a?(Directives::Permission) })
      # somehow check the configured permission level against this query's permission level:
      YourApp.permitted?(context[:permission_level], dir.arguments[:level])
    else 
      true # no config 
    end 
  end 
end 

You could speed this up by having the directive also assign instance variables, see the gem's deprecation_reason han…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by gbrlsnchs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants