How to pass the cors parameter to the new version (12.0.1) of @nestjs/graphql?

800 Views Asked by At

In my Nest.js project, I use the following versions of Node.js (both on the Frontend and Backend):

"node": "16.14.2",
"npm": "8.5.0"

I've updated from @nestjs/graphql (and all the packages) version ^9.1.2 to version ^12.0.1 and now I have an error when passing the "cors" parameter to GraphQLModule.forRoot<ApolloDriverConfig>():

    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      subscriptions: {
        'graphql-ws': true,
      },
      buildSchemaOptions: { dateScalarMode: 'timestamp' },
      // enable the code first approach
      autoSchemaFile: join(process.cwd(), 'schema.gql'),
      sortSchema: true,
      playground: process.env.GRAPHQL_PLAYGROUND_ENABLED === 'true',

      cors: {
        origin: [
          'http://localhost:3000',
          'http://127.0.0.1:3000',
          'https://studio.apollographql.com',
        ],
        credentials: true,
      },
      context: ({ req, res }) => ({ req, res }),
    }),

Object literal may only specify known properties, and 'cors' does not exist in type 'ApolloDriverConfig'.

GraphQL Playground works well, but the front-end is broken. Adding // @ts-ignore under the cors parameter doesn't help.

How can I configure cors in the new version of ApolloGraphQL for Nest?

1

There are 1 best solutions below

0
On

You need to enable cors in your main, you can add the following code to your main

app.enableCors({
    origin: 'http://localhost:****',
    credentials: true,
  });