moleculer-postgraphile
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

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 {
  schemastring// Schema name -> required
  pgPoolpg.Pool// pgPool -> required
  options?: PostGraphileCoreOptions// Postgraphile option -> default = {}
  action?: string// action name -> default = 'graphql'
  cache?: { prefixstring, namestring }; // for setting cache -> default = true
}

Test

Before testing, please provision env with docker

yarn provision:dev

And then

yarn test:unit

Package Sidebar

Install

npm i moleculer-postgraphile

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

329 kB

Total Files

20

Last publish

Collaborators

  • lucduong