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 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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/graphql_devise/schema_plugin.rb', line 34

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 auth_required && !(public_introspection && introspection_field?(field))
    raise_on_missing_resource(context, field, auth_required)
  end

  yield
end

#use(schema_definition) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphql_devise/schema_plugin.rb', line 21

def use(schema_definition)
  if Gem::Version.new(GraphQL::VERSION) >= Gem::Version.new('2.3')
    schema_definition.trace_with(
      FieldAuthTracer,
      authenticate_default: @authenticate_default,
      public_introspection: public_introspection,
      unauthenticated_proc: @unauthenticated_proc
    )
  else
    schema_definition.tracer(self)
  end
end