-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2546 from tugascript/tugascript/add-mercurius-hoo…
…ks-object feat(mercurius): add mercurius hooks
- Loading branch information
Showing
21 changed files
with
717 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/mercurius/lib/interfaces/mercurius-hook.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { | ||
MercuriusContext, | ||
onGatewayReplaceSchemaHookHandler, | ||
onResolutionHookHandler, | ||
onSubscriptionEndHookHandler, | ||
onSubscriptionResolutionHookHandler, | ||
preExecutionHookHandler, | ||
preGatewayExecutionHookHandler, | ||
preGatewaySubscriptionExecutionHookHandler, | ||
preParsingHookHandler, | ||
preSubscriptionExecutionHookHandler, | ||
preSubscriptionParsingHookHandler, | ||
preValidationHookHandler, | ||
} from 'mercurius'; | ||
|
||
export interface MercuriusHooksObject< | ||
Context extends MercuriusContext = MercuriusContext, | ||
> { | ||
preParsing?: | ||
| preParsingHookHandler<Context> | ||
| preParsingHookHandler<Context>[]; | ||
preValidation?: | ||
| preValidationHookHandler<Context> | ||
| preValidationHookHandler<Context>[]; | ||
preExecution?: | ||
| preExecutionHookHandler<Context> | ||
| preExecutionHookHandler<Context>[]; | ||
onResolution?: | ||
| onResolutionHookHandler<Context> | ||
| onResolutionHookHandler<Context>[]; | ||
preSubscriptionParsing?: | ||
| preSubscriptionParsingHookHandler<Context> | ||
| preSubscriptionParsingHookHandler<Context>[]; | ||
preSubscriptionExecution?: | ||
| preSubscriptionExecutionHookHandler<Context> | ||
| preSubscriptionExecutionHookHandler<Context>[]; | ||
onSubscriptionResolution?: | ||
| onSubscriptionResolutionHookHandler<Context> | ||
| onSubscriptionResolutionHookHandler<Context>[]; | ||
onSubscriptionEnd?: | ||
| onSubscriptionEndHookHandler<Context> | ||
| onSubscriptionEndHookHandler<Context>[]; | ||
} | ||
|
||
export interface MercuriusHooks< | ||
Context extends MercuriusContext = MercuriusContext, | ||
> { | ||
hooks?: MercuriusHooksObject<Context>; | ||
} | ||
|
||
export interface MercuriusGatewayHooksObject< | ||
Context extends MercuriusContext = MercuriusContext, | ||
> extends MercuriusHooksObject<Context> { | ||
preGatewayExecution?: | ||
| preGatewayExecutionHookHandler<Context> | ||
| preGatewayExecutionHookHandler<Context>[]; | ||
preGatewaySubscriptionExecution?: | ||
| preGatewaySubscriptionExecutionHookHandler<Context> | ||
| preGatewaySubscriptionExecutionHookHandler<Context>[]; | ||
onGatewayReplaceSchema?: | ||
| onGatewayReplaceSchemaHookHandler | ||
| onGatewayReplaceSchemaHookHandler[]; | ||
} | ||
|
||
export interface MercuriusGatewayHooks< | ||
Context extends MercuriusContext = MercuriusContext, | ||
> { | ||
hooks?: MercuriusGatewayHooksObject<Context>; | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/mercurius/lib/utils/register-mercurius-hooks.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { FastifyInstance } from 'fastify'; | ||
import { MercuriusGatewayHooksObject } from '../interfaces/mercurius-hook.interface'; | ||
import { isArray, isNull, isUndefined } from './validation.util'; | ||
|
||
export function registerMercuriusHooks( | ||
app: FastifyInstance, | ||
hooks?: MercuriusGatewayHooksObject | null, | ||
): void { | ||
if (isUndefined(hooks) || isNull(hooks)) { | ||
return; | ||
} | ||
|
||
Object.entries(hooks).forEach(([hookName, hookFn]: [any, any]) => { | ||
if (isUndefined(hookFn) || isNull(hookFn)) { | ||
return; | ||
} | ||
|
||
if (isArray<any>(hookFn)) { | ||
hookFn.forEach((fn) => app.graphql.addHook(hookName, fn)); | ||
return; | ||
} | ||
|
||
app.graphql.addHook(hookName, hookFn); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { NestFactory } from '@nestjs/core'; | ||
import { FastifyAdapter } from '@nestjs/platform-fastify'; | ||
import * as request from 'supertest'; | ||
import { ApplicationModule } from '../hooks/base-array/hooks.module'; | ||
import { MockLogger } from '../hooks/mocks/logger.mock'; | ||
|
||
describe('Base hooks in array format', () => { | ||
let app: INestApplication; | ||
let logger: MockLogger; | ||
|
||
beforeEach(async () => { | ||
logger = new MockLogger(); | ||
app = await NestFactory.create(ApplicationModule, new FastifyAdapter(), { | ||
logger, | ||
}); | ||
await app.init(); | ||
await app.getHttpAdapter().getInstance().ready(); | ||
}); | ||
|
||
it('hooks should be triggered', async () => { | ||
await request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
operationName: null, | ||
variables: {}, | ||
query: '{ getAnimalName }', | ||
}) | ||
.expect(200, { | ||
data: { | ||
getAnimalName: 'cat', | ||
}, | ||
}); | ||
expect(logger.warn).toHaveBeenCalledTimes(8); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
1, | ||
'preParsing1', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
2, | ||
'preParsing2', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
3, | ||
'preValidation1', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
4, | ||
'preValidation2', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
5, | ||
'preExecution1', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
6, | ||
'preExecution2', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
7, | ||
'onResolution1', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
8, | ||
'onResolution2', | ||
'GqlConfigService', | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { NestFactory } from '@nestjs/core'; | ||
import { FastifyAdapter } from '@nestjs/platform-fastify'; | ||
import * as request from 'supertest'; | ||
import { ApplicationModule } from '../hooks/base/hooks.module'; | ||
import { MockLogger } from '../hooks/mocks/logger.mock'; | ||
|
||
describe('Base hooks', () => { | ||
let app: INestApplication; | ||
let logger: MockLogger; | ||
|
||
beforeEach(async () => { | ||
logger = new MockLogger(); | ||
app = await NestFactory.create(ApplicationModule, new FastifyAdapter(), { | ||
logger, | ||
}); | ||
await app.init(); | ||
await app.getHttpAdapter().getInstance().ready(); | ||
}); | ||
|
||
it('hooks should be triggered', async () => { | ||
await request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
operationName: null, | ||
variables: {}, | ||
query: '{ getAnimalName }', | ||
}) | ||
.expect(200, { | ||
data: { | ||
getAnimalName: 'cat', | ||
}, | ||
}); | ||
expect(logger.warn).toHaveBeenCalledTimes(4); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
1, | ||
'preParsing', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
2, | ||
'preValidation', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
3, | ||
'preExecution', | ||
'GqlConfigService', | ||
); | ||
expect(logger.warn).toHaveBeenNthCalledWith( | ||
4, | ||
'onResolution', | ||
'GqlConfigService', | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
Oops, something went wrong.