|
| 1 | +# frozen_string_literal: true |
| 2 | +module Graphql |
| 3 | + class Dashboard < Rails::Engine |
| 4 | + module OperationStore |
| 5 | + module CheckInstalled |
| 6 | + def self.included(child_module) |
| 7 | + child_module.before_action(:check_installed) |
| 8 | + end |
| 9 | + |
| 10 | + def check_installed |
| 11 | + if !schema_class.respond_to?(:operation_store) || schema_class.operation_store.nil? |
| 12 | + render "graphql/dashboard/operation_store/not_installed" |
| 13 | + end |
| 14 | + end |
| 15 | + end |
| 16 | + class ClientsController < Dashboard::ApplicationController |
| 17 | + include CheckInstalled |
| 18 | + |
| 19 | + def index |
| 20 | + @order_by = params[:order_by] || "name" |
| 21 | + @order_dir = params[:order_dir].presence || "asc" |
| 22 | + clients_page = schema_class.operation_store.all_clients( |
| 23 | + page: params[:page]&.to_i || 1, |
| 24 | + per_page: params[:per_page]&.to_i || 25, |
| 25 | + order_by: @order_by, |
| 26 | + order_dir: @order_dir, |
| 27 | + ) |
| 28 | + |
| 29 | + @clients_page = clients_page |
| 30 | + end |
| 31 | + |
| 32 | + def new |
| 33 | + @client = init_client(secret: SecureRandom.hex(32)) |
| 34 | + end |
| 35 | + |
| 36 | + def create |
| 37 | + client_params = params.require(:client).permit(:name, :secret) |
| 38 | + schema_class.operation_store.upsert_client(client_params[:name], client_params[:secret]) |
| 39 | + flash[:success] = "Created #{client_params[:name].inspect}" |
| 40 | + redirect_to graphql_dashboard.operation_store_clients_path |
| 41 | + end |
| 42 | + |
| 43 | + def edit |
| 44 | + @client = schema_class.operation_store.get_client(params[:name]) |
| 45 | + end |
| 46 | + |
| 47 | + def update |
| 48 | + client_name = params[:name] |
| 49 | + client_secret = params.require(:client).permit(:secret)[:secret] |
| 50 | + schema_class.operation_store.upsert_client(client_name, client_secret) |
| 51 | + flash[:success] = "Updated #{client_name.inspect}" |
| 52 | + redirect_to graphql_dashboard.operation_store_clients_path |
| 53 | + end |
| 54 | + |
| 55 | + def destroy |
| 56 | + client_name = params[:name] |
| 57 | + schema_class.operation_store.delete_client(client_name) |
| 58 | + flash[:success] = "Deleted #{client_name.inspect}" |
| 59 | + redirect_to graphql_dashboard.operation_store_clients_path |
| 60 | + end |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + def init_client(name: nil, secret: nil) |
| 65 | + GraphQL::Pro::OperationStore::ClientRecord.new( |
| 66 | + name: name, |
| 67 | + secret: secret, |
| 68 | + created_at: nil, |
| 69 | + operations_count: 0, |
| 70 | + archived_operations_count: 0, |
| 71 | + last_synced_at: nil, |
| 72 | + last_used_at: nil, |
| 73 | + ) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + class OperationsController < Dashboard::ApplicationController |
| 78 | + include CheckInstalled |
| 79 | + |
| 80 | + def index |
| 81 | + @client_operations = client_name = params[:client_name] |
| 82 | + per_page = params[:per_page]&.to_i || 25 |
| 83 | + page = params[:page]&.to_i || 1 |
| 84 | + @is_archived = params[:archived_status] == :archived |
| 85 | + order_by = params[:order_by] || "name" |
| 86 | + order_dir = params[:order_dir]&.to_sym || :asc |
| 87 | + if @client_operations |
| 88 | + @operations_page = schema_class.operation_store.get_client_operations_by_client( |
| 89 | + client_name, |
| 90 | + page: page, |
| 91 | + per_page: per_page, |
| 92 | + is_archived: @is_archived, |
| 93 | + order_by: order_by, |
| 94 | + order_dir: order_dir, |
| 95 | + ) |
| 96 | + opposite_archive_mode_count = schema_class.operation_store.get_client_operations_by_client( |
| 97 | + client_name, |
| 98 | + page: 1, |
| 99 | + per_page: 1, |
| 100 | + is_archived: !@is_archived, |
| 101 | + order_by: order_by, |
| 102 | + order_dir: order_dir, |
| 103 | + ).total_count |
| 104 | + else |
| 105 | + @operations_page = schema_class.operation_store.all_operations( |
| 106 | + page: page, |
| 107 | + per_page: per_page, |
| 108 | + is_archived: @is_archived, |
| 109 | + order_by: order_by, |
| 110 | + order_dir: order_dir, |
| 111 | + ) |
| 112 | + opposite_archive_mode_count = schema_class.operation_store.all_operations( |
| 113 | + page: 1, |
| 114 | + per_page: 1, |
| 115 | + is_archived: !@is_archived, |
| 116 | + order_by: order_by, |
| 117 | + order_dir: order_dir, |
| 118 | + ).total_count |
| 119 | + end |
| 120 | + |
| 121 | + if @is_archived |
| 122 | + @archived_operations_count = @operations_page.total_count |
| 123 | + @unarchived_operations_count = opposite_archive_mode_count |
| 124 | + else |
| 125 | + @archived_operations_count = opposite_archive_mode_count |
| 126 | + @unarchived_operations_count = @operations_page.total_count |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + def show |
| 131 | + digest = params[:digest] |
| 132 | + @operation = schema_class.operation_store.get_operation_by_digest(digest) |
| 133 | + if @operation |
| 134 | + # Parse & re-format the query |
| 135 | + document = GraphQL.parse(@operation.body) |
| 136 | + @graphql_source = document.to_query_string |
| 137 | + |
| 138 | + @client_operations = schema_class.operation_store.get_client_operations_by_digest(digest) |
| 139 | + @entries = schema_class.operation_store.get_index_entries_by_digest(digest) |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + def update |
| 144 | + is_archived = case params[:modification] |
| 145 | + when :archive |
| 146 | + true |
| 147 | + when :unarchive |
| 148 | + false |
| 149 | + else |
| 150 | + raise ArgumentError, "Unexpected modification: #{params[:modification].inspect}" |
| 151 | + end |
| 152 | + |
| 153 | + if (client_name = params[:client_name]) |
| 154 | + operation_aliases = params[:operation_aliases] |
| 155 | + schema_class.operation_store.archive_client_operations( |
| 156 | + client_name: client_name, |
| 157 | + operation_aliases: operation_aliases, |
| 158 | + is_archived: is_archived |
| 159 | + ) |
| 160 | + flash[:success] = "#{is_archived ? "Archived" : "Activated"} #{operation_aliases.size} #{"operation".pluralize(operation_aliases.size)}" |
| 161 | + else |
| 162 | + digests = params[:digests] |
| 163 | + schema_class.operation_store.archive_operations( |
| 164 | + digests: digests, |
| 165 | + is_archived: is_archived |
| 166 | + ) |
| 167 | + flash[:success] = "#{is_archived ? "Archived" : "Activated"} #{digests.size} #{"operation".pluralize(digests.size)}" |
| 168 | + end |
| 169 | + head :no_content |
| 170 | + end |
| 171 | + end |
| 172 | + |
| 173 | + class IndexEntriesController < Dashboard::ApplicationController |
| 174 | + def index |
| 175 | + @search_term = if request.params["q"] && request.params["q"].length > 0 |
| 176 | + request.params["q"] |
| 177 | + else |
| 178 | + nil |
| 179 | + end |
| 180 | + |
| 181 | + @index_entries_page = schema_class.operation_store.all_index_entries( |
| 182 | + search_term: @search_term, |
| 183 | + page: params[:page]&.to_i || 1, |
| 184 | + per_page: params[:per_page]&.to_i || 25, |
| 185 | + ) |
| 186 | + end |
| 187 | + |
| 188 | + def show |
| 189 | + name = params[:name] |
| 190 | + @entry = schema_class.operation_store.index.get_entry(name) |
| 191 | + @chain = schema_class.operation_store.index.index_entry_chain(name) |
| 192 | + @operations = schema_class.operation_store.get_operations_by_index_entry(name) |
| 193 | + end |
| 194 | + end |
| 195 | + end |
| 196 | + end |
| 197 | +end |
0 commit comments