Class: ElasticGraph::GraphQL

Inherits:
Object
  • Object
show all
Extended by:
Support::FromYamlFile
Defined in:
lib/elastic_graph/graphql.rb,
lib/elastic_graph/graphql/client.rb,
lib/elastic_graph/graphql/config.rb,
lib/elastic_graph/graphql/schema.rb,
lib/elastic_graph/graphql/schema/type.rb,
lib/elastic_graph/graphql/schema/field.rb,
lib/elastic_graph/graphql/http_endpoint.rb,
lib/elastic_graph/graphql/decoded_cursor.rb,
lib/elastic_graph/graphql/query_executor.rb,
lib/elastic_graph/graphql/aggregation/key.rb,
lib/elastic_graph/graphql/datastore_query.rb,
lib/elastic_graph/graphql/resolvers/object.rb,
lib/elastic_graph/graphql/schema/arguments.rb,
lib/elastic_graph/graphql/aggregation/query.rb,
lib/elastic_graph/graphql/schema/enum_value.rb,
lib/elastic_graph/graphql/query_adapter/sort.rb,
lib/elastic_graph/graphql/filtering/field_path.rb,
lib/elastic_graph/graphql/schema/relation_join.rb,
lib/elastic_graph/graphql/filtering/range_query.rb,
lib/elastic_graph/graphql/query_adapter/filters.rb,
lib/elastic_graph/graphql/query_details_tracker.rb,
lib/elastic_graph/graphql/resolvers/query_source.rb,
lib/elastic_graph/graphql/aggregation/computation.rb,
lib/elastic_graph/graphql/datastore_search_router.rb,
lib/elastic_graph/graphql/filtering/boolean_query.rb,
lib/elastic_graph/graphql/resolvers/query_adapter.rb,
lib/elastic_graph/graphql/aggregation/path_segment.rb,
lib/elastic_graph/graphql/query_adapter/pagination.rb,
lib/elastic_graph/graphql/aggregation/query_adapter.rb,
lib/elastic_graph/graphql/aggregation/term_grouping.rb,
lib/elastic_graph/graphql/datastore_query/paginator.rb,
lib/elastic_graph/graphql/aggregation/resolvers/node.rb,
lib/elastic_graph/graphql/resolvers/relay_connection.rb,
lib/elastic_graph/graphql/resolvers/resolvable_value.rb,
lib/elastic_graph/graphql/aggregation/query_optimizer.rb,
lib/elastic_graph/graphql/datastore_response/document.rb,
lib/elastic_graph/graphql/filtering/filter_interpreter.rb,
lib/elastic_graph/graphql/scalar_coercion_adapters/date.rb,
lib/elastic_graph/graphql/aggregation/field_path_encoder.rb,
lib/elastic_graph/graphql/datastore_query/routing_picker.rb,
lib/elastic_graph/graphql/query_adapter/requested_fields.rb,
lib/elastic_graph/graphql/resolvers/nested_relationships.rb,
lib/elastic_graph/graphql/scalar_coercion_adapters/longs.rb,
lib/elastic_graph/graphql/scalar_coercion_adapters/no_op.rb,
lib/elastic_graph/graphql/aggregation/field_term_grouping.rb,
lib/elastic_graph/graphql/scalar_coercion_adapters/cursor.rb,
lib/elastic_graph/graphql/aggregation/resolvers/grouped_by.rb,
lib/elastic_graph/graphql/aggregation/script_term_grouping.rb,
lib/elastic_graph/graphql/filtering/filter_args_translator.rb,
lib/elastic_graph/graphql/resolvers/get_record_field_value.rb,
lib/elastic_graph/graphql/scalar_coercion_adapters/untyped.rb,
lib/elastic_graph/graphql/filtering/filter_node_interpreter.rb,
lib/elastic_graph/graphql/resolvers/graphql_adapter_builder.rb,
lib/elastic_graph/graphql/aggregation/nested_sub_aggregation.rb,
lib/elastic_graph/graphql/aggregation/resolvers/count_detail.rb,
lib/elastic_graph/graphql/datastore_query/document_paginator.rb,
lib/elastic_graph/graphql/datastore_response/search_response.rb,
lib/elastic_graph/graphql/query_adapter/abstract_type_filter.rb,
lib/elastic_graph/graphql/scalar_coercion_adapters/date_time.rb,
lib/elastic_graph/graphql/scalar_coercion_adapters/time_zone.rb,
lib/elastic_graph/graphql/aggregation/date_histogram_grouping.rb,
lib/elastic_graph/graphql/scalar_coercion_adapters/local_time.rb,
lib/elastic_graph/graphql/filtering/filter_value_set_extractor.rb,
lib/elastic_graph/graphql/resolvers/relay_connection/page_info.rb,
lib/elastic_graph/graphql/resolvers/nested_relationships_source.rb,
lib/elastic_graph/graphql/aggregation/composite_grouping_adapter.rb,
lib/elastic_graph/graphql/aggregation/resolvers/sub_aggregations.rb,
lib/elastic_graph/graphql/aggregation/resolvers/aggregated_values.rb,
lib/elastic_graph/graphql/datastore_query/index_expression_builder.rb,
lib/elastic_graph/graphql/resolvers/relay_connection/array_adapter.rb,
lib/elastic_graph/graphql/scalar_coercion_adapters/valid_time_zones.rb,
lib/elastic_graph/graphql/aggregation/non_composite_grouping_adapter.rb,
lib/elastic_graph/graphql/resolvers/relay_connection/generic_adapter.rb,
lib/elastic_graph/graphql/resolvers/indexed_type_root_fields_resolver.rb,
lib/elastic_graph/graphql/aggregation/resolvers/relay_connection_builder.rb,
lib/elastic_graph/graphql/resolvers/relay_connection/search_response_adapter_builder.rb

