0.7.47 • Published 4 years ago

@rxdi/graph-gateway v0.7.47

Weekly downloads
14
License
MIT
Repository
github
Last release
4 years ago

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

4 years ago

0.7.47

4 years ago

0.7.39

4 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.5

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago