Class: GraphqlDevise::SchemaPlugin

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_devise/schema_plugin.rb

Constant Summary collapse

INTROSPECTION_FIELDS =

NOTE: Based on GQL-Ruby docs graphql-ruby.org/schema/introspection.html

['__schema', '__type', '__typename']
DEFAULT_NOT_AUTHENTICATED =
->(field) { raise GraphqlDevise::AuthenticationError, "#{field} field requires authentication" }

Instance Method Summary collapse

Constructor Details

#initialize(query: nil, mutation: nil, authenticate_default: true, public_introspection: !Rails.env.production?,, resource_loaders: [], unauthenticated_proc: DEFAULT_NOT_AUTHENTICATED) ⇒ SchemaPlugin

Returns a new instance of SchemaPlugin.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/graphql_devise/schema_plugin.rb', line 9

def initialize(query: nil, mutation: nil, authenticate_default: true, public_introspection: !Rails.env.production?, resource_loaders: [], unauthenticated_proc: DEFAULT_NOT_AUTHENTICATED)
  @query                = query
  @mutation             = mutation
  @resource_loaders     = resource_loaders
  @authenticate_default = authenticate_default
  @public_introspection = public_introspection
  @unauthenticated_proc = unauthenticated_proc

  # Must happen on initialize so operations are loaded before the types are added to the schema on GQL < 1.10
  load_fields
end

Instance Method Details

#trace(event, trace_data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql_devise/schema_plugin.rb', line 25

def trace(event, trace_data)
  # Authenticate only root level queries
  return yield unless event == 'execute_field' && path(trace_data).count == 1

  field         = traced_field(trace_data)
  auth_required = authenticate_option(field, trace_data)
  context       = context_from_data(trace_data)

  if context.key?(:resource_name)
    ActiveSupport::Deprecation.warn(<<-DEPRECATION.strip_heredoc, caller)
      Providing `resource_name` as part of the GQL context, or doing so by using the `graphql_context(resource_name)`
      method on your controller is deprecated and will be removed in a future version of this gem.
      Please use `gql_devise_context` in you controller instead.

      EXAMPLE
      include GraphqlDevise::Concerns::SetUserByToken

      DummySchema.execute(params[:query], context: gql_devise_context(User))
      DummySchema.execute(params[:query], context: gql_devise_context(User, Admin))
    DEPRECATION
  end

  if auth_required && !(public_introspection && introspection_field?(field))
    context = set_current_resource(context)
    raise_on_missing_resource(context, field, auth_required)
  end

  yield
end

#use(schema_definition) ⇒ Object



21
22
23
# File 'lib/graphql_devise/schema_plugin.rb', line 21

def use(schema_definition)
  schema_definition.tracer(self)
end