0.1.2 • Published 5 years ago

moleculer-postgraphile v0.1.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Moleculer Postgraphile

This package use for creating mixin for each service which can support

CircleCI Coverage Status License: MIT npm version

Usage

yarn add moleculer-postgraphile
broker.createService({
  name: 'public',
  mixins: [
    PostgraphileMixin({
      schema: 'public',
      pgPool: new Pool({
        connectionString: process.env.DATABASE_URL
      })
    })
  ]
});
  • The mixin will create the graphql action for service.
  • To change the action name, just metion the action in option
{
  schema: 'public',
  action: 'graphile',
  pgPool: ...
}
  • To call graphql from other service:
const query = `
  query { 
    allPubPosts {
      nodes {
        id
        title
        content
      }
    }
  }
`;
const variables = {};
const response = await broker.call('public.graphql', { query, variables });
  • This mixin only create one graphql schema & service for one database schema.
  • For stitching multiple schema from multiple services, use can use introspectionQuery
import { introspectionQuery } from 'graphql';
const schema = broker.call('serviceName.graphql', {
  query: introspectionQuery
});
  • After then, you should create custom ApolloLink which context call to:
broker.call('serviceName.graphql', { query, variables });
  • There is another way to discover the introspection without calling introspectionQuery
  • After creating graphql schema, the mixin will store the introspected query in cacher and set service.settings.hasGraphQLSchema = true
  • And will emit the event graphile.updated with params { schema } // schema stands for Schema Name
  • To get the introspectionQuery:
if (this.broker.cacher) {
  this.broker.cacher.get(`graphile.schema.${schemaName}`);
}
  • Custom cache key
PostgraphileMixin({
  schema: 'public',
  pgPool: new Pool({
    connectionString: process.env.DATABASE_URL
  }),
  cache: {
    prefix: 'my_key_prefix',
    name: 'my_key_name'
  }
});
  • Available options:
export interface MixinOptions {
  schema: string; // Schema name -> required
  pgPool: pg.Pool; // pgPool -> required
  options?: PostGraphileCoreOptions; // Postgraphile option -> default = {}
  action?: string; // action name -> default = 'graphql'
  cache?: { prefix: string, name: string }; // for setting cache -> default = true
}

Test

Before testing, please provision env with docker

yarn provision:dev

And then

yarn test:unit