|
| 1 | +-- migrate:up |
| 2 | + |
| 3 | +create or replace function extensions.grant_pg_graphql_access() |
| 4 | + returns event_trigger |
| 5 | + language plpgsql |
| 6 | +AS $func$ |
| 7 | +DECLARE |
| 8 | + func_is_graphql_resolve bool; |
| 9 | +BEGIN |
| 10 | + func_is_graphql_resolve = ( |
| 11 | + SELECT n.proname = 'resolve' |
| 12 | + FROM pg_event_trigger_ddl_commands() AS ev |
| 13 | + LEFT JOIN pg_catalog.pg_proc AS n |
| 14 | + ON ev.objid = n.oid |
| 15 | + ); |
| 16 | + |
| 17 | + IF func_is_graphql_resolve |
| 18 | + THEN |
| 19 | + -- Update public wrapper to pass all arguments through to the pg_graphql resolve func |
| 20 | + DROP FUNCTION IF EXISTS graphql_public.graphql; |
| 21 | + create or replace function graphql_public.graphql( |
| 22 | + "operationName" text default null, |
| 23 | + query text default null, |
| 24 | + variables jsonb default null, |
| 25 | + extensions jsonb default null |
| 26 | + ) |
| 27 | + returns jsonb |
| 28 | + language sql |
| 29 | + as $$ |
| 30 | + select graphql.resolve( |
| 31 | + query := query, |
| 32 | + variables := coalesce(variables, '{}'), |
| 33 | + "operationName" := "operationName", |
| 34 | + extensions := extensions |
| 35 | + ); |
| 36 | + $$; |
| 37 | + |
| 38 | + -- This hook executes when `graphql.resolve` is created. That is not necessarily the last |
| 39 | + -- function in the extension so we need to grant permissions on existing entities AND |
| 40 | + -- update default permissions to any others that are created after `graphql.resolve` |
| 41 | + grant usage on schema graphql to postgres, anon, authenticated, service_role; |
| 42 | + grant select on all tables in schema graphql to postgres, anon, authenticated, service_role; |
| 43 | + grant execute on all functions in schema graphql to postgres, anon, authenticated, service_role; |
| 44 | + grant all on all sequences in schema graphql to postgres, anon, authenticated, service_role; |
| 45 | + alter default privileges in schema graphql grant all on tables to postgres, anon, authenticated, service_role; |
| 46 | + alter default privileges in schema graphql grant all on functions to postgres, anon, authenticated, service_role; |
| 47 | + alter default privileges in schema graphql grant all on sequences to postgres, anon, authenticated, service_role; |
| 48 | + |
| 49 | + -- Allow postgres role to allow granting usage on graphql and graphql_public schemas to custom roles |
| 50 | + grant usage on schema graphql_public to postgres with grant option; |
| 51 | + grant usage on schema graphql to postgres with grant option; |
| 52 | + END IF; |
| 53 | + |
| 54 | +END; |
| 55 | +$func$; |
| 56 | + |
| 57 | +-- Cycle the extension off and back on to apply the permissions update. |
| 58 | + |
| 59 | +drop extension if exists pg_graphql; |
| 60 | +-- Avoids limitation of only being able to load the extension via dashboard |
| 61 | +-- Only install as well if the extension is actually installed |
| 62 | +DO $$ |
| 63 | +DECLARE |
| 64 | + graphql_exists boolean; |
| 65 | +BEGIN |
| 66 | + graphql_exists = ( |
| 67 | + select count(*) = 1 |
| 68 | + from pg_available_extensions |
| 69 | + where name = 'pg_graphql' |
| 70 | + ); |
| 71 | + |
| 72 | + IF graphql_exists |
| 73 | + THEN |
| 74 | + create extension if not exists pg_graphql; |
| 75 | + END IF; |
| 76 | +END $$; |
| 77 | + |
| 78 | +-- migrate:down |
0 commit comments