Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.1 KB
Raw Blame
import { defineCommand } from '@/core/command'
import { DiagnosticsReport, printDiagnostics } from '@/core/diagnostics'

export default defineCommand({
    description: 'Check the syntax and types of a HogQL query without running it',
    args: {
        query: { description: 'The HogQL query to check, or - to read it from stdin', stdin: true },
    },
    scopes: ['query:read'],
    notes: 'Exits with code 1 when the query has errors, so scripts and agents can branch on it.',
    examples: ['hog query check "select event from events"'],

    async run({ args, ctx }) {
        const api = await ctx.posthog()
        const response = await api.post(
            'query',
            { query: { kind: 'HogQLMetadata', language: 'hogQL', query: args.query } },
            DiagnosticsReport
        )
        return { query: args.query, response }
    },

    human({ result, out }) {
        printDiagnostics(out, result.query, result.response)
    },

    agent({ result, out }) {
        out.json(result.response)
    },

    exitCode: ({ response }) => ((response.errors ?? []).length > 0 ? 1 : 0),
})