Skip to content

Commit

Permalink
fix(gql): updates gql types to work with apollo > 2.11
Browse files Browse the repository at this point in the history
As of apollo-server-* > 2.11.0 `esModuleInterop` in the tsconfig needed to be turned to `true`. This
is now the case for the project. Apollo-server-fatify has been udpated as well to accommodate the
ability to work with fastify as a graphql server. Minor adjustments still needed
  • Loading branch information
jmcdo29 committed Jul 18, 2020
1 parent b2d053e commit a097842
Show file tree
Hide file tree
Showing 21 changed files with 2,265 additions and 2,594 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'sonarjs/cognitive-complexity': ['error', 20],
'sort-imports': [
'error',
Expand Down
8 changes: 7 additions & 1 deletion integration/src/gql/gql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { GqlResolver } from './gql.resolver';
@Module({
imports: [
GraphQLModule.forRoot({
context: ({ req, res }) => ({ req, res }),
context: ({ req, res, request, reply }) => {
if (req && res) {
return { req, res };
} else if (request && reply) {
return { req: request, res: reply };
}
},
autoSchemaFile: true,
}),
],
Expand Down
2 changes: 1 addition & 1 deletion integration/test/utils/gql-promise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { request } from 'http';

export const gqlPromise = (url: string, query: object) =>
export const gqlPromise = (url: string, query: Record<string, unknown>) =>
new Promise((resolve, reject) => {
const req = request(
url,
Expand Down
2 changes: 1 addition & 1 deletion integration/test/utils/ws-promise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as WebSocket from 'ws';
import WebSocket from 'ws';

export const createConnection = (
client: (url: string) => SocketIOClient.Socket | WebSocket,
Expand Down
6 changes: 3 additions & 3 deletions integration/test/ws.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from '@ogma/nestjs-module';
import { SocketIOParser } from '@ogma/platform-socket.io';
import { WsParser } from '@ogma/platform-ws';
import * as Io from 'socket.io-client';
import * as WebSocket from 'ws';
import Io from 'socket.io-client';
import WebSocket from 'ws';
import { WsModule } from '../src/ws/ws.module';
import {
createConnection,
Expand Down Expand Up @@ -44,7 +44,7 @@ describe.each`
protocol: 'http' | 'ws';
sendMethod: 'send' | 'emit';
serializer: (message: string) => string;
deserializer: (message: string) => string | object;
deserializer: (message: string) => string | Record<string, unknown>;
}) => {
let app: INestApplication;
let interceptor: OgmaInterceptor;
Expand Down
56 changes: 28 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,52 +51,52 @@
},
"devDependencies": {
"@commitlint/cli": "^9.1.1",
"@commitlint/config-conventional": "^8.3.4",
"@golevelup/ts-jest": "^0.2.1",
"@commitlint/config-conventional": "^9.1.1",
"@golevelup/ts-jest": "^0.3.0",
"@grpc/proto-loader": "^0.5.4",
"@nestjs/cli": "^7.0.2",
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/graphql": "^7.0.0",
"@nestjs/microservices": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
"@nestjs/cli": "^7.4.1",
"@nestjs/common": "^7.3.2",
"@nestjs/core": "^7.3.2",
"@nestjs/graphql": "^7.5.5",
"@nestjs/microservices": "^7.3.2",
"@nestjs/platform-express": "^7.3.2",
"@nestjs/platform-fastify": "^7.0.0",
"@nestjs/platform-socket.io": "^7.0.0",
"@nestjs/platform-ws": "^7.0.0",
"@nestjs/schematics": "^7.0.0",
"@nestjs/websockets": "^7.0.0",
"@types/bunyan": "^1.8.6",
"@types/express": "^4.17.0",
"@types/express": "^4.17.7",
"@types/jest": "^26.0.4",
"@types/morgan": "^1.9.0",
"@types/morgan": "^1.9.1",
"@types/node": "^14.0.23",
"@types/pino": "^6.0.0",
"@types/socket.io": "^1.4.33",
"@types/socket.io": "^2.1.8",
"@types/socket.io-client": "^1.4.32",
"@types/ws": "^7.2.4",
"@typescript-eslint/eslint-plugin": "^2.15.0",
"@typescript-eslint/parser": "^2.15.0",
"@typescript-eslint/eslint-plugin": "^3.6.1",
"@typescript-eslint/parser": "^3.6.1",
"amqp-connection-manager": "^3.2.0",
"amqplib": "^0.5.6",
"apollo-server-express": "^2.11.0",
"apollo-server-fastify": "^2.11.0",
"bunyan": "^1.8.12",
"amqplib": "^0.6.0",
"apollo-server-express": "2.16.0",
"apollo-server-fastify": "2.16.0",
"bunyan": "^1.8.14",
"conventional-changelog-cli": "^2.0.31",
"cz-conventional-changelog": "^3.1.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint": "^7.4.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-sonarjs": "^0.5.0",
"express": "^4.17.0",
"fastify": "^2.13.0",
"graphql": "^15.3.0",
"graphql-tools": "^6.0.13",
"graphql-tools": "^6.0.14",
"grpc": "^1.24.2",
"husky": "^4.2.3",
"jest": "^25.1.0",
"husky": "^4.2.5",
"jest": "^26.1.0",
"kafkajs": "^1.12.0",
"lerna": "^3.20.2",
"lint-staged": "^10.0.8",
"lint-staged": "^10.2.11",
"morgan": "^1.10.0",
"mqtt": "^4.0.1",
"nats": "^1.4.9",
Expand All @@ -106,11 +106,11 @@
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.0",
"rxjs": "^6.0.0",
"ts-jest": "^25.2.1",
"ts-loader": "^8.0.0",
"ts-jest": "^26.1.3",
"ts-loader": "^8.0.1",
"ts-node": "^8.8.1",
"typescript": "^3.8.3",
"winston": "^3.2.1",
"typescript": "^3.9.7",
"winston": "^3.3.3",
"ws": "^7.2.3"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions packages/logger/src/command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function wrapInParens(message: string): string {
return '[' + message + ']';
}

function isOgmaFormat(log: object): log is OgmaLog {
function isOgmaFormat(log: Record<string, unknown>): log is OgmaLog {
return standardKeys.every((key) =>
Object.prototype.hasOwnProperty.call(log, key),
);
Expand Down Expand Up @@ -100,7 +100,7 @@ async function rehydrate(
.map((log) => JSON.parse(log))
.forEach((log: OgmaLog) => {
const { time, application, context, pid, level, ...rest } = log;
let message: string | object;
let message: string | Record<string, unknown>;
if (rest.message) {
message = getMessage(log);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/nestjs-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"test:cov": "jest --coverage"
},
"dependencies": {
"@golevelup/nestjs-modules": "0.4.0",
"@golevelup/nestjs-modules": "0.4.1",
"@ogma/logger": "^0.1.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject } from '@nestjs/common';
import { createProviderToken } from '../ogma.provider';

export const OgmaLogger = (topic: string | Function) =>
export const OgmaLogger = (topic: string | (() => any)) =>
Inject(createProviderToken(typeof topic === 'function' ? topic.name : topic));
2 changes: 1 addition & 1 deletion packages/nestjs-module/src/ogma.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class OgmaModule {
*
* @param context string context for the OgmaService to use in logging
*/
static forFeature(context: string | Function): DynamicModule {
static forFeature(context: string | (() => any)): DynamicModule {
const providers: Provider[] = createLoggerProviders(context);
return {
imports: [OgmaCoreModule.Deferred],
Expand Down
5 changes: 3 additions & 2 deletions packages/nestjs-module/src/ogma.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Ogma, OgmaOptions } from '@ogma/logger';
import { Observable } from 'rxjs';
import {
OGMA_INSTANCE,
OGMA_SERVICE_TOKEN,
Expand All @@ -25,7 +26,7 @@ import {
import { OgmaInterceptor } from './interceptor/ogma.interceptor';

export class NoopInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler) {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle();
}
}
Expand Down Expand Up @@ -88,7 +89,7 @@ export function createProviderToken(topic: string): string {
return OGMA_SERVICE_TOKEN + ':' + topic;
}

export function createLoggerProviders(topic: string | Function): Provider[] {
export function createLoggerProviders(topic: string | (() => any)): Provider[] {
topic = typeof topic === 'function' ? topic.name : topic;
const token = createProviderToken(topic);
return [
Expand Down
16 changes: 12 additions & 4 deletions packages/nestjs-module/test/delegator.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ describe('DelegatorService', () => {
describe('getContextSuccessString', () => {
const data = 'someData';
it(logProperly('http'), () => {
const ctxMock = createMock<ExecutionContext>(httpContext);
const ctxMock = createMock<ExecutionContext>(
httpContext as Partial<ExecutionContext>,
);
const spy = spyFactory(http, 'getSuccessContext').mockReturnValueOnce(
parserReturn,
);
Expand Down Expand Up @@ -155,7 +157,9 @@ describe('DelegatorService', () => {
const spy = spyFactory(http, 'getErrorContext').mockReturnValueOnce(
parserReturn,
);
const ctxMock = createMock<ExecutionContext>(httpContext);
const ctxMock = createMock<ExecutionContext>(
httpContext as Partial<ExecutionContext>,
);
expect(
delegate.getContextErrorString(error, ctxMock, startTime, options),
).toBe(parsedString);
Expand Down Expand Up @@ -203,7 +207,9 @@ describe('DelegatorService', () => {
const spy = spyFactory(http, 'getSuccessContext').mockReturnValueOnce(
parserReturn,
);
const ctxMock = createMock<ExecutionContext>(httpContext);
const ctxMock = createMock<ExecutionContext>(
httpContext as Partial<ExecutionContext>,
);
expect(
delegate.getContextSuccessString('data', ctxMock, startTime, {
json: true,
Expand Down Expand Up @@ -232,7 +238,9 @@ describe('DelegatorService', () => {
protocol: 'HTTP/1.1',
status: '200',
});
const ctxMock = createMock<ExecutionContext>(httpContext);
const ctxMock = createMock<ExecutionContext>(
httpContext as Partial<ExecutionContext>,
);
expect(
delegate.getContextSuccessString(null, ctxMock, startTime, options),
).toBe('127.0.0.1 - GET / HTTP/1.1 200 2ms - 0');
Expand Down
19 changes: 11 additions & 8 deletions packages/nestjs-module/test/ogma.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const nullifyOption = (

const logMock = jest.fn();

const httpContext = {
getType: () => 'http',
getArgs: () => [1, 2, 3],
};

describe('OgmaInterceptor', () => {
let interceptor: OgmaInterceptor;
let delegator: DelegatorService;
Expand Down Expand Up @@ -82,10 +87,9 @@ describe('OgmaInterceptor', () => {
delegateSpy.mockClear();
});
it('should log data', (done) => {
const ctxMock = createMock<ExecutionContext>({
getType: () => 'http',
getArgs: () => [1, 2, 3],
});
const ctxMock = createMock<ExecutionContext>(
httpContext as Partial<ExecutionContext>,
);
interceptor.intercept(ctxMock, callHandler).subscribe({
next: () => {
return;
Expand Down Expand Up @@ -132,10 +136,9 @@ describe('OgmaInterceptor', () => {
delegateSpy.mockClear();
});
it('should log error', (done) => {
const ctxMock = createMock<ExecutionContext>({
getType: () => 'http',
getArgs: () => [1, 2, 3],
});
const ctxMock = createMock<ExecutionContext>(
httpContext as Partial<ExecutionContext>,
);
interceptor.intercept(ctxMock, callHandler).subscribe({
next: () => {
throw new Error('Logging data in error');
Expand Down
4 changes: 2 additions & 2 deletions packages/platform-fastify/src/fastify-interceptor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export class FastifyParser extends AbstractInterceptorService {
return inColor ? this.wrapInColor(status) : status.toString();
}

private getRequest(context: ExecutionContext): FastifyRequest {
getRequest(context: ExecutionContext): FastifyRequest {
return context.switchToHttp().getRequest<FastifyRequest>();
}

private getResponse(context: ExecutionContext): FastifyReply<ServerResponse> {
getResponse(context: ExecutionContext): FastifyReply<ServerResponse> {
return context.switchToHttp().getResponse<FastifyReply<ServerResponse>>();
}

Expand Down
7 changes: 6 additions & 1 deletion packages/platform-graphql-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@
"@nestjs/common": "^7.0.6",
"@nestjs/core": "^7.0.6",
"@ogma/nestjs-module": "^0.1.0",
"@ogma/platform-fastify": "^0.1.0",
"apollo-server-fastify": "^2.16.0",
"graphql": "^15.3.0",
"graphql-tools": "^6.0.14",
"rxjs": "^6.5.4"
},
"peerDependencies": {
"@ogma/nestjs-module": "^0.1.0"
"@ogma/nestjs-module": "^0.1.0",
"@ogma/platform-fastify": "^0.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,23 @@
import { ExecutionContext, HttpException, Injectable } from '@nestjs/common';
import { ExecutionContext, Injectable } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { AbstractInterceptorService } from '@ogma/nestjs-module';
import { FastifyParser } from '@ogma/platform-fastify';
import { FastifyReply, FastifyRequest } from 'fastify';

@Injectable()
export class GraphQLFastifyParser extends AbstractInterceptorService {
getCallerIp(context: ExecutionContext) {
const req = this.getRequest(context);
return req.ips && req.ips.length ? req.ips.join(' ') : req.ip;
}

getCallPoint(context: ExecutionContext) {
const req = this.getRequest(context);
return req.url;
}

getMethod(context: ExecutionContext) {
export class GraphQLFastifyParser extends FastifyParser {
getMethod(context: ExecutionContext): string {
return this.getContext(context).getInfo().operation.operation;
}

getProtocol(context: ExecutionContext) {
const req = this.getRequest(context);
return `HTTP/${req.httpVersionMajor}.${req.httpVersionMinor}`;
}

getStatus(
context: ExecutionContext,
inColor: boolean,
error?: Error | HttpException,
) {
const status = error ? this.determineStatusFromError(error) : 200;
return inColor ? this.wrapInColor(status) : status.toString();
}

private determineStatusFromError(error: Error | HttpException) {
try {
return (error as HttpException).getStatus();
} catch (err) {
return 500;
}
}

private getContext(context: ExecutionContext): GqlExecutionContext {
return GqlExecutionContext.create(context);
}

private getRequest(context: ExecutionContext) {
getRequest(context: ExecutionContext): FastifyRequest {
return this.getContext(context).getContext().req;
}

getResponse(context: ExecutionContext): FastifyReply<any> {
return this.getContext(context).getContext().res;
}
}
Loading

0 comments on commit a097842

Please sign in to comment.