-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat(nestjs-gateway): migrate express gateway to apollo server #417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
97d411c
feat(nestjs-gateway): migrate express gateway to apollo server
TorinAsakura 4e72360
fix(nestjs-gateway): preserve apollo runtime compatibility
TorinAsakura f5b142e
refactor(nestjs-gateway): split http gateway boundaries
TorinAsakura 532d769
fix(nestjs-gateway): restore apollo mesh runtime boot
TorinAsakura 995025f
fix(nestjs-gateway): preserve apollo health route
TorinAsakura 61a43b3
fix(nestjs-gateway): preserve grpc error details
TorinAsakura e33e4b7
fix(nestjs-gateway): wrap subscription request context
TorinAsakura 90569c7
fix(nestjs-gateway): load array type definition paths
TorinAsakura f1256c0
fix(nestjs-gateway): avoid internal module barrel imports
TorinAsakura 6300293
fix(nestjs-gateway): harden grpc status detail decoding
TorinAsakura 6726e59
fix(nestjs-gateway): normalize grpc status detail type names
TorinAsakura 4dcd174
fix(nestjs-gateway): accept cross realm grpc detail constructors
TorinAsakura 70f8856
fix(nestjs-gateway): resolve grpc detail constructors
TorinAsakura 3506ef5
fix(nestjs-gateway): address apollo runtime review findings
TorinAsakura 0ce1476
chore(nestjs-gateway): flatten module interfaces naming
TorinAsakura 15594b9
chore(nestjs-gateway): move http interfaces to http boundary
TorinAsakura 74be3fd
chore(nestjs-gateway): isolate graphql upload adapter
TorinAsakura 96c6c71
chore(nestjs-gateway): use public grpc connectivity export
TorinAsakura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,2 @@ | ||
| export { PubSub } from 'graphql-subscriptions' | ||
|
|
||
| export * from './enums/index.js' | ||
| export * from './module/index.js' |
This file contains hidden or 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,77 @@ | ||
| import type { ServiceError } from '@grpc/grpc-js' | ||
| import type { GraphQLFormattedError } from 'graphql' | ||
|
|
||
| import { ErrorStatus } from '@atls/grpc-error-status' | ||
|
|
||
| type ErrorExtensions = { | ||
| exception?: Record<string, unknown> | ServiceError | ||
| } | ||
|
|
||
| const grpcErrorMessagePattern = /^(?<code>\d+)\s+(?<status>[A-Z_]+):\s*(?<message>.*)$/ | ||
|
|
||
| const isGrpcErrorStatus = (error: unknown): error is ServiceError => { | ||
| if (typeof error !== 'object' || error === null) { | ||
| return false | ||
| } | ||
|
|
||
| const candidate = error as Partial<ServiceError> | ||
|
|
||
| return ( | ||
| Number(candidate.code) >= 0 && | ||
| candidate.metadata !== undefined && | ||
| candidate.details !== undefined | ||
| ) | ||
| } | ||
|
|
||
| const formatGrpcError = (error: ServiceError): Record<string, unknown> => | ||
| ErrorStatus.fromServiceError(error).toObject() | ||
|
|
||
| const formatGrpcMessageError = (error: Error): Record<string, unknown> | undefined => { | ||
| const match = grpcErrorMessagePattern.exec(error.message) | ||
|
|
||
| if (!match?.groups) { | ||
| return undefined | ||
| } | ||
|
|
||
| return { | ||
| status: match.groups.status, | ||
| code: Number(match.groups.code), | ||
| message: match.groups.message, | ||
| details: [], | ||
| } | ||
| } | ||
|
|
||
| export const formatError = ( | ||
| error: GraphQLFormattedError & { extensions?: ErrorExtensions }, | ||
| exceptionOverride?: unknown | ||
| ): GraphQLFormattedError => { | ||
| const exception = isGrpcErrorStatus(exceptionOverride) | ||
| ? exceptionOverride | ||
| : error.extensions?.exception | ||
|
|
||
| if (exception && isGrpcErrorStatus(exception)) { | ||
| return { | ||
| ...error, | ||
| extensions: { | ||
| ...error.extensions, | ||
| exception: formatGrpcError(exception), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| if (exceptionOverride instanceof Error) { | ||
| const formattedException = formatGrpcMessageError(exceptionOverride) | ||
|
|
||
| if (formattedException) { | ||
| return { | ||
| ...error, | ||
| extensions: { | ||
| ...error.extensions, | ||
| exception: formattedException, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return error | ||
| } |
This file contains hidden or 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,2 @@ | ||
| export * from './format.js' | ||
| export * from './unsupported.js' |
84 changes: 84 additions & 0 deletions
84
packages/nestjs-gateway/src/mesh/errors/tests/format.test.ts
This file contains hidden or 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,84 @@ | ||
| import assert from 'node:assert/strict' | ||
| import { describe } from 'node:test' | ||
| import { it } from 'node:test' | ||
|
|
||
| import { BadRequest } from '@atls/grpc-error-status' | ||
| import { ErrorStatus } from '@atls/grpc-error-status' | ||
| import { status } from '@grpc/grpc-js' | ||
|
|
||
| import { formatError } from '../format.js' | ||
|
|
||
| describe('formatError', () => { | ||
| it('formats direct gRPC service errors', () => { | ||
| const serviceError = new ErrorStatus(status.INVALID_ARGUMENT, 'Test').toServiceError() | ||
|
|
||
| const formattedError = formatError({ | ||
| message: '3 INVALID_ARGUMENT: Test', | ||
| extensions: { | ||
| exception: serviceError, | ||
| }, | ||
| }) | ||
|
|
||
| assert.deepEqual(formattedError.extensions?.exception, { | ||
| status: 'INVALID_ARGUMENT', | ||
| code: status.INVALID_ARGUMENT, | ||
| message: 'Test', | ||
| details: [], | ||
| }) | ||
| }) | ||
|
|
||
| it('preserves gRPC status details', () => { | ||
| const violation = new BadRequest.FieldViolation() | ||
| const badRequest = new BadRequest() | ||
|
|
||
| violation.setField('id') | ||
| violation.setDescription('id must be an email') | ||
| badRequest.addFieldViolations(violation) | ||
| const serviceError = new ErrorStatus(status.INVALID_ARGUMENT, 'Request validation failed') | ||
| .addDetail(badRequest) | ||
| .toServiceError() | ||
|
|
||
| const formattedError = formatError({ | ||
| message: '3 INVALID_ARGUMENT: Request validation failed', | ||
| extensions: { | ||
| exception: serviceError, | ||
| }, | ||
| }) | ||
|
|
||
| assert.deepEqual(formattedError.extensions?.exception, { | ||
| status: 'INVALID_ARGUMENT', | ||
| code: status.INVALID_ARGUMENT, | ||
| message: 'Request validation failed', | ||
| details: [ | ||
| { | ||
| '@type': 'type.googleapis.com/google.rpc.BadRequest', | ||
| fieldViolationsList: [ | ||
| { | ||
| field: 'id', | ||
| description: 'id must be an email', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }) | ||
| }) | ||
|
|
||
| it('formats gRPC errors unwrapped as regular errors', () => { | ||
| const formattedError = formatError( | ||
| { | ||
| message: '3 INVALID_ARGUMENT: Test', | ||
| extensions: { | ||
| code: 'INTERNAL_SERVER_ERROR', | ||
| }, | ||
| }, | ||
| new Error('3 INVALID_ARGUMENT: Test') | ||
| ) | ||
|
|
||
| assert.deepEqual(formattedError.extensions?.exception, { | ||
| status: 'INVALID_ARGUMENT', | ||
| code: status.INVALID_ARGUMENT, | ||
| message: 'Test', | ||
| details: [], | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or 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,6 @@ | ||
| export class GatewayUnsupportedHttpAdapterError extends Error { | ||
| constructor(adapter: string) { | ||
| super(`Gateway HTTP adapter "${adapter}" is not supported`) | ||
| this.name = 'GatewayUnsupportedHttpAdapterError' | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.