Skip to content

Commit

Permalink
feat: ✨ rebuild header in gateway app
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Dec 7, 2024
1 parent 7430114 commit 5c837c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 15 additions & 1 deletion apps/gateway/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IntrospectAndCompose } from '@apollo/gateway';
import { IntrospectAndCompose, RemoteGraphQLDataSource } from '@apollo/gateway';
import { gatewayConfig, userAppConfig } from '@shared/config';
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
Expand Down Expand Up @@ -31,6 +31,20 @@ import { AppService } from './app.service';
},
],
}),
buildService: ({ url = '' }) => {
return new RemoteGraphQLDataSource({
url,
willSendRequest({ request, context }) {
// rebuild header
if (Object.keys(context).length > 0 && request.http) {
request.http.headers.set(
'Authorization',
context['req']['headers']['authorization'],
);
}
},
});
},
},
};
},
Expand Down
4 changes: 3 additions & 1 deletion apps/users/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import { AppService } from './app.service';
playground: process.env['NODE_ENV'] !== 'production',
sortSchema: true,
plugins: [ApolloServerPluginInlineTrace()],
context: ({ req }: { req: Request }) => ({ req }), // Make sure request is passed to the context
context: ({ req }: { req: Request }) => {
return { headers: req?.headers };
},
}),
UsersModule,
AuthModule,
Expand Down

0 comments on commit 5c837c5

Please sign in to comment.