graphile / crystal

🔮 Graphile's Crystal Monorepo; home to Grafast, PostGraphile, pg-introspection, pg-sql2 and much more!
https://graphile.org/
Other
12.63k stars 572 forks source link

Aliases for table names #598

Closed peay closed 6 years ago

peay commented 7 years ago

To roll out schema changes, I am experimenting with deploying a new version of certain tables (say, with a _v2 suffix) and testing a bit the API before making it available.

To this end, I've been looking for a way to "alias" table names: being able to refer in queries to objectName, and have some internal mapping in PostgraphQL that says whether objectName is table object_name_v1 or object_name_v2.

The obvious candidate is to use views, but since this does not support foreign key references yet, this is a bit limited (see #376). Another way is to move this "mapping" to the clients querying the PostgraphQL API, but this also isn't very satisfactory, especially with a lot of different client types.

Given the limitations with using views, I would find some kind of aliasing feature for table names very useful.

benjie commented 7 years ago

If you simply mean renaming the tables you can do so using the v4 inflector. It's not "official" yet and may undergo breaking changes before the final v4 release, but it should be enough to scratch your itch.

https://github.com/postgraphql/postgraphql/pull/506#issuecomment-328831456

peay commented 7 years ago

This looks pretty great, thanks, will give it a try!

Do you already have a timeline in mind for when this could graduate into an official release?

benjie commented 7 years ago

Not at the moment, sorry :(

benjie commented 6 years ago

Very soon 🙊

You can provide custom inflectors quite easily in v4; e.g. if you wanted to change the patch input to update mutation from *Patch to *ChangeSet you could create a plugin like this:

MyInflectorPlugin.js:

const { makeAddInflectorsPlugin } = require('graphile-utils');

module.exports = makeAddInflectorsPlugin({
  patchType(typeName: string) {
    // return this.upperCamelCase(`${typeName}-patch`);
    return this.upperCamelCase(`${typeName}-change-set`);
  },
});

Then --append-plugins `pwd`/MyInflectorPlugin.js

The built in inflectors are defined in PgBasicsPlugin; you can overwrite any that you want. Be sure to not use arrow functions otherwise you'll override this and lose access to the other inflectors.

https://github.com/graphile/graphile-build/blob/0fce134cac219d196ec97ead668ddbef7140b66c/packages/graphile-build-pg/src/plugins/PgBasicsPlugin.js#L161-L542