Class: GraphQL::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Tracing::Traceable
Defined in:
lib/graphql/query.rb,
lib/graphql/query/result.rb,
lib/graphql/query/context.rb,
lib/graphql/query/variables.rb,
lib/graphql/query/fingerprint.rb,
lib/graphql/query/null_context.rb,
lib/graphql/query/validation_pipeline.rb,
lib/graphql/query/input_validation_result.rb,
lib/graphql/query/variable_validation_error.rb

Overview

A combination of query string and Schema instance which can be reduced to a #result.

Defined Under Namespace

Modules: Fingerprint Classes: Context, InputValidationResult, NullContext, OperationNameMissingError, Result, ValidationPipeline, VariableValidationError, Variables

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tracing::Traceable

#trace

Constructor Details

#initialize(schema, query_string = nil, query: nil, document: nil, context: nil, variables: nil, validate: true, subscription_topic: nil, operation_name: nil, root_value: nil, max_depth: schema.max_depth, max_complexity: schema.max_complexity, except: nil, only: nil, warden: nil) ⇒ Query

Prepare query query_string on schema

Parameters:

  • schema (GraphQL::Schema)
  • query_string (String) (defaults to: nil)
  • context (#[]) (defaults to: nil)

    an arbitrary hash of values which you can access in Field#resolve

  • variables (Hash) (defaults to: nil)

    values for $variables in the query

  • operation_name (String) (defaults to: nil)

    if the query string contains many operations, this is the one which should be executed

  • root_value (Object) (defaults to: nil)

    the object used to resolve fields on the root type

  • max_depth (Numeric) (defaults to: schema.max_depth)

    the maximum number of nested selections allowed for this query (falls back to schema-level value)

  • max_complexity (Numeric) (defaults to: schema.max_complexity)

    the maximum field complexity for this query (falls back to schema-level value)

  • except (<#call(schema_member, context)>) (defaults to: nil)

    If provided, objects will be hidden from the schema when .call(schema_member, context) returns truthy

  • only (<#call(schema_member, context)>) (defaults to: nil)

    If provided, objects will be hidden from the schema when .call(schema_member, context) returns false



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
150
151
152
153
154
155
# File 'lib/graphql/query.rb', line 86

def initialize(schema, query_string = nil, query: nil, document: nil, context: nil, variables: nil, validate: true, subscription_topic: nil, operation_name: nil, root_value: nil, max_depth: schema.max_depth, max_complexity: schema.max_complexity, except: nil, only: nil, warden: nil)
  # Even if `variables: nil` is passed, use an empty hash for simpler logic
  variables ||= {}
  @schema = schema
  @filter = schema.default_filter.merge(except: except, only: only)
  @context = schema.context_class.new(query: self, object: root_value, values: context)
  @warden = warden
  @subscription_topic = subscription_topic
  @root_value = root_value
  @fragments = nil
  @operations = nil
  @validate = validate
  context_tracers = (context ? context.fetch(:tracers, []) : [])
  @tracers = schema.tracers + context_tracers

  # Support `ctx[:backtrace] = true` for wrapping backtraces
  if context && context[:backtrace] && !@tracers.include?(GraphQL::Backtrace::Tracer)
    context_tracers += [GraphQL::Backtrace::Tracer]
    @tracers << GraphQL::Backtrace::Tracer
  end

  if context_tracers.any? && !(schema.trace_class <= GraphQL::Tracing::LegacyTrace)
    raise ArgumentError, "context[:tracers] and context[:backtrace] are not supported without `tracer_class(GraphQL::Tracing::LegacyTrace)` in the schema configuration, please add it."
  end


  @analysis_errors = []
  if variables.is_a?(String)
    raise ArgumentError, "Query variables should be a Hash, not a String. Try JSON.parse to prepare variables."
  else
    @provided_variables = variables || {}
  end

  @query_string = query_string || query
  @document = document

  if @query_string && @document
    raise ArgumentError, "Query should only be provided a query string or a document, not both."
  end

  if @query_string && !@query_string.is_a?(String)
    raise ArgumentError, "Query string argument should be a String, got #{@query_string.class.name} instead."
  end

  # A two-layer cache of type resolution:
  # { abstract_type => { value => resolved_type } }
  @resolved_types_cache = Hash.new do |h1, k1|
    h1[k1] = Hash.new do |h2, k2|
      h2[k2] = @schema.resolve_type(k1, k2, @context)
    end
  end

  # Trying to execute a document
  # with no operations returns an empty hash
  @ast_variables = []
  @mutation = false
  @operation_name = operation_name
  @prepared_ast = false
  @validation_pipeline = nil
  @max_depth = max_depth
  @max_complexity = max_complexity

  @result_values = nil
  @executed = false

  # TODO add a general way to define schema-level filters
  if @schema.respond_to?(:visible?)
    merge_filters(only: @schema.method(:visible?))
  end
end

Instance Attribute Details

#analysis_errorsObject

Returns the value of attribute analysis_errors.



306
307
308
# File 'lib/graphql/query.rb', line 306

def analysis_errors
  @analysis_errors
end

#contextObject (readonly)

Returns the value of attribute context.



28
29
30
# File 'lib/graphql/query.rb', line 28

def context
  @context
end

#multiplexObject

Returns the value of attribute multiplex.



166
167
168
# File 'lib/graphql/query.rb', line 166

def multiplex
  @multiplex
end

#operation_namenil, String

Returns The operation name provided by client or the one inferred from the document. Used to determine which operation to run.

Returns:

  • (nil, String)

    The operation name provided by client or the one inferred from the document. Used to determine which operation to run.



34
35
36
# File 'lib/graphql/query.rb', line 34

def operation_name
  @operation_name
end

#provided_variablesObject (readonly)

Returns the value of attribute provided_variables.



28
29
30
# File 'lib/graphql/query.rb', line 28

def provided_variables
  @provided_variables
end

#query_stringObject

If a document was provided to GraphQL::Schema#execute instead of the raw query string, we will need to get it from the document



158
159
160
# File 'lib/graphql/query.rb', line 158

def query_string
  @query_string ||= (document ? document.to_query_string : nil)
end

#result_valuesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



198
199
200
# File 'lib/graphql/query.rb', line 198

def result_values
  @result_values
end

#root_valueObject

The value for root types



31
32
33
# File 'lib/graphql/query.rb', line 31

def root_value
  @root_value
end

#schemaObject (readonly)

Returns the value of attribute schema.



28
29
30
# File 'lib/graphql/query.rb', line 28

def schema
  @schema
end

#subscription_topicString? (readonly)

Returns the triggered event, if this query is a subscription update.

Returns:

  • (String, nil)

    the triggered event, if this query is a subscription update



71
72
73
# File 'lib/graphql/query.rb', line 71

def subscription_topic
  @subscription_topic
end

#tracersObject (readonly)

Returns the value of attribute tracers.



73
74
75
# File 'lib/graphql/query.rb', line 73

def tracers
  @tracers
end

#validateBoolean

Returns if false, static validation is skipped (execution behavior for invalid queries is undefined).

Returns:

  • (Boolean)

    if false, static validation is skipped (execution behavior for invalid queries is undefined)



37
38
39
# File 'lib/graphql/query.rb', line 37

def validate
  @validate
end

Instance Method Details

#arguments_cacheObject



259
260
261
# File 'lib/graphql/query.rb', line 259

def arguments_cache
  @arguments_cache ||= Execution::Interpreter::ArgumentsCache.new(self)
end

#arguments_for(ast_node, definition, parent_object: nil) ⇒ Object

Node-level cache for calculating arguments. Used during execution and query analysis.

Parameters:



255
256
257
# File 'lib/graphql/query.rb', line 255

def arguments_for(ast_node, definition, parent_object: nil)
  arguments_cache.fetch(ast_node, definition, parent_object)
end

#current_traceGraphQL::Tracing::Trace



169
170
171
# File 'lib/graphql/query.rb', line 169

def current_trace
  @current_trace ||= multiplex ? multiplex.current_trace : schema.new_trace(multiplex: multiplex, query: self)
end

#documentGraphQL::Language::Nodes::Document



51
52
53
54
55
56
57
58
# File 'lib/graphql/query.rb', line 51

def document
  # It's ok if this hasn't been assigned yet
  if @query_string || @document
    with_prepared_ast { @document }
  else
    nil
  end
end

#executed?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/graphql/query.rb', line 217

def executed?
  @executed
end

#fingerprintString

This contains a few components:

  • The selected operation name (or anonymous)
  • The fingerprint of the query string
  • The number of given variables (for readability)
  • The fingerprint of the given variables

This fingerprint can be used to track runs of the same operation-variables combination over time.

Returns:

  • (String)

    An opaque hash identifying this operation-variables combination

See Also:



285
286
287
# File 'lib/graphql/query.rb', line 285

def fingerprint
  @fingerprint ||= "#{operation_fingerprint}/#{variables_fingerprint}"
end

#fragmentsObject



200
201
202
# File 'lib/graphql/query.rb', line 200

def fragments
  with_prepared_ast { @fragments }
end

#handle_or_reraise(err) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



356
357
358
# File 'lib/graphql/query.rb', line 356

def handle_or_reraise(err)
  schema.handle_or_reraise(context, err)
end

#inspectObject



60
61
62
# File 'lib/graphql/query.rb', line 60

def inspect
  "query ..."
end

#interpreter?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/graphql/query.rb', line 162

def interpreter?
  true
end

#lookaheadGraphQL::Execution::Lookahead

A lookahead for the root selections of this query



179
180
181
182
183
184
185
# File 'lib/graphql/query.rb', line 179

def lookahead
  @lookahead ||= begin
    ast_node = selected_operation
    root_type = warden.root_type_for_operation(ast_node.operation_type || "query")
    GraphQL::Execution::Lookahead.new(query: self, root_type: root_type, ast_nodes: [ast_node])
  end
end

#merge_filters(only: nil, except: nil) ⇒ void

This method returns an undefined value.



342
343
344
345
346
347
348
349
# File 'lib/graphql/query.rb', line 342

def merge_filters(only: nil, except: nil)
  if @prepared_ast
    raise "Can't add filters after preparing the query"
  else
    @filter = @filter.merge(only: only, except: except)
  end
  nil
end

#mutation?Boolean

Returns:

  • (Boolean)


333
334
335
# File 'lib/graphql/query.rb', line 333

def mutation?
  with_prepared_ast { @mutation }
end

#operation_fingerprintString

Returns An opaque hash for identifying this query’s given query string and selected operation.

Returns:

  • (String)

    An opaque hash for identifying this query’s given query string and selected operation



290
291
292
# File 'lib/graphql/query.rb', line 290

def operation_fingerprint
  @operation_fingerprint ||= "#{selected_operation_name || "anonymous"}/#{Fingerprint.generate(query_string)}"
end

#operationsObject



204
205
206
# File 'lib/graphql/query.rb', line 204

def operations
  with_prepared_ast { @operations }
end

#query?Boolean

Returns:

  • (Boolean)


337
338
339
# File 'lib/graphql/query.rb', line 337

def query?
  with_prepared_ast { @query }
end

#resolve_type(abstract_type, value = :__undefined__) ⇒ GraphQL::ObjectType?

Returns The runtime type of value from Schema.resolve_type.

Parameters:

  • abstract_type (GraphQL::UnionType, GraphQL::InterfaceType)
  • value (Object) (defaults to: :__undefined__)

    Any runtime value

Returns:

See Also:

  • to apply filtering from `only` / `except`


321
322
323
324
325
326
327
328
329
330
331
# File 'lib/graphql/query.rb', line 321

def resolve_type(abstract_type, value = :__undefined__)
  if value.is_a?(Symbol) && value == :__undefined__
    # Old method signature
    value = abstract_type
    abstract_type = nil
  end
  if value.is_a?(GraphQL::Schema::Object)
    value = value.object
  end
  @resolved_types_cache[abstract_type][value]
end

#resultHash

Get the result for this query, executing it once

Returns:

  • (Hash)

    A GraphQL response, with "data" and/or "errors" keys



210
211
212
213
214
215
# File 'lib/graphql/query.rb', line 210

def result
  if !@executed
    Execution::Interpreter.run_all(@schema, [self], context: @context)
  end
  @result ||= Query::Result.new(query: self, values: @result_values)
end

#sanitized_query_string(inline_variables: true) ⇒ String?

A version of the given query string, with: - Variables inlined to the query - Strings replaced with <REDACTED>

Returns:

  • (String, nil)

    Returns nil if the query is invalid.



267
268
269
270
271
# File 'lib/graphql/query.rb', line 267

def sanitized_query_string(inline_variables: true)
  with_prepared_ast {
    schema.sanitized_printer.new(self, inline_variables: inline_variables).sanitized_query_string
  }
end

#selected_operationGraphQL::Language::Nodes::OperationDefinition?

This is the operation to run for this query. If more than one operation is present, it must be named at runtime.



228
229
230
# File 'lib/graphql/query.rb', line 228

def selected_operation
  with_prepared_ast { @selected_operation }
end

#selected_operation_nameString?

Returns The name of the operation to run (may be inferred).

Returns:

  • (String, nil)

    The name of the operation to run (may be inferred)



65
66
67
68
# File 'lib/graphql/query.rb', line 65

def selected_operation_name
  return nil unless selected_operation
  selected_operation.name
end

#static_errorsObject



221
222
223
# File 'lib/graphql/query.rb', line 221

def static_errors
  validation_errors + analysis_errors + context.errors
end

#subscription?Boolean

Returns:

  • (Boolean)


351
352
353
# File 'lib/graphql/query.rb', line 351

def subscription?
  with_prepared_ast { @subscription }
end

#subscription_update?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/graphql/query.rb', line 173

def subscription_update?
  @subscription_topic && subscription?
end

#valid?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/graphql/query.rb', line 307

def valid?
  validation_pipeline.valid? && analysis_errors.empty?
end

#validation_pipelineObject



299
300
301
# File 'lib/graphql/query.rb', line 299

def validation_pipeline
  with_prepared_ast { @validation_pipeline }
end

#variablesGraphQL::Query::Variables

Determine the values for variables of this query, using default values if a value isn’t provided at runtime.

If some variable is invalid, errors are added to #validation_errors.

Returns:



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/graphql/query.rb', line 238

def variables
  @variables ||= begin
    with_prepared_ast {
      GraphQL::Query::Variables.new(
        @context,
        @ast_variables,
        @provided_variables,
      )
    }
  end
end

#variables_fingerprintString

Returns An opaque hash for identifying this query’s given a variable values (not including defaults).

Returns:

  • (String)

    An opaque hash for identifying this query’s given a variable values (not including defaults)



295
296
297
# File 'lib/graphql/query.rb', line 295

def variables_fingerprint
  @variables_fingerprint ||= "#{provided_variables.size}/#{Fingerprint.generate(provided_variables.to_json)}"
end

#wardenObject



311
312
313
# File 'lib/graphql/query.rb', line 311

def warden
  with_prepared_ast { @warden }
end