Method: GraphQL::Query#initialize
- Defined in:
- lib/graphql/query.rb
#initialize(schema, query_string = nil, document: nil, context: nil, variables: {}, validate: true, operation_name: nil, root_value: nil, max_depth: nil, max_complexity: nil, except: nil, only: nil) ⇒ Query
Prepare query query_string on schema
43 44 45 46 47 48 49 50 51 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 83 84 85 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 |
# File 'lib/graphql/query.rb', line 43 def initialize(schema, query_string = nil, document: nil, context: nil, variables: {}, validate: true, operation_name: nil, root_value: nil, max_depth: nil, max_complexity: nil, except: nil, only: nil) fail ArgumentError, "a query string or document is required" unless query_string || document @schema = schema mask = GraphQL::Schema::Mask.combine(schema.default_mask, except: except, only: only) @context = Context.new(query: self, values: context) @warden = GraphQL::Schema::Warden.new(mask, schema: @schema, context: @context) @root_value = root_value @fragments = {} @operations = {} 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 parse_error = nil @document = document || begin GraphQL.parse(query_string) rescue GraphQL::ParseError => err parse_error = err @schema.parse_error(err, @context) nil end @document && @document.definitions.each do |part| case part when GraphQL::Language::Nodes::FragmentDefinition @fragments[part.name] = part when GraphQL::Language::Nodes::OperationDefinition @operations[part.name] = part end end @resolved_types_cache = Hash.new { |h, k| h[k] = @schema.resolve_type(k, @context) } @arguments_cache = ArgumentsCache.build(self) # Trying to execute a document # with no operations returns an empty hash @ast_variables = [] @mutation = false operation_name_error = nil @operation_name = nil if @operations.any? selected_operation = find_operation(@operations, operation_name) if selected_operation.nil? operation_name_error = GraphQL::Query::OperationNameMissingError.new(operation_name) else @operation_name = selected_operation.name @ast_variables = selected_operation.variables @mutation = selected_operation.operation_type == "mutation" @selected_operation = selected_operation end end @validation_pipeline = GraphQL::Query::ValidationPipeline.new( query: self, parse_error: parse_error, operation_name_error: operation_name_error, max_depth: max_depth || schema.max_depth, max_complexity: max_complexity || schema.max_complexity, ) @result = nil @executed = false end |