1

My graphql request works good when I test on localhost, but when deployed to railway, it throws cors error. I use apollo angular in frontend and work good in localhost. I use angular v16 and nestjs v9

(async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );
  await app.register(contentParser);
  await app.register(helmet);
  await app.register(fastifyCsrf);

  const config = app.get(ConfigService);
  const client = config.getOrThrow(CLIENT);
  app.enableCors({ origin: client });

  app.useGlobalPipes(
    new ValidationPipe({
      exceptionFactory(validationErros) {
        const firstError = validationErros[0];
        const firstErrorConstraints = firstError.constraints ?? {
          error: 'Validation Error',
        };
        const error = Object.values(firstErrorConstraints)[0];

        throw new BadRequestException(error, {
          cause: new Error(`ValidationException: ${error}`),
        });
      },
    }),
  );
  app.useGlobalFilters(new HttpExceptionsFilter());

  const port = config.get('PORT') || 3000;

  await app.listen(port);

  const logger = new Logger('Bootstrap');
  logger.log(`cors enabled with: ${client}`);
  logger.log(`App listen on: ${port}`);
})();
Mary
  • 205
  • 1
  • 2
  • 6

0 Answers0