graphile / graphile.github.io

PostGraphile (PostGraphQL) and Graphile-Build website - contributions very welcome!
https://www.graphile.org/
26 stars 127 forks source link

Documentation - modify introspection results #125

Open dan-barbulescu opened 5 years ago

dan-barbulescu commented 5 years ago

I'm submitting a ...

PostGraphile version: 4.3.3

You mentioned the possibility of changing introspection results to switch data types. Please provide some examples on how to do this.

Additionally, it would be nice to know how to add things to the introspection results (i.e. to implement additional smart comments).

benjie commented 5 years ago

Introspection results are available via the pgIntrospectionResultsByKind object on Build; but for your changes to take effect you must make any changes before other plugins use the introspection results. One way to achieve this is using the new dependencies system; e.g.:

module.exports = builder => {
  builder.hook(
    // Hook name:
    'build',
    // Hook function:
    build => {
      const attribute = build.pgIntrosectionResultsByKind.attribute.find(
        attr => attr.name === 'my_column' && attr.class && attr.class.name === 'my_table' && attr.class.namespaceName === 'my_schema'
      );
      if (attr) {
        const newTypeId = '23'; // OID 23 is an int4
        attr.typeId = newTypeId;
        attr.type = build.pgIntrospectionResultsByKind.type.find(t => t.id === newTypeId);
      }
      // Must return input, or a derivative thereof
      return build;
    },
    // Provides: We don't particularly provide anything, so empty:
    [],
    // Before: Must be before PgTypes (where the type is first used):
    ['PgTypes'],
    // After: Must be after PgIntrospection (otherwise pgIntrospectionResultsByKind doesn't exist):
    ['PgIntrospection']
  );
};

Adding tags/etc is done in the same way, in this case it'd be attr.tags.myNewTag = 'foo'; you can see an example of this here: https://www.graphile.org/postgraphile/plugin-gallery/#Customisation__OmitMutationsByDefaultPlugin

You can see a list of the types and their fields here:

https://github.com/graphile/graphile-engine/blob/master/packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.d.ts

benjie commented 5 years ago

Moving to website repo