@@ -74,20 +74,6 @@ CREATE SCHEMA storage;
7474CREATE SCHEMA vault ;
7575
7676
77- --
78- -- Name: pg_graphql; Type: EXTENSION; Schema: -; Owner: -
79- --
80-
81- CREATE EXTENSION IF NOT EXISTS pg_graphql WITH SCHEMA graphql;
82-
83-
84- --
85- -- Name: EXTENSION pg_graphql; Type: COMMENT; Schema: -; Owner: -
86- --
87-
88- COMMENT ON EXTENSION pg_graphql IS ' pg_graphql: GraphQL support' ;
89-
90-
9177--
9278-- Name: pg_stat_statements; Type: EXTENSION; Schema: -; Owner: -
9379--
@@ -228,54 +214,43 @@ COMMENT ON FUNCTION extensions.grant_pg_cron_access() IS 'Grants access to pg_cr
228214CREATE FUNCTION extensions .grant_pg_graphql_access() RETURNS event_trigger
229215 LANGUAGE plpgsql
230216 AS $_$
231- DECLARE
232- func_is_graphql_resolve bool;
233- BEGIN
234- func_is_graphql_resolve = (
235- SELECT n .proname = ' resolve'
236- FROM pg_event_trigger_ddl_commands() AS ev
237- LEFT JOIN pg_catalog .pg_proc AS n
238- ON ev .objid = n .oid
239- );
240-
241- IF func_is_graphql_resolve
242- THEN
243- -- Update public wrapper to pass all arguments through to the pg_graphql resolve func
244- DROP FUNCTION IF EXISTS graphql_public .graphql ;
245- create or replace function graphql_public .graphql(
246- " operationName" text default null ,
247- query text default null ,
248- variables jsonb default null ,
249- extensions jsonb default null
250- )
251- returns jsonb
252- language sql
253- as $$
254- select graphql .resolve (
255- query := query,
256- variables := coalesce(variables, ' {}' ),
257- " operationName" := " operationName" ,
258- extensions := extensions
259- );
260- $$;
261-
262- -- This hook executes when `graphql.resolve` is created. That is not necessarily the last
263- -- function in the extension so we need to grant permissions on existing entities AND
264- -- update default permissions to any others that are created after `graphql.resolve`
265- grant usage on schema graphql to postgres, anon, authenticated, service_role;
266- grant select on all tables in schema graphql to postgres, anon, authenticated, service_role;
267- grant execute on all functions in schema graphql to postgres, anon, authenticated, service_role;
268- grant all on all sequences in schema graphql to postgres, anon, authenticated, service_role;
269- alter default privileges in schema graphql grant all on tables to postgres, anon, authenticated, service_role;
270- alter default privileges in schema graphql grant all on functions to postgres, anon, authenticated, service_role;
271- alter default privileges in schema graphql grant all on sequences to postgres, anon, authenticated, service_role;
272-
273- -- Allow postgres role to allow granting usage on graphql and graphql_public schemas to custom roles
274- grant usage on schema graphql_public to postgres with grant option;
275- grant usage on schema graphql to postgres with grant option;
276- END IF;
277-
278- END;
217+ begin
218+ if not exists (
219+ select 1
220+ from pg_event_trigger_ddl_commands() ev
221+ join pg_catalog .pg_extension e on ev .objid = e .oid
222+ where e .extname = ' pg_graphql'
223+ ) then
224+ return;
225+ end if;
226+
227+ drop function if exists graphql_public .graphql ;
228+ create or replace function graphql_public .graphql(
229+ " operationName" text default null ,
230+ query text default null ,
231+ variables jsonb default null ,
232+ extensions jsonb default null
233+ )
234+ returns jsonb
235+ language sql
236+ as $$
237+ select graphql .resolve (
238+ query := query,
239+ variables := coalesce(variables, ' {}' ),
240+ " operationName" := " operationName" ,
241+ extensions := extensions
242+ );
243+ $$;
244+
245+ -- Attach the wrapper to the extension so DROP EXTENSION cascades to it,
246+ -- which in turn triggers set_graphql_placeholder to reinstall the "not enabled" stub.
247+ alter extension pg_graphql add function graphql_public .graphql (text , text , jsonb, jsonb);
248+
249+ grant usage on schema graphql to postgres, anon, authenticated, service_role;
250+ grant execute on function graphql .resolve to postgres, anon, authenticated, service_role;
251+ grant usage on schema graphql to postgres with grant option;
252+ grant usage on schema graphql_public to postgres with grant option;
253+ end;
279254$_$;
280255
281256
@@ -472,6 +447,39 @@ $_$;
472447COMMENT ON FUNCTION extensions.set_graphql_placeholder() IS ' Reintroduces placeholder function for graphql_public.graphql' ;
473448
474449
450+ --
451+ -- Name: graphql(text, text, jsonb, jsonb); Type: FUNCTION; Schema: graphql_public; Owner: -
452+ --
453+
454+ CREATE FUNCTION graphql_public .graphql(" operationName" text DEFAULT NULL ::text , query text DEFAULT NULL ::text , variables jsonb DEFAULT NULL ::jsonb, extensions jsonb DEFAULT NULL ::jsonb) RETURNS jsonb
455+ LANGUAGE plpgsql
456+ AS $$
457+ DECLARE
458+ server_version float;
459+ BEGIN
460+ server_version = (SELECT (SPLIT_PART((select version()), ' ' , 2 ))::float);
461+
462+ IF server_version >= 14 THEN
463+ RETURN jsonb_build_object(
464+ ' errors' , jsonb_build_array(
465+ jsonb_build_object(
466+ ' message' , ' pg_graphql extension is not enabled.'
467+ )
468+ )
469+ );
470+ ELSE
471+ RETURN jsonb_build_object(
472+ ' errors' , jsonb_build_array(
473+ jsonb_build_object(
474+ ' message' , ' pg_graphql is only available on projects running Postgres 14 onwards.'
475+ )
476+ )
477+ );
478+ END IF;
479+ END;
480+ $$;
481+
482+
475483--
476484-- Name: get_auth(text); Type: FUNCTION; Schema: pgbouncer; Owner: -
477485--
@@ -776,7 +784,7 @@ CREATE EVENT TRIGGER issue_pg_cron_access ON ddl_command_end
776784--
777785
778786CREATE EVENT TRIGGER issue_pg_graphql_access ON ddl_command_end
779- WHEN TAG IN (' CREATE FUNCTION ' )
787+ WHEN TAG IN (' CREATE EXTENSION ' )
780788 EXECUTE FUNCTION extensions .grant_pg_graphql_access ();
781789
782790
0 commit comments