---
source: gql2sql/src/lib.rs
expression: statement.to_string()
---
SELECT json_build_object('app', (SELECT to_json((SELECT "root" FROM (SELECT "base"."orgId", "base"."id", "base"."branch", "base"."name", "base"."description", "base"."theme", "base"."favicon", "base"."customCSS", "base"."analytics", "base"."customDomain", "join.components" AS "components", "join.connections" AS "connections", "join.layouts" AS "layouts", "join.plugins" AS "plugins", "join.schemas" AS "schemas", "join.styles" AS "styles", "join.workflows" AS "workflows") AS "root")) AS "root" FROM (SELECT * FROM "App" WHERE "orgId" = $1 AND "id" = $2 AND "branch" = $3 LIMIT 1) AS "base" LEFT JOIN LATERAL (SELECT coalesce(json_agg(CAST(to_json((SELECT "root" FROM (SELECT "base.Component"."appId", "base.Component"."branch", "base.Component"."id", "base.Component"."branch") AS "root")) AS jsonb) || CASE WHEN "PageMeta.PageMeta"."PageMeta" IS NOT NULL THEN to_jsonb("PageMeta") WHEN "ComponentMeta.ComponentMeta"."ComponentMeta" IS NOT NULL THEN to_jsonb("ComponentMeta") ELSE jsonb_build_object() END), '[]') AS "join.components" FROM (SELECT * FROM "Component" WHERE "Component"."appId" = "base"."id" AND "Component"."branch" = "base"."branch") AS "base.Component" LEFT JOIN LATERAL (SELECT to_json((SELECT "root" FROM (SELECT "base.Component.PageMeta"."componentId", "base.Component.PageMeta"."branch", "base.Component.PageMeta"."title", "base.Component.PageMeta"."description", "base.Component.PageMeta"."path", "base.Component.PageMeta"."socialImage", "base.Component.PageMeta"."urlParams", "base.Component.PageMeta"."loader", "base.Component.PageMeta"."protection", "base.Component.PageMeta"."maxAge", "base.Component.PageMeta"."sMaxAge", "base.Component.PageMeta"."staleWhileRevalidate") AS "root")) AS "PageMeta" FROM (SELECT * FROM "PageMeta" WHERE "PageMeta"."componentId" = "base.Component"."id" AND "PageMeta"."branch" = "base.Component"."branch" LIMIT 1) AS "base.Component.PageMeta") AS "PageMeta.PageMeta" ON ('true') LEFT JOIN LATERAL (SELECT to_json((SELECT "root" FROM (SELECT "base.Component.ComponentMeta"."componentId", "base.Component.ComponentMeta"."branch", "base.Component.ComponentMeta"."title", "join.sources" AS "sources", "join.events" AS "events") AS "root")) AS "ComponentMeta" FROM (SELECT * FROM "ComponentMeta" WHERE "ComponentMeta"."componentId" = "base.Component"."id" AND "ComponentMeta"."branch" = "base.Component"."branch" LIMIT 1) AS "base.Component.ComponentMeta" LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Component.ComponentMeta.Source"."componentId", "base.Component.ComponentMeta.Source"."branch", "base.Component.ComponentMeta.Source"."id", "base.Component.ComponentMeta.Source"."branch", "base.Component.ComponentMeta.Source"."name", "base.Component.ComponentMeta.Source"."provider", "base.Component.ComponentMeta.Source"."description", "base.Component.ComponentMeta.Source"."template", "base.Component.ComponentMeta.Source"."instanceTemplate", "base.Component.ComponentMeta.Source"."outputType", "base.Component.ComponentMeta.Source"."source", "base.Component.ComponentMeta.Source"."sourceProp", "base.Component.ComponentMeta.Source"."componentId", "base.Component.ComponentMeta.Source"."utilityId", "join.component" AS "component", "join.utility" AS "utility") AS "root"))), '[]') AS "join.sources" FROM (SELECT * FROM "Source" WHERE "Source"."componentId" = "base.Component.ComponentMeta"."id" AND "Source"."branch" = "base.Component.ComponentMeta"."branch") AS "base.Component.ComponentMeta.Source" LEFT JOIN LATERAL (SELECT to_json((SELECT "root" FROM (SELECT "base.Component.ComponentMeta.Source.Element"."id", "base.Component.ComponentMeta.Source.Element"."branch", "base.Component.ComponentMeta.Source.Element"."id", "base.Component.ComponentMeta.Source.Element"."branch", "base.Component.ComponentMeta.Source.Element"."name", "base.Component.ComponentMeta.Source.Element"."kind", "base.Component.ComponentMeta.Source.Element"."source", "base.Component.ComponentMeta.Source.Element"."styles", "base.Component.ComponentMeta.Source.Element"."props", "base.Component.ComponentMeta.Source.Element"."order", "base.Component.ComponentMeta.Source.Element"."conditions") AS "root")) AS "join.component" FROM (SELECT * FROM "Element" WHERE "Element"."id" = "base.Component.ComponentMeta.Source"."componentId" AND "Element"."branch" = "base.Component.ComponentMeta.Source"."branch" ORDER BY "order" ASC LIMIT 1) AS "base.Component.ComponentMeta.Source.Element") AS "join.component.Element" ON ('true') LEFT JOIN LATERAL (SELECT to_json((SELECT "root" FROM (SELECT "base.Component.ComponentMeta.Source.Utility"."id", "base.Component.ComponentMeta.Source.Utility"."branch", "base.Component.ComponentMeta.Source.Utility"."id", "base.Component.ComponentMeta.Source.Utility"."branch", "base.Component.ComponentMeta.Source.Utility"."name", "base.Component.ComponentMeta.Source.Utility"."kind", "base.Component.ComponentMeta.Source.Utility"."kindId", "base.Component.ComponentMeta.Source.Utility"."data") AS "root")) AS "join.utility" FROM (SELECT * FROM "Utility" WHERE "Utility"."id" = "base.Component.ComponentMeta.Source"."componentId" AND "Utility"."branch" = "base.Component.ComponentMeta.Source"."branch" LIMIT 1) AS "base.Component.ComponentMeta.Source.Utility") AS "join.utility.Utility" ON ('true')) AS "join.sources.Source" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Component.ComponentMeta.Event"."componentMetaId", "base.Component.ComponentMeta.Event"."branch", "base.Component.ComponentMeta.Event"."id", "base.Component.ComponentMeta.Event"."branch", "base.Component.ComponentMeta.Event"."name", "base.Component.ComponentMeta.Event"."label", "base.Component.ComponentMeta.Event"."help", "base.Component.ComponentMeta.Event"."type") AS "root"))), '[]') AS "join.events" FROM (SELECT * FROM "Event" WHERE "Event"."componentMetaId" = "base.Component.ComponentMeta"."id" AND "Event"."branch" = "base.Component.ComponentMeta"."branch") AS "base.Component.ComponentMeta.Event") AS "join.events.Event" ON ('true')) AS "ComponentMeta.ComponentMeta" ON ('true')) AS "join.components.Component" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Connection"."appId", "base.Connection"."branch", "base.Connection"."id", "base.Connection"."branch", "base.Connection"."name", "base.Connection"."kind", "base.Connection"."prodUrl", "join.mutationSchema" AS "mutationSchema", "join.endpoints" AS "endpoints", "join.headers" AS "headers") AS "root"))), '[]') AS "join.connections" FROM (SELECT * FROM "Connection" WHERE "Connection"."appId" = "base"."id" AND "Connection"."branch" = "base"."branch") AS "base.Connection" LEFT JOIN LATERAL (SELECT to_json((SELECT "root" FROM (SELECT "base.Connection.Schema"."mutationConnectionId", "base.Connection.Schema"."branch", "base.Connection.Schema"."id", "base.Connection.Schema"."branch", "base.Connection.Schema"."schema") AS "root")) AS "join.mutationSchema" FROM (SELECT * FROM "Schema" WHERE "Schema"."mutationConnectionId" = "base.Connection"."id" AND "Schema"."branch" = "base.Connection"."branch" LIMIT 1) AS "base.Connection.Schema") AS "join.mutationSchema.Schema" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Connection.Endpoint"."connectionId", "base.Connection.Endpoint"."branch", "base.Connection.Endpoint"."id", "base.Connection.Endpoint"."branch", "base.Connection.Endpoint"."name", "base.Connection.Endpoint"."method", "base.Connection.Endpoint"."path", "base.Connection.Endpoint"."responseSchemaId", "join.headers" AS "headers", "join.search" AS "search") AS "root"))), '[]') AS "join.endpoints" FROM (SELECT * FROM "Endpoint" WHERE "Endpoint"."connectionId" = "base.Connection"."id" AND "Endpoint"."branch" = "base.Connection"."branch") AS "base.Connection.Endpoint" LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Connection.Endpoint.Header"."parentEndpointId", "base.Connection.Endpoint.Header"."branch", "base.Connection.Endpoint.Header"."id", "base.Connection.Endpoint.Header"."branch", "base.Connection.Endpoint.Header"."key", "base.Connection.Endpoint.Header"."value", "base.Connection.Endpoint.Header"."dynamic") AS "root"))), '[]') AS "join.headers" FROM (SELECT * FROM "Header" WHERE "Header"."parentEndpointId" = "base.Connection.Endpoint"."id" AND "Header"."branch" = "base.Connection.Endpoint"."branch") AS "base.Connection.Endpoint.Header") AS "join.headers.Header" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Connection.Endpoint.Search"."endpointId", "base.Connection.Endpoint.Search"."branch", "base.Connection.Endpoint.Search"."id", "base.Connection.Endpoint.Search"."branch", "base.Connection.Endpoint.Search"."key", "base.Connection.Endpoint.Search"."value", "base.Connection.Endpoint.Search"."dynamic") AS "root"))), '[]') AS "join.search" FROM (SELECT * FROM "Search" WHERE "Search"."endpointId" = "base.Connection.Endpoint"."id" AND "Search"."branch" = "base.Connection.Endpoint"."branch") AS "base.Connection.Endpoint.Search") AS "join.search.Search" ON ('true')) AS "join.endpoints.Endpoint" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Connection.Header"."parentConnectionId", "base.Connection.Header"."branch", "base.Connection.Header"."id", "base.Connection.Header"."branch", "base.Connection.Header"."key", "base.Connection.Header"."value", "base.Connection.Header"."dynamic") AS "root"))), '[]') AS "join.headers" FROM (SELECT * FROM "Header" WHERE "Header"."parentConnectionId" = "base.Connection"."id" AND "Header"."branch" = "base.Connection"."branch") AS "base.Connection.Header") AS "join.headers.Header" ON ('true')) AS "join.connections.Connection" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Layout"."appId", "base.Layout"."branch", "base.Layout"."id", "base.Layout"."branch", "base.Layout"."name", "base.Layout"."source", "base.Layout"."kind", "base.Layout"."styles", "base.Layout"."props") AS "root"))), '[]') AS "join.layouts" FROM (SELECT * FROM "Layout" WHERE "Layout"."appId" = "base"."id" AND "Layout"."branch" = "base"."branch") AS "base.Layout") AS "join.layouts.Layout" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Plugin"."appId", "base.Plugin"."branch", "base.Plugin"."instanceId", "base.Plugin"."kind") AS "root"))), '[]') AS "join.plugins" FROM (SELECT * FROM "Plugin" WHERE "Plugin"."appId" = "base"."id" AND "Plugin"."branch" = "base"."branch") AS "base.Plugin") AS "join.plugins.Plugin" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Schema"."appId", "base.Schema"."branch", "base.Schema"."id", "base.Schema"."branch", "base.Schema"."schema") AS "root"))), '[]') AS "join.schemas" FROM (SELECT * FROM "Schema" WHERE "Schema"."appId" = "base"."id" AND "Schema"."branch" = "base"."branch") AS "base.Schema") AS "join.schemas.Schema" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Style"."appId", "base.Style"."branch", "base.Style"."id", "base.Style"."branch", "base.Style"."name", "base.Style"."kind", "base.Style"."styles", "base.Style"."isDefault") AS "root"))), '[]') AS "join.styles" FROM (SELECT * FROM "Style" WHERE "Style"."appId" = "base"."id" AND "Style"."branch" = "base"."branch") AS "base.Style") AS "join.styles.Style" ON ('true') LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Workflow"."appId", "base.Workflow"."branch", "base.Workflow"."id", "base.Workflow"."branch", "base.Workflow"."name", "base.Workflow"."args", "join.steps" AS "steps") AS "root"))), '[]') AS "join.workflows" FROM (SELECT * FROM "Workflow" WHERE "Workflow"."appId" = "base"."id" AND "Workflow"."branch" = "base"."branch") AS "base.Workflow" LEFT JOIN LATERAL (SELECT coalesce(json_agg(to_json((SELECT "root" FROM (SELECT "base.Workflow.Step"."workflowId", "base.Workflow.Step"."branch", "base.Workflow.Step"."id", "base.Workflow.Step"."branch", "base.Workflow.Step"."parentId", "base.Workflow.Step"."kind", "base.Workflow.Step"."kindId", "base.Workflow.Step"."data", "base.Workflow.Step"."order") AS "root"))), '[]') AS "join.steps" FROM (SELECT * FROM "Step" WHERE "Step"."workflowId" = "base.Workflow"."id" AND "Step"."branch" = "base.Workflow"."branch" ORDER BY "order" ASC) AS "base.Workflow.Step") AS "join.steps.Step" ON ('true')) AS "join.workflows.Workflow" ON ('true'))) AS "data"