#NestJS+Apollo Server 4 - Problems with providing GraphQLModule config

1 messages · Page 1 of 1 (latest)

unborn steeple
#

I recently upgrade from apollo server 3 to 4 in my NestJS+gql+apollo server. I think I have fixed most breaking changes and the server starts up fine now.

However, doing queries from the frontend to the server now results in a CORS error.

Here is my GraphQLModule in my app.module.ts:

 GraphQLModule.forRootAsync<ApolloDriverConfig>({
      driver: ApolloDriver,
      useFactory: async () => ({
        typePaths: ['**/*.graphql'],
        plugins: [ApolloServerPluginUsageReportingDisabled()],
        introspection: process.env.NODE_ENV !== 'test',
        context: args =>
          process.env.NODE_ENV === 'test'
            ? mockContext(args)
            : getContext(args),
        cors: corsSettings,
      }),
    }),

I also have this in my main.ts:

app.enableCors(corsSettings);

Additionally, it does seem like the context in the GraphQLModule is called at all anymore, which I noticed when running my tests (no mock context was available).

Does anyone see any problem with my setup? I can provide more code if needed.

Thanks!

unborn steeple
#

Fixed the cors part by adding app.enableCors() before app.listen() 😃

Still issues with mockContext however... This is what my tests look like

describe('FooBarResolver', () => {
  let module: TestingModule;
  let app: INestApplication;
  let server: ApolloServer;

  beforeEach(async () => {
    module = await getTestModule();
    app = module.createNestApplication();
    await app.init();
    server = getApolloServer(module);
  });

  afterAll(async () => {
    await module.close();
  });

  it('can get foo bar', async () => {
    await setTestPolicies(Subject.foo_bar, Action.read);

    const response = await server.executeOperation({
      query: print(GET_FOO_BAR),
    });
    expect(response.errors).toBeUndefined();
    expect(response.data?.fooBar.length).toBeGreaterThan(0);
  });
});

But somehow its not picking up the gql mockContext , it does run the useFactory function however

unborn steeple
#

Anyone know why it wouldnt run context when running tests?

#

something to do with the new ApolloServer 🤔 ?

mint rose
#

Hi, I realise this was a while ago, but did you find a way around this? Tackling the same issue now