JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q32793F
  • License MIT

Package Exports

  • @rxdi/graph-gateway

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rxdi/graph-gateway) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@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 };
}