Skip to content
postgraphile filter extention that supports polymorphic associations
TypeScript PLpgSQL JavaScript Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Greenkeeper badge CircleCI Known Vulnerabilities

PostGraphile Connection Plugin Polymorphic

This plugin exposes nested filters created by polymorphic associations. Polymorphic associations are defined like this in ruby on rails.


Requires postgraphile@4.2+ and the following plugins appended prior to this plugin:

  • postgraphile-plugin-connection-filter@^1.0.0



The connection filter has already come with very nice and comprehensive nested assocation filters. But it does not support polymorphic associations. For example, if you have a tag table, that is polymorphic associated with multiple tables.

create table taggs(
  id: integer primary_key,
  taggable_type: text,
  taggable_id: integer,

create table user(
  id: integer,
  name: text,

And a record in taggs is

id: 50
taggable_type: 'User'
taggable_id: 1

This means the tag(id:50) is connected to User record of id:1.

If you want to filter the connections like the following:

  userAsTaggable:{id:1} // This does not exist in regular connection filter

and hoping to find the tag(id:50). You won't be able to do it because the userAsTaggable does not exist.

What this plugin does

This plugin will create the forward relationship. (this userAsTaggable) field, and also the backward relationship. (the taggs field on User Connection, and other connections that is associated). Also if the taggable_type and taggable_id are has an unique constraint. The backward filter is not a single object filter, instead of a multi-field, which consist of three fields some,every,none.


yarn add postgraphile-connection-filter-polymorphic

How to use

This plugin uses smart comments to know the association. Originally I was planning to use a distinct query on the table to find all possible associations, but that took a long time and is not very customizable. The smart comments are on the type field

@polymorphicTo User
@polymorphicTo Post

Use the previous example, where a tag is associated with User, and Post.

comment on column taggs.taggable_type is E'@isPolymorphic\n@polymorphicTo User\n@polymorphicTo Post';

The meta information will transfer to a {isPolymorphic:true, polymorphicTo:['User','Post]}.

Note you MUST have at least two entries for @polymorphicTo in the comment. Because otherwise it will be converted as a string, insteady of array.


To establish a test environment, create an empty PostgreSQL database and set a TEST_DATABASE_URL environment variable with your database connection string.

createdb graphile_test
export TEST_DATABASE_URL=postgres://localhost:5432/graphile_test
yarn test
You can’t perform that action at this time.