Skip to content

QueryEditor with JSON Textarea Interface #442

Closed

Description

@malibio

Overview

Implement QueryEditor component as a simple JSON textarea for editing query definitions. This MVP approach allows manual query creation/editing while laying groundwork for future visual builder or natural language interface.

Architecture References

Problem Statement

Users need a way to create and edit QueryDefinition JSON manually. While a visual query builder or natural language interface would be ideal, we need a working solution now that can be progressively enhanced later.

Dependencies

Proposed Solution

Progressive Enhancement Path

Phase 1 (This Issue): JSON textarea with validation
Phase 2 (Future): Visual query builder with filter chips
Phase 3 (Future): Natural language interface (AI converts text to QueryDefinition)

All modes operate on the same QueryDefinition type, making upgrades non-breaking.

Why JSON First?

  • Immediate functionality (no complex UI needed)
  • Full query power (no limitations from simplified UI)
  • Developer-friendly for power users
  • Easy to copy/paste/share queries
  • Foundation for future enhancements

Implementation

File: packages/desktop-app/src/lib/components/query/query-editor.svelte

<script lang="ts">
  import { queryService } from '$lib/services/query-service';
  import type { QueryDefinition } from '$lib/types/query';

  let { 
    query = null,
    onSave,
    onCancel
  }: {
    query?: QueryDefinition | null;
    onSave: (query: QueryDefinition) => void;
    onCancel?: () => void;
  } = $props();

  const DEFAULT_QUERY: QueryDefinition = {
    targetType: 'task',
    filters: [],
    limit: 50
  };

  let queryJson = $state(JSON.stringify(query || DEFAULT_QUERY, null, 2));
  let error = $state<string | null>(null);
  let previewCount = $state<number | null>(null);

  function validateAndSave() {
    try {
      const parsed = JSON.parse(queryJson) as QueryDefinition;
      
      // Basic validation
      if (!parsed.targetType) {
        throw new Error('Missing required field: targetType');
      }
      if (!Array.isArray(parsed.filters)) {
        throw new Error('filters must be an array');
      }
      
      error = null;
      onSave(parsed);
    } catch (e) {
      error = e instanceof Error ? e.message : 'Invalid JSON';
    }
  }

  async function loadPreview() {
    try {
      const parsed = JSON.parse(queryJson) as QueryDefinition;
      const results = await queryService.execute(parsed);
      previewCount = results.length;
    } catch {
      previewCount = null;
    }
  }
</script>

<div class="query-editor">
  <div class="editor-header">
    <h2>Query Definition</h2>
    <button onclick={loadPreview} class="preview-btn">
      Preview ({previewCount !== null ? `${previewCount} results` : 'click to preview'})
    </button>
  </div>

  <textarea
    bind:value={queryJson}
    rows="20"
    class="query-textarea font-mono"
    placeholder="Enter QueryDefinition JSON..."
  />
  
  {#if error}
    <div class="error-message">{error}</div>
  {/if}

  <details class="examples">
    <summary>Query Examples & Templates</summary>
    <!-- Include example queries -->
  </details>

  <div class="editor-footer">
    <button onclick={validateAndSave} class="save-btn">Save Query</button>
    {#if onCancel}
      <button onclick={onCancel} class="cancel-btn">Cancel</button>
    {/if}
  </div>
</div>

Query Examples to Include

All Incomplete Tasks

{
  "targetType": "task",
  "filters": [
    { "type": "property", "operator": "in", "property": "status", "value": ["open", "in_progress"] }
  ]
}

Recent Text Nodes with Keyword

{
  "targetType": "text",
  "filters": [
    { "type": "content", "operator": "contains", "value": "meeting" }
  ],
  "sorting": [{ "field": "created_at", "direction": "desc" }],
  "limit": 10
}

Children of Specific Node

{
  "targetType": "*",
  "filters": [
    { "type": "relationship", "relationshipType": "children", "nodeId": "your-node-id" }
  ]
}

Acceptance Criteria

  • QueryEditor renders with JSON textarea
  • Invalid JSON shows clear error message
  • Valid QueryDefinition can be saved
  • Cancel action discards changes
  • Query templates/examples provided
  • Preview shows estimated result count
  • Component works in both create and edit modes
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Testing Requirements

  • Unit tests for JSON validation logic
  • Integration tests for save/cancel flows
  • Tests for query examples (all examples are valid)
  • Keyboard accessibility tests

Non-Goals

  • Visual query builder (future enhancement)
  • Natural language interface (future enhancement)
  • Syntax highlighting with Monaco editor (optional nice-to-have)

Activity

  1. malibio commented on Apr 5, 2026

    @malibio
    CollaboratorAuthor

    Implemented in PR #1044 — QueryEditor component with JSON textarea, validation, preview, and template examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions