postgraphile-table-extension-plugin
v1.0.0
Published
PostGraphile plugin that exten a table's fields with another relation (table/view)
Readme
postgraphile-table-extension-plugin
Motivation
Sometimes we create want to add some table/view's (call table A) to another table (table B), so that when we query for table B, we could also get the fields in table A without go into the association
Example
type User {
id
portfolioByPortfolioId {
id
name
}
}With this extension, it is possible to do the following
query {
allUsers{
id
name
}
}instead of
query{
allUsers{
id
portfolioByPortfolioId {
id
name
}
}
}Description
Currently, to extend a table A to B (adding fields from A to B), it must satisfy the following
- table A and table B must be associated with a foreign key.
a. if A has a foreign key like
b_id. Then this would be aforwardExtension. b. if B has a foregin key likea_id. then adding fields fromAtoBwould be abackwardExtension. - the foreignKey between
AandBshould be an unique foreign key. - The fields that gets copied cannot overlap.
How to Use
npm install postgraphile-table-extension-pluginThen add it to postgraphile
import {tableExtensionPlugin} from 'postgraphile-table-extension-plugin';
createPostGraphileSchema(client, ['p'], {
appendPlugins:[tableExtensionPlugin]
});Then use @smartComment on the foreignKey to indication extension
For example, if you want to add table A's fields to table B, then
comment on table_a.b_id is E'@forwardExtension';or if the foreign key is on table B, then
comment on table_b.a_id is E'@backwardExtension';