GraphQL — Omit table from generating under ‘graphiql’. Postgres smart comments.

Pratik Agashe
make it heady
Published in
2 min readFeb 27, 2020

Before we start, some quick backstory: this is a sub-post of the “Building Full-stack web app with PostGraphile and React.” Throughout this post, I have used related table names.

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data.

Graphiql is a graphical interactive in-browser GraphQL IDE. You can see all available queries and mutations based on our schema, which is given to the client.

What if we want to omit some table from generating their mutations from graphql?

If we are using Postgres, then we can add smart comments for a particular table. That will exclude that table from generating a mutation.

There are two ways to add smart comments.

Option 1:

  1. Open `sql shell (psql)`- This is a command line tool for psql. It will ask us which table we want to select.
  2. Enter table name: in our case, postgers-graphql.
  3. Run the following commands, and graphql will remove the mutation from auto-generated graphiql.
comment on table public.knex_migrations is   E'@name knex_migrations\n@omit create,update,delete\nThis is the documentation.';comment on table public.knex_migrations_lock is   E'@name knex_migrations_lock\n@omit create,update,delete\nThis is the documentation.';
Reference to add Smart Comment from SQL Shell (psql)

Option 2:

  1. Open pgAdmin.
  2. Navigate to the database -> schema (public) -> table list.
  3. Right-click on the table knex_migration and select properties
  4. One popup will open. Under comments, add the following lines.
@name knex_migrations
@omit create,update,delete
This is the documentation.

5. Follow the same process for knex_migrations_lock, and paste the following lines.

@name knex_migrations_lock
@omit create,update,delete
This is the documentation.
Reference to add Smart Comment from pgAdmin 4

That’s it!

Now when we reload our graphiql, we’ll find the mutations for these tables are no longer there.

Thanks!

P.S: We’re hiring at Heady! 👋 If you would like to come work with us on some really cool apps please check out our careers page.

--

--