inngest / envelop-plugin-inngest

An envelop plugin that sends GraphQL response data to Inngest to help build event-driven applications.
MIT License
10 stars 3 forks source link

Bug: Running plugin setup can duplicate inngestPlugin in the GraphQLHandler extraPlugins #67

Open dthyresson opened 1 year ago

dthyresson commented 1 year ago

If the GraphQLHandler is:

import { createGraphQLHandler } from '@redwoodjs/graphql-server'

import directives from 'src/directives/**/*.{js,ts}'
import sdls from 'src/graphql/**/*.sdl.{js,ts}'
import services from 'src/services/**/*.{js,ts}'

import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
import { inngestPlugin } from 'src/plugins/useInngest'

export const handler = createGraphQLHandler({
  loggerConfig: { logger, options: {} },
  directives,
  sdls,
  services,

  onException: () => {
    // Disconnect from your database with an unhandled exception.
    db.$disconnect()
  },

  extraPlugins: [inngestPlugin],
})

where extraPlugins: [inngestPlugin], is already set, re-running and forcing the plugin setup duplicates:

import { createGraphQLHandler } from '@redwoodjs/graphql-server'

import directives from 'src/directives/**/*.{js,ts}'
import sdls from 'src/graphql/**/*.sdl.{js,ts}'
import services from 'src/services/**/*.{js,ts}'

import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
import { inngestPlugin } from 'src/plugins/useInngest'

export const handler = createGraphQLHandler({
  loggerConfig: { logger, options: {} },
  directives,
  sdls,
  services,

  onException: () => {
    // Disconnect from your database with an unhandled exception.
    db.$disconnect()
  },

  extraPlugins: [inngestPlugin],
  extraPlugins: [inngestPlugin]
})
dthyresson commented 1 year ago

While there is a test for when the extraPlugins exists:

it('when GraphQL handler already has useInngest setup', () => {
          const source = fs.readFileSync(path.join(__dirname, '__testfixtures__', 'plugin', 'exists.input.js'), 'utf8');
          const output = pluginTransform(
            { path: '../__testfixtures__/exists.input.js', source },
            { jscodeshift },
            { ...defaultJscodeshiftOpts }
          );
          expect(output).toMatchSnapshot();
        });

with a fixture:

import { createGraphQLHandler } from '@redwoodjs/graphql-server';

import directives from 'src/directives/**/*.{js,ts}';
import sdls from 'src/graphql/**/*.sdl.{js,ts}';
import services from 'src/services/**/*.{js,ts}';

import { getCurrentUser } from 'src/lib/auth';
import { db } from 'src/lib/db';
import { logger } from 'src/lib/logger';

import { inngestPlugin } from 'src/inngest/plugin';

export const handler = createGraphQLHandler({
  getCurrentUser,
  loggerConfig: { logger, options: {} },
  directives,
  sdls,
  services,
  extraPlugins: [inngestPlugin],
  onException: () => {
    // Disconnect from your database with an unhandled exception.
    db.$disconnect();
  },
});

and that test passes, for some reason the plugins still are duplicated.

dthyresson commented 1 year ago

While test pass, for some reason when running in a RW project, the codemod behaves differently.

Something in this detection behaves differently:

      const extraPluginsProp = optionsProps.properties?.find(
        (p): p is Property => j.Property.check(p) && j.Identifier.check(p.key) && p.key.name === 'extraPlugins'
      );

Perhaps some type guard issue?

Tobbe commented 1 year ago

I ran into something similar where I already had useResponseCache configured

export const handler = createGraphQLHandler({
  loggerConfig: { logger, options: {} },
  directives,
  sdls,
  services,
  cors: {
    origin: '*',
    credentials: true,
  },
  extraPlugins:
    process.env.NODE_ENV === 'production'
      ? [useResponseCache({ cache, session: () => null, ...cacheConfig })]
      : [],
  onException: () => {
    // Disconnect from your database with an unhandled exception.
    db.$disconnect()
  },
  extraPlugins: [inngestPlugin],
})

Got an extra extraPlugins at the end