Skip to content

Instantly share code, notes, and snippets.

@rattrayalex
Last active April 21, 2021 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rattrayalex/defcdffb3315348283757bd5f991beac to your computer and use it in GitHub Desktop.
Save rattrayalex/defcdffb3315348283757bd5f991beac to your computer and use it in GitHub Desktop.
Postgraphile Pagila gql to sql demo
query MyQuery {
actor(actorId: 10) {
firstName
lastName
filmActors {
nodes {
film {
title
description
}
}
}
}
film(filmId: 10) {
title
description
}
}
select to_json((__local_0__."first_name")) as "firstName",
to_json((__local_0__."last_name")) as "lastName",
to_json(
(
with __local_1__ as (
select to_json(
(
json_build_object(
'@film'::text,
(
select json_build_object(
'title'::text,
(__local_2__."title"),
'description'::text,
(__local_2__."description")
) as object
from "public"."film" as __local_2__
where (__local_3__."film_id" = __local_2__."film_id") and (TRUE) and (TRUE)
)
)
)
) as "@nodes"
from (
select __local_3__.*
from "public"."film_actor" as __local_3__
where (__local_3__."actor_id" = __local_0__."actor_id") and (TRUE) and (TRUE)
order by __local_3__."actor_id" ASC,
__local_3__."film_id" ASC
) __local_3__
),
__local_4__ as (
select json_agg(
to_json(__local_1__)
) as data
from __local_1__
)
select json_build_object(
'data'::text,
coalesce(
(
select __local_4__.data
from __local_4__
),
'[]'::json
)
)
)
) as "@filmActors"
from "public"."actor" as __local_0__
where (
__local_0__."actor_id" = $1
) and (TRUE) and (TRUE)
select to_json((__local_0__."title")) as "title",
to_json((__local_0__."description")) as "description"
from "public"."film" as __local_0__
where (
__local_0__."film_id" = $1
) and (TRUE) and (TRUE)
@rattrayalex
Copy link
Author

Another example is here: https://gist.github.com/rattrayalex/ae39c2cf0356f1257ece4f3c4f6488cd
in which I rewrote the generated sql to an aspirational readable version

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