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/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/list_records.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/resolvers/graphql_adapter.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/monkey_patches/schema_field.rb,
lib/elastic_graph/graphql/filtering/filter_interpreter.rb,
lib/elastic_graph/graphql/monkey_patches/schema_object.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/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/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/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/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, MonkeyPatches, Resolvers, ScalarCoercionAdapters Classes: Client, Config, DatastoreQuery, DatastoreSearchRouter, DecodedCursor, HTTPEndpoint, 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 content_type
    @content_type ||= normalized_headers["CONTENT-TYPE"]
  end

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

Represents an HTTP response, containing:

  • status_code: an integer like 200.

  • headers: a hash with string keys and values containing HTTP response headers.

  • body: a string containing the response body.

Data.define(:status_code, :headers, :body) do
  # @implements HTTPResponse

  # Helper method for building a JSON response.
  def self.json(status_code, body)
    new(status_code, {"Content-Type" => HTTPEndpoint::APPLICATION_JSON}, ::JSON.generate(body))
  end

  # Helper method for building an error response.
  def self.error(status_code, message)
    json(status_code, {"errors" => [{"message" => message}]})
  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.



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

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.extension_class }
end

Instance Attribute Details

#clockObject (readonly)



21
22
23
# File 'lib/elastic_graph/graphql.rb', line 21

def clock
  @clock
end

#configObject (readonly)



21
22
23
# File 'lib/elastic_graph/graphql.rb', line 21

def config
  @config
end

#datastore_coreObject (readonly)



21
22
23
# File 'lib/elastic_graph/graphql.rb', line 21

def datastore_core
  @datastore_core
end

#graphql_schema_stringObject (readonly)



21
22
23
# File 'lib/elastic_graph/graphql.rb', line 21

def graphql_schema_string
  @graphql_schema_string
end

#loggerObject (readonly)



21
22
23
# File 'lib/elastic_graph/graphql.rb', line 21

def logger
  @logger
end

#runtime_metadataObject (readonly)



21
22
23
# File 'lib/elastic_graph/graphql.rb', line 21

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`).



26
27
28
29
30
31
# File 'lib/elastic_graph/graphql.rb', line 26

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

Instance Method Details

#datastore_query_adaptersObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/elastic_graph/graphql.rb', line 172

def datastore_query_adapters
  @datastore_query_adapters ||= begin
    require "elastic_graph/graphql/aggregation/query_adapter"
    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::Pagination.new(schema_element_names: schema_element_names),
      GraphQL::QueryAdapter::Filters.new(schema_element_names: schema_element_names, filter_args_translator: filter_args_translator),
      GraphQL::QueryAdapter::Sort.new(order_by_arg_name: schema_element_names.order_by),
      Aggregation::QueryAdapter.new(
        schema: schema,
        config: config,
        filter_args_translator: filter_args_translator,
        runtime_metadata: ,
        sub_aggregation_grouping_adapter: sub_aggregation_grouping_adapter
      ),
      GraphQL::QueryAdapter::RequestedFields.new(schema)
    ]
  end
end

#datastore_query_builderObject



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/elastic_graph/graphql.rb', line 128

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

#datastore_search_routerObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/elastic_graph/graphql.rb', line 115

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



207
208
209
210
211
212
# File 'lib/elastic_graph/graphql.rb', line 207

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



199
200
201
202
203
204
# File 'lib/elastic_graph/graphql.rb', line 199

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

#graphql_gem_pluginsObject



142
143
144
145
146
147
# File 'lib/elastic_graph/graphql.rb', line 142

def graphql_gem_plugins
  @graphql_gem_plugins ||= begin
    require "graphql"
    {::GraphQL::Dataloader => {}}
  end
end

#graphql_http_endpointObject



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

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



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

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,
      datastore_search_router: datastore_search_router
    )
  end
end

#graphql_resolversObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/elastic_graph/graphql.rb', line 150

def graphql_resolvers
  @graphql_resolvers ||= begin
    require "elastic_graph/graphql/resolvers/get_record_field_value"
    require "elastic_graph/graphql/resolvers/list_records"
    require "elastic_graph/graphql/resolvers/nested_relationships"

    nested_relationships = Resolvers::NestedRelationships.new(
      schema_element_names: .schema_element_names,
      logger: logger
    )

    list_records = Resolvers::ListRecords.new

    get_record_field_value = Resolvers::GetRecordFieldValue.new(
      schema_element_names: .schema_element_names
    )

    [nested_relationships, list_records, get_record_field_value]
  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.



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

def load_dependencies_eagerly
  # 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



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

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

#schemaObject



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
# File 'lib/elastic_graph/graphql.rb', line 88

def schema
  @schema ||= begin
    require "elastic_graph/graphql/schema"

    Schema.new(
      graphql_schema_string: graphql_schema_string,
      config: config,
      runtime_metadata: ,
      index_definitions_by_graphql_type: @datastore_core.index_definitions_by_graphql_type,
      graphql_gem_plugins: graphql_gem_plugins
    ) do |schema|
      @graphql_adapter || begin
        @schema = schema # assign this so that `#schema` returns the schema when `datastore_query_adapters` is called below
        require "elastic_graph/graphql/resolvers/graphql_adapter"
        Resolvers::GraphQLAdapter.new(
          schema: schema,
          datastore_query_builder: datastore_query_builder,
          datastore_query_adapters: datastore_query_adapters,
          runtime_metadata: ,
          resolvers: graphql_resolvers
        )
      end
    end
  end
end

#sub_aggregation_grouping_adapterObject



215
216
217
218
219
220
# File 'lib/elastic_graph/graphql.rb', line 215

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