Skip to content

Commit

Permalink
feat(apollo): fix cors and add @fastify/cors when wrapped by fastify
Browse files Browse the repository at this point in the history
co-authored: @lpessoa
  • Loading branch information
luas10c committed Jan 29, 2023
1 parent 3c7db81 commit 2f979fa
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/apollo/lib/drivers/apollo-base.driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand Down

0 comments on commit 2f979fa

Please sign in to comment.