Module: GraphqlRoleRestrict

Defined in:
lib/graphql_role_restrict.rb,
lib/graphql_role_restrict/version.rb

Overview

This is based on the #visible? method described in graphql-ruby.org/authorization/visibility.html

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#context_role_valid?(context, role_restrict) ⇒ Boolean

Check the validatity of the the role_restrict value against an object representing the current_user stored in context (injection of that value has to be done at the controller level before accessing graphql code)

Method NEEDS to be Overwritten once the module is included in your Argument/Field class to allow custom logic

Parameters:

  • context (GraphQL::Query::Context)

    Graphql context object

  • role_restrict (Any)

    Any value passed in to a role_restrict option of a field / argument

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/graphql_role_restrict.rb', line 37

def context_role_valid?(context, role_restrict)
  case role_restrict
  when :logged_in
    !!context[:current_user]
  else
    true
  end
end

#initialize(*args, role_restrict: :logged_in, **kwargs, &block) ⇒ Object

register a new option `role_restrict`



7
8
9
10
11
12
# File 'lib/graphql_role_restrict.rb', line 7

def initialize(*args, role_restrict: :logged_in, **kwargs, &block)
  @role_restrict = role_restrict

  # Pass on the default args:
  super(*args, **kwargs, &block)
end

#to_graphqlObject



14
15
16
17
18
# File 'lib/graphql_role_restrict.rb', line 14

def to_graphql
  field_defn = super # Returns a GraphQL::Field
  field_defn.[:role_restrict] = @role_restrict
  field_defn
end

#visible?(context) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/graphql_role_restrict.rb', line 20

def visible?(context)
  super && context_role_valid?(context, @role_restrict)
end