Overview

Wraps a GraphQL::Schema object in order to provide higher-level, more convenient APIs on top of that. The schema is assumed to be immutable, so this class memoizes many computations it does, ensuring we never need to traverse the schema graph multiple times.

Defined Under Namespace

Modules: Aggregation, DatastoreResponse, Filtering, Resolvers, ScalarCoercionAdapters Classes: Client, Config, DatastoreQuery, DatastoreSearchRouter, DecodedCursor, HTTPEndpoint, HTTPResponse, QueryAdapter, QueryDetailsTracker, QueryExecutor, Schema

Constant Summary collapse

HTTPRequest =

Represents an HTTP request, containing:

  • http_method: a symbol like :get or :post.

  • url: a string containing the full URL.

  • headers: a hash with string keys and values containing HTTP headers. The headers can be in any form like ‘Content-Type`, `content-type`, `CONTENT-TYPE`, `CONTENT_TYPE`, etc.

  • body: a string containing the request body, if there was one.

Support::MemoizableData.define(:http_method, :url, :headers, :body) do
  # @implements HTTPRequest

  # HTTP headers are intended to be case-insensitive, and different Web frameworks treat them differently.
  # For example, Rack uppercases them with `_` in place of `-`.  With AWS Lambda proxy integrations API
  # gateway HTTP APIs, header names are lowercased:
  # https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
  #
  # ...but for integration with API gateway REST APIs, header names are provided as-is:
  # https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
  #
  # To be maximally compatible here, this normalizes to uppercase form with dashes in place of underscores.
  def normalized_headers
    @normalized_headers ||= headers.transform_keys do |key|
      HTTPRequest.normalize_header_name(key)
    end
  end

  def mime_type
    @mime_type ||= normalized_headers["CONTENT-TYPE"].to_s.split(";").first # strip ; delimiter and any trailing parameters
  end

  def self.normalize_header_name(header)
    header.upcase.tr("_", "-")
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, datastore_core:, graphql_adapter: nil, datastore_search_router: nil, filter_interpreter: nil, sub_aggregation_grouping_adapter: nil, monotonic_clock: nil, clock: ::Time) ⇒ GraphQL

Returns a new instance of GraphQL.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/elastic_graph/graphql.rb', line 36

def initialize(
  config:,
  datastore_core:,
  graphql_adapter: nil,
  datastore_search_router: nil,
  filter_interpreter: nil,
  sub_aggregation_grouping_adapter: nil,
  monotonic_clock: nil,
  clock: ::Time
)
  @config = config
  @datastore_core = datastore_core
  @graphql_adapter = graphql_adapter
  @datastore_search_router = datastore_search_router
  @filter_interpreter = filter_interpreter
  @sub_aggregation_grouping_adapter = sub_aggregation_grouping_adapter
  @monotonic_clock = monotonic_clock
  @clock = clock
  @logger = @datastore_core.logger
  @runtime_metadata = @datastore_core.schema_artifacts.
  @graphql_schema_string = @datastore_core.schema_artifacts.graphql_schema_string

  # Apply any extension modules that have been configured.
  @config.extension_modules.each { |mod| extend mod }
  @runtime_metadata.graphql_extension_modules.each { |ext_mod| extend ext_mod.load_extension.extension_class }
end

Instance Attribute Details

#clockObject (readonly)



23
24
25
# File 'lib/elastic_graph/graphql.rb', line 23

def clock
  @clock
end

#configObject (readonly)



23
24
25
# File 'lib/elastic_graph/graphql.rb', line 23

def config
  @config
end

#datastore_coreObject (readonly)



23
24
25
# File 'lib/elastic_graph/graphql.rb', line 23

def datastore_core
  @datastore_core
end

#graphql_schema_stringObject (readonly)



23
24
25
# File 'lib/elastic_graph/graphql.rb', line 23

def graphql_schema_string
  @graphql_schema_string
end

#loggerObject (readonly)



23
24
25
# File 'lib/elastic_graph/graphql.rb', line 23

def logger
  @logger
end

#runtime_metadataObject (readonly)



23
24
25
# File 'lib/elastic_graph/graphql.rb', line 23

def 
  @runtime_metadata
end

Class Method Details

.from_parsed_yaml(parsed_yaml, &datastore_client_customization_block) ⇒ Object

A factory method that builds a GraphQL instance from the given parsed YAML config. ‘from_yaml_file(file_name, &block)` is also available (via `Support::FromYamlFile`).



