Class: GraphQL::Execution::PrepareObjectStep

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/execution/prepare_object_step.rb

Instance Method Summary collapse

Constructor Details

#initialize(object:, runner:, graphql_result:, key:, is_non_null:, field_resolve_step:, next_objects:, next_results:, is_from_array:) ⇒ PrepareObjectStep

Returns a new instance of PrepareObjectStep.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/graphql/execution/prepare_object_step.rb', line 7

def initialize(object:, runner:, graphql_result:, key:, is_non_null:, field_resolve_step:, next_objects:, next_results:, is_from_array:)
  @object = object
  @runner = runner
  @field_resolve_step = field_resolve_step
  @is_non_null = is_non_null
  @next_objects = next_objects
  @next_results = next_results
  @graphql_result = graphql_result
  @resolved_type = nil
  @authorized_value = nil
  @authorization_error = nil
  @execution_error = nil
  @key = key
  @next_step = :resolve_type
  @is_from_array = is_from_array
end

Instance Method Details

#add_field_error(err) ⇒ Object



84
85
86
# File 'lib/graphql/execution/prepare_object_step.rb', line 84

def add_field_error(err)
  @field_resolve_step.add_graphql_error(@graphql_result, @key, err)
end

#authorize ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/graphql/execution/prepare_object_step.rb', line 88

def authorize
  if @field_resolve_step.was_scoped && !@resolved_type.reauthorize_scoped_objects
    @authorized_value = @object
    create_result
    return
  end

  query = @field_resolve_step.selections_step.query
  begin
    query.current_trace.begin_authorized(@resolved_type, @object, query.context)
    @authorized_value = @resolved_type.authorized?(@object, query.context)
    query.current_trace.end_authorized(@resolved_type, @object, query.context, @authorized_value)
  rescue StandardError => err
    @authorized_value = nil
    handle_authorization_error(err, query)
  end

  if @runner.resolves_lazies && @runner.lazy?(@authorized_value)
    @runner.dataloader.lazy_at_depth(@field_resolve_step.path.size, self)
    @next_step = :create_result
  else
    create_result
  end
end

#call ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/graphql/execution/prepare_object_step.rb', line 52

def call
  @field_resolve_step.set_current_field
  case @next_step
  when :resolve_type
    static_type = @field_resolve_step.static_type
    if static_type.kind.abstract?
      query = @field_resolve_step.selections_step.query
      @resolved_type, new_value = ResolveTypeStep.resolve_type(static_type, @object, query)
      if new_value
        ResolveTypeStep.assert_valid_resolved_type(static_type, @resolved_type, new_value, @field_resolve_step)
        @object = new_value
      end
    else
      @resolved_type = static_type
    end
    if @runner.resolves_lazies && @runner.lazy?(@resolved_type)
      @next_step = :authorize
      @runner.dataloader.lazy_at_depth(@field_resolve_step.path.size, self)
    else
      authorize
    end
  when :authorize
    authorize
  when :create_result
    create_result
  else
    raise ArgumentError, "This is a bug, unknown step: #{@next_step.inspect}"
  end
ensure
  @field_resolve_step.set_current_field(nil)
end

#create_result ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/graphql/execution/prepare_object_step.rb', line 113

def create_result
  if @execution_error
    # `.authorized?` raised (or its lazy value raised) an error other than UnauthorizedError:
    # it isn't an authorization decision, so don't call `unauthorized_object`.
    @graphql_result[@key] = add_field_error(@execution_error)
  elsif !@authorized_value
    @authorization_error ||= GraphQL::UnauthorizedError.new(object: @object, type: @resolved_type, context: @field_resolve_step.selections_step.query.context)
  end

  if @authorization_error
    begin
      new_obj = @runner.schema.unauthorized_object(@authorization_error)
      if new_obj
        @authorized_value = true
        @object = new_obj
      elsif @is_non_null
        @graphql_result[@key] = @field_resolve_step.add_non_null_error(@is_from_array)
      else
        @graphql_result[@key] = add_field_error(@authorization_error)
      end
    rescue GraphQL::RuntimeError => err
      # Like the classic runtime, report the raised error itself; add_graphql_error propagates the null for non-null types.
      @graphql_result[@key] = add_field_error(err)
    end
  end

  if @authorized_value
    next_result_h = {}
    @next_results << next_result_h
    @next_objects << @object
    @graphql_result[@key] = next_result_h
    @runner.runtime_type_at[next_result_h] = @resolved_type
    @runner.static_type_at[next_result_h] = @field_resolve_step.static_type
  end

  @field_resolve_step.authorized_finished(self)
end

#value ⇒ Object



24
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
# File 'lib/graphql/execution/prepare_object_step.rb', line 24

def value
  @field_resolve_step.set_current_field
  if @authorized_value
    query = @field_resolve_step.selections_step.query
    query.current_trace.begin_authorized(@resolved_type, @object, query.context)
    begin
      @authorized_value = @runner.schema.sync_lazy(@authorized_value)
    rescue StandardError => err
      @authorized_value = nil
      handle_authorization_error(err, query)
    end
    query.current_trace.end_authorized(@resolved_type, @object, query.context, @authorized_value)
  elsif @resolved_type
    ctx = @field_resolve_step.selections_step.query.context
    st = @field_resolve_step.static_type
    ctx.query.current_trace.begin_resolve_type(st, @object, ctx)
    @resolved_type, new_value = @field_resolve_step.sync(@resolved_type)
    ResolveTypeStep.assert_valid_resolved_type(st, @resolved_type, new_value, @field_resolve_step)
    if new_value
      @object = new_value
    end
    ctx.query.current_trace.end_resolve_type(st, @object, ctx, @resolved_type)
  end
  @runner.add_step(self)
ensure
  @field_resolve_step.set_current_field(nil)
end