Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support graphql aliases on links_of_type #20

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/graphql/types/links_type.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Types
class LinksType < Types::BaseObject
field :available_translations, [EditionType], null: true
field :links_of_type, [EditionType] do
field :links_of_type, [EditionType], extras: [:ast_node] do
argument :type, String, required: true
argument :reverse, Boolean, default_value: false
end
Expand Down Expand Up @@ -30,7 +30,10 @@ def available_translations
end

# rubocop:disable Lint/UnusedMethodArgument
def links_of_type(type:, reverse:) = object.dig(:links, type)
def links_of_type(type:, reverse:, ast_node:)
key = ast_node.alias || type
object.dig(:links, key)
end
# rubocop:enable Lint/UnusedMethodArgument
end
end
6 changes: 3 additions & 3 deletions app/models/expanded_edition_dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def get_or_prepare_statement(include_drafts: false, include_withdrawn: false, lo
db = Sequel::Model.db

paths_from_json_sql = <<~SQL
SELECT path, next, columns
FROM json_to_recordset(?) AS paths(path text[], next text, columns jsonb)
SELECT path, next, next_alias, columns
FROM json_to_recordset(?) AS paths(path text[], next text, next_alias text, columns jsonb)
SQL

state_filter = ["published", ("draft" if include_drafts), ("unpublished" if include_withdrawn)].compact
Expand All @@ -34,7 +34,7 @@ def get_or_prepare_statement(include_drafts: false, include_withdrawn: false, lo
*PathTreeHelpers::ALL_EDITION_COLUMNS.without(:state).map { Sequel[:editions][it] },
].compact
child_selections = [
Sequel[:edition_links][:path].pg_array.push(Sequel[:link_type].cast(:text)).as(:path),
Sequel[:edition_links][:path].pg_array.push(Sequel[:next_alias]).as(:path),
Sequel[:edition_links][:id_path].pg_array.push(Sequel[:editions][:id]).as(:id_path),
Sequel[:documents][:content_id].as(:content_id),
Sequel[:documents][:id].as(:document_id),
Expand Down
5 changes: 4 additions & 1 deletion lib/path_tree_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def self.build_paths(lookahead)

def self.build_path(raw_path)
{
path: raw_path[0...-1].map { it[:type] },
path: raw_path[0...-1].map { it[:alias] || it[:type] },
next: raw_path.last[:type],
next_alias: raw_path.last[:alias] || raw_path.last[:type],
columns: raw_path.last[:columns],
}
end
Expand All @@ -60,6 +61,8 @@ def self.extract_paths(lookahead, current_path = [])
def self.build_segment(selection)
segment = {}
segment = segment.merge(selection.arguments)
selection.ast_nodes.map(&:alias) => [ selection_alias ]
segment = segment.merge({ alias: selection_alias }) if selection_alias.present?
segment.merge({ columns: (selection.selections.map(&:name).to_set & ALL_EDITION_COLUMNS).index_with(true) })
end

Expand Down
35 changes: 19 additions & 16 deletions test/lib/path_tree_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,33 @@
require_relative "../test_helper"

class PathTreeHelpersTest < Minitest::Test
Selection = Data.define(:name, :arguments, :selections)
AstNode = Data.define(:alias)
Selection = Data.define(:name, :ast_nodes, :arguments, :selections)

def test_extract_paths
ast_nodes = [AstNode.new(alias: nil)]
lookahead = Minitest::Mock.new
lookahead.expect :selections, [
Selection.new(name: :links, arguments: {}, selections: [
Selection.new(name: :links_of_type, arguments: { name: "person" }, selections: [
Selection.new(name: :base_path, arguments: {}, selections: []),
Selection.new(name: :title, arguments: {}, selections: []),
Selection.new(name: :links, arguments: {}, selections: [
Selection.new(name: :links_of_type, arguments: { name: "person", reverse: true }, selections: [
Selection.new(name: :base_path, arguments: {}, selections: []),
Selection.new(name: :title, arguments: {}, selections: []),
Selection.new(name: :links, arguments: {}, selections: [
Selection.new(name: :links_of_type, arguments: { name: "role" }, selections: [
Selection.new(name: :base_path, arguments: {}, selections: []),
Selection.new(name: :title, arguments: {}, selections: []),
Selection.new(name: :links, ast_nodes:, arguments: {}, selections: [
Selection.new(name: :links_of_type, ast_nodes:, arguments: { name: "person" }, selections: [
Selection.new(name: :base_path, ast_nodes: [], arguments: {}, selections: []),
Selection.new(name: :title, ast_nodes:, arguments: {}, selections: []),
Selection.new(name: :links, ast_nodes:, arguments: {}, selections: [
Selection.new(name: :links_of_type, ast_nodes:, arguments: { name: "person", reverse: true }, selections: [
Selection.new(name: :base_path, ast_nodes:, arguments: {}, selections: []),
Selection.new(name: :title, ast_nodes:, arguments: {}, selections: []),
Selection.new(name: :links, ast_nodes:, arguments: {}, selections: [
Selection.new(name: :links_of_type, ast_nodes:, arguments: { name: "role" }, selections: [
Selection.new(name: :base_path, ast_nodes:, arguments: {}, selections: []),
Selection.new(name: :title, ast_nodes:, arguments: {}, selections: []),
]),
]),
]),
]),
]),
Selection.new(name: :links_of_type, arguments: { name: "organisation" }, selections: [
Selection.new(name: :base_path, arguments: {}, selections: []),
Selection.new(name: :title, arguments: {}, selections: []),
Selection.new(name: :links_of_type, ast_nodes:, arguments: { name: "organisation" }, selections: [
Selection.new(name: :base_path, ast_nodes:, arguments: {}, selections: []),
Selection.new(name: :title, ast_nodes:, arguments: {}, selections: []),
]),
]),
]
Expand Down