@rxdi/graph-gateway
TypeScript icon, indicating that this package has built-in type declarations

0.7.47 • Public • Published

@rxdi/graph-gateway

Create easy graph gatway from existing Graphql endpoints

Features

  • Merge multiple Remote graphql servers to single schema
  • Microservice architecture

Installation

npm i -g @rxdi/graph-gateway

Usage

import {
  Module,
  CoreModule,
  Bootstrap,
  ON_REQUEST_HANDLER,
  GRAPHQL_PLUGIN_CONFIG
} from '@gapi/core';
import {
  MicroserviceModule,
  AFTER_MIDDLEWARE,
  BEFORE_MIDDLEWARE,
  MiddlewareOptions
} from '@rxdi/graph-gateway';
import { ResponseToolkit } from 'hapi';

@Module({
  providers: [
    {
      provide: ON_REQUEST_HANDLER,
      deps: [GRAPHQL_PLUGIN_CONFIG],
      useFactory: (config: GRAPHQL_PLUGIN_CONFIG) => async (
        next,
        request: Request,
        h: ResponseToolkit,
        err: Error
      ) => {
        config.graphqlOptions.context = config.graphqlOptions.context || {};
        config.graphqlOptions.context['headers'] = request.headers;
        console.log('On request handler');
        return next();
      }
    },
    {
      provide: BEFORE_MIDDLEWARE,
      useFactory: () => (options: MiddlewareOptions) => {
        console.log('Before Middleware', options.headers);
        return options;
      }
    },
    // Notice how after middleware has multi injection so you can specify which values to return
    // This behaviour is valid only for After middleware
    {
      provide: AFTER_MIDDLEWARE,
      useFactory: () => (res, options: MiddlewareOptions) => {
        if (res.statusCode && res.statusCode !== 200) {
          throw new Error('Unauthorized');
        }
        console.log('RES1', res);
        return res;
      }
    },
    {
      provide: AFTER_MIDDLEWARE,
      useFactory: () => (res, options: MiddlewareOptions) => {
        console.log('RES2', res);
        return res;
      }
    }
  ],
  imports: [
    MicroserviceModule.forRoot([
      {
        link: 'http://localhost:9000/graphql',
        name: 'Main'
      },
      {
        link: 'http://localhost:9001/graphql',
        name: 'Main2'
      }
    ]),
    CoreModule.forRoot()
  ]
})
export class AppModule {}

Bootstrap(AppModule).subscribe(() => console.log('Started'));

Middleware options

export interface MicroserviceInterface {
  name: string;
  link: string;
}
export interface MiddlewareOptions {
  context: Object;
  operationName: string;
  variables: Object;
  query: string;
  microservice: MicroserviceInterface;
  method: string | 'POST' | 'GET';
  headers: { [key: string]: string };
}

Dependencies (3)

Dev Dependencies (4)

Package Sidebar

Install

npm i @rxdi/graph-gateway

Weekly Downloads

0

Version

0.7.47

License

MIT

Unpacked Size

32.5 kB

Total Files

20

Last publish

Collaborators

  • rxdi