28
29
30
31
32
33
# File 'lib/elastic_graph/graphql.rb', line 28

def self.from_parsed_yaml(parsed_yaml, &datastore_client_customization_block)
  new(
    config: GraphQL::Config.from_parsed_yaml(parsed_yaml) || GraphQL::Config.new,
    datastore_core: DatastoreCore.from_parsed_yaml(parsed_yaml, &datastore_client_customization_block)
  )
end

Instance Method Details

#datastore_query_adaptersObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/elastic_graph/graphql.rb', line 192

def datastore_query_adapters
  @datastore_query_adapters ||= begin
    require "elastic_graph/graphql/aggregation/query_adapter"
    require "elastic_graph/graphql/query_adapter/abstract_type_filter"
    require "elastic_graph/graphql/query_adapter/filters"
    require "elastic_graph/graphql/query_adapter/pagination"
    require "elastic_graph/graphql/query_adapter/sort"
    require "elastic_graph/graphql/query_adapter/requested_fields"

    schema_element_names = .schema_element_names

    [
      GraphQL::QueryAdapter::AbstractTypeFilter.new(schema_element_names),
      GraphQL::QueryAdapter::Pagination.new(schema_element_names: schema_element_names),
      GraphQL::QueryAdapter::Filters.new(
        schema_element_names: schema_element_names,
        filter_args_translator: filter_args_translator,
        filter_node_interpreter: filter_node_interpreter
      ),
      GraphQL::QueryAdapter::Sort.new(order_by_arg_name: schema_element_names.order_by),
      Aggregation::QueryAdapter::WithoutSchema.new(
        config: config,
        filter_args_translator: filter_args_translator,
        runtime_metadata: ,
        sub_aggregation_grouping_adapter: sub_aggregation_grouping_adapter
      ),
      GraphQL::QueryAdapter::RequestedFields::WithoutSchema.new
    ]
  end
end

#datastore_query_builderObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/elastic_graph/graphql.rb', line 119

def datastore_query_builder
  @datastore_query_builder ||= begin
    require "elastic_graph/graphql/datastore_query"
    DatastoreQuery::Builder.new(
      filter_interpreter:,
      filter_node_interpreter:,
      runtime_metadata:,
      logger:,
      default_page_size: @config.default_page_size,
      max_page_size: @config.max_page_size
    )
  end
end

#datastore_search_routerObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/elastic_graph/graphql.rb', line 106

def datastore_search_router
  @datastore_search_router ||= begin
    require "elastic_graph/graphql/datastore_search_router"
    DatastoreSearchRouter.new(
      datastore_clients_by_name: @datastore_core.clients_by_name,
      logger: logger,
      monotonic_clock: monotonic_clock,
      config: @config
    )
  end
end

#filter_args_translatorObject



240
241
242
243
244
245
# File 'lib/elastic_graph/graphql.rb', line 240

def filter_args_translator
  @filter_args_translator ||= begin
    require "elastic_graph/graphql/filtering/filter_args_translator"
    Filtering::FilterArgsTranslator.new(schema_element_names: .schema_element_names)
  end
end

#filter_interpreterObject



224
225
226
227
228
229
# File 'lib/elastic_graph/graphql.rb', line 224

def filter_interpreter
  @filter_interpreter ||= begin
    require "elastic_graph/graphql/filtering/filter_interpreter"
    Filtering::FilterInterpreter.new(filter_node_interpreter: filter_node_interpreter, logger: logger)
  end
end

#filter_node_interpreterObject



232
233
234
235
236
237
# File 'lib/elastic_graph/graphql.rb', line 232

def filter_node_interpreter
  @filter_node_interpreter ||= begin
    require "elastic_graph/graphql/filtering/filter_node_interpreter"
    Filtering::FilterNodeInterpreter.new(runtime_metadata: )
  end
end

#graphql_adapterObject



156
157
158
159
160
161
162
163
164
165
# File 'lib/elastic_graph/graphql.rb', line 156

def graphql_adapter
  @graphql_adapter ||= begin
    require "elastic_graph/graphql/resolvers/graphql_adapter_builder"
    Resolvers::GraphQLAdapterBuilder.new(
      named_resolvers: named_graphql_resolvers,
      query_adapter: resolver_query_adapter,
      runtime_metadata: 
    ).build
  end
end

#graphql_gem_pluginsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/elastic_graph/graphql.rb', line 134

def graphql_gem_plugins
  @graphql_gem_plugins ||= begin
    Support::GraphQLGemLoader.load

    {
      # We depend on this to avoid N+1 calls to the datastore.
      ::GraphQL::Dataloader => {},
      # https://graphql-ruby.org/authorization/visibility.html
      ::GraphQL::Schema::Visibility => {
        # The GraphQL gem internally cache the visibility per profile. Ideally, we'd have only a single profile,
        # but that makes for a chicken-and-egg problem: our visibility logic depends on the schema being fully
        # loaded, but the `visibility?` methods are called while loading the schema. To deal with that, we're
        # using tw profiles:
        #
        # - `boot` is used while loading, and skips our normal visibility logic.
        # - `main` is used after that and uses our normal visibility logic.
        profiles: {:boot => {}, VISIBILITY_PROFILE => {}} # : ::Hash[::Symbol, ::Hash[::Symbol, untyped]]
      }
    }
  end
end

#graphql_http_endpointObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/elastic_graph/graphql.rb', line 64

def graphql_http_endpoint
  @graphql_http_endpoint ||= begin
    require "elastic_graph/graphql/http_endpoint"
    HTTPEndpoint.new(
      query_executor: graphql_query_executor,
      monotonic_clock: monotonic_clock,
      client_resolver: config.client_resolver
    )
  end
end

#graphql_query_executorObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/elastic_graph/graphql.rb', line 76

def graphql_query_executor
  @graphql_query_executor ||= begin
    require "elastic_graph/graphql/query_executor"
    QueryExecutor.new(
      schema: schema,
      monotonic_clock: monotonic_clock,
      logger: logger,
      slow_query_threshold_ms: @config.slow_query_latency_warning_threshold_in_ms
    )
  end
end

#load_dependencies_eagerlyObject

Loads dependencies eagerly. In some environments (such as in an AWS Lambda) this is desirable as we to load all dependencies at boot time instead of deferring dependency loading until we handle the first query. In other environments (such as tests), it’s nice to load dependencies when needed.



267
268
269
270
271
272
273
274
# File 'lib/elastic_graph/graphql.rb', line 267

def load_dependencies_eagerly
  Support::GraphQLGemLoader.load
  ::GraphQL.eager_load!

  # run a simple GraphQL query to force load any dependencies needed to handle GraphQL queries
  graphql_query_executor.execute(EAGER_LOAD_QUERY, client: Client::ELASTICGRAPH_INTERNAL)
  graphql_http_endpoint # force load this too.
end

#monotonic_clockObject



256
257
258
259
260
261
# File 'lib/elastic_graph/graphql.rb', line 256

def monotonic_clock
  @monotonic_clock ||= begin
    require "elastic_graph/support/monotonic_clock"
    Support::MonotonicClock.new
  end
end

#named_graphql_resolversObject



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/elastic_graph/graphql.rb', line 178

def named_graphql_resolvers
  @named_graphql_resolvers ||= .graphql_resolvers_by_name.transform_values do |resolver|
    ext = resolver.load_resolver

    ->(field_config) do
      (_ = ext.extension_class).new(
        elasticgraph_graphql: self,
        config: ext.config.merge(field_config)
      )
    end
  end
end

#resolver_query_adapterObject



167
168
169
170
171
172
173
174
175
# File 'lib/elastic_graph/graphql.rb', line 167

def resolver_query_adapter
  @resolver_query_adapter ||= begin
    require "elastic_graph/graphql/resolvers/query_adapter"
    Resolvers::QueryAdapter.new(
      datastore_query_builder: datastore_query_builder,
      datastore_query_adapters: datastore_query_adapters
    )
  end
end

#schemaObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/elastic_graph/graphql.rb', line 89

def schema
  @schema ||= begin
    require "elastic_graph/graphql/schema"
    Schema.new(
      graphql_schema_string: graphql_schema_string,
      config: config,
      logger: logger,
      runtime_metadata: ,
      datastore_search_router: datastore_search_router,
      index_definitions_by_graphql_type: @datastore_core.index_definitions_by_graphql_type,
      graphql_gem_plugins: graphql_gem_plugins,
      graphql_adapter: graphql_adapter
    )
  end
end

#sub_aggregation_grouping_adapterObject



248
249
250
251
252
253
# File 'lib/elastic_graph/graphql.rb', line 248

def sub_aggregation_grouping_adapter
  @sub_aggregation_grouping_adapter ||= begin
    require "elastic_graph/graphql/aggregation/non_composite_grouping_adapter"
    Aggregation::NonCompositeGroupingAdapter
  end
end