From 2f979fac0b6e784f490fe3b76118a2be6f08e7f7 Mon Sep 17 00:00:00 2001 From: Luciano Alves Date: Sun, 29 Jan 2023 06:56:06 -0300 Subject: [PATCH] feat(apollo): fix cors and add @fastify/cors when wrapped by fastify co-authored: @lpessoa --- packages/apollo/lib/drivers/apollo-base.driver.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/apollo/lib/drivers/apollo-base.driver.ts b/packages/apollo/lib/drivers/apollo-base.driver.ts index b699708f6..960b7633f 100644 --- a/packages/apollo/lib/drivers/apollo-base.driver.ts +++ b/packages/apollo/lib/drivers/apollo-base.driver.ts @@ -13,6 +13,8 @@ import { AbstractGraphQLDriver } from '@nestjs/graphql'; UserInputError, } from 'apollo-server-core'; */ +import { loadPackage } from '@nestjs/common/utils/load-package.util'; + import { GraphQLError, GraphQLFormattedError } from 'graphql'; import * as omit from 'lodash.omit'; import { ApolloDriverConfig } from '../interfaces'; @@ -25,9 +27,8 @@ import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin import { expressMiddleware } from '@apollo/server/express4'; import * as express from 'express'; import * as http from 'node:http'; -import * as cors from 'cors'; -import fastifyApollo, { +import { fastifyApolloHandler, fastifyApolloDrainPlugin, } from '@as-integrations/fastify'; @@ -123,6 +124,8 @@ export abstract class ApolloBaseDriver< hooks?.preStartHook(); } + const cors = loadPackage('cors', null, () => require('cors')); + const { path, typeDefs, resolvers, schema } = options; const httpAdapter = this.httpAdapterHost.httpAdapter; @@ -160,6 +163,10 @@ export abstract class ApolloBaseDriver< hooks?.preStartHook(); } + const cors = loadPackage('@fastify/cors', null, () => + require('@fastify/cors'), + ); + const httpAdapter = this.httpAdapterHost.httpAdapter; const app = httpAdapter.getInstance(); @@ -176,11 +183,10 @@ export abstract class ApolloBaseDriver< app.route({ url: path, - method: ['POST', 'OPTIONS'], + method: ['GET', 'POST', 'OPTIONS'], handler: fastifyApolloHandler(server), }); - await app.register(fastifyApollo(server)); await app.register(cors, options.cors); this.apolloServer = server;