/* 
This file is auto generated by pgrx.

The ordering of items is not stable, it is driven by a dependency graph.
*/

-- src/lib.rs:26
-- pg_graphql::_internal_resolve
CREATE  FUNCTION graphql."_internal_resolve"(
	"query" TEXT, /* &str */
	"variables" jsonb DEFAULT '{}', /* core::option::Option<pgrx::datum::json::JsonB> */
	"operationName" TEXT DEFAULT null, /* core::option::Option<alloc::string::String> */
	"extensions" jsonb DEFAULT null /* core::option::Option<pgrx::datum::json::JsonB> */
) RETURNS jsonb /* pgrx::datum::json::JsonB */

LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'resolve_wrapper';

-- src/lib.rs:22
-- requires:
--   resolve

create or replace function graphql.resolve(
      "query" text,
      "variables" jsonb default '{}',
      "operationName" text default null,
      "extensions" jsonb default null
)
    returns jsonb
    language plpgsql
as $$
declare
    res jsonb;
    message_text text;
begin
  begin
    select graphql._internal_resolve("query" := "query",
                                     "variables" := "variables",
                                     "operationName" := "operationName",
                                     "extensions" := "extensions") into res;
    return res;
  exception
    when others then
    get stacked diagnostics message_text = message_text;
    return
    jsonb_build_object('data', null,
                       'errors', jsonb_build_array(jsonb_build_object('message', message_text)));
  end;
end;
$$;


-- src/lib.rs:20
create function graphql.comment_directive(comment_ text)
    returns jsonb
    language sql
    immutable
as $$
    /*
    comment on column public.account.name is '@graphql.name: myField'
    */
    select
        coalesce(
            (
                regexp_match(
                    comment_,
                    '@graphql\((.+?)\)'
                )
            )[1]::jsonb,
            jsonb_build_object()
        )
$$;


-- src/lib.rs:21
create or replace function graphql.exception(message text)
    returns text
    language plpgsql
as $$
begin
    raise exception using errcode='22000', message=message;
end;
$$;


-- src/lib.rs:19
-- Is updated every time the schema changes
create sequence if not exists graphql.seq_schema_version as int cycle;

create or replace function graphql.increment_schema_version()
    returns event_trigger
    security definer
    language plpgsql
as $$
begin
    perform nextval('graphql.seq_schema_version');
end;
$$;

create or replace function graphql.get_schema_version()
    returns int
    security definer
    language sql
as $$
    select last_value from graphql.seq_schema_version;
$$;

-- On DDL event, increment the schema version number
create event trigger graphql_watch_ddl
    on ddl_command_end
    execute procedure graphql.increment_schema_version();

create event trigger graphql_watch_drop
    on sql_drop
    execute procedure graphql.increment_schema_version();

