NextJS 13.5 causes graphql schema errors

178 Views Asked by At

After updating to NextJS 13.5 I started seeing these errors when deployed on Vercel (but works fine locally):

Error: Schema must contain uniquely named types but contains multiple types named "h". at new GraphQLSchema (/var/task/node_modules/.pnpm/[email protected]/node_modules/graphql/type/schema.js:219:15)

My init code is pretty straightforward in the serverless function:

const schema = buildSchemaSync({
        authChecker: UserAuthority,
        resolvers: [...]
    });

    const plugins = [
        // Install a landing page plugin based on NODE_ENV
        process.env.NODE_ENV === 'production'
            ? ApolloServerPluginLandingPageProductionDefault({
                graphRef: 'my-graph-id@my-graph-variant',
                footer: false,
            })
            : ApolloServerPluginLandingPageLocalDefault({ footer: false }),
    ];

    const server = new ApolloServer({
        schema,
        introspection: true,
        csrfPrevention: true,
        plugins,
        cache: 'bounded'
        // enable GraphQL Playground
    });

    const handler = startServerAndCreateNextHandler(server, {
        context
    })
    await handler(req, res);

This seems to be related to Next optimisations given there are no types named 'h'. Is there a way to ensure every unique name generated as part of optimization's is unique?

2

There are 2 best solutions below

2
meds On BEST ANSWER

The issue is from server side optimisation that minifies class names, which type-graphql relies on to generate its schema.

To disable serverside optimisation only (and prevent bloat on thefrontend) you can set this in your next.config

experimental: {
    serverMinification: false,
}

Looks like it was turned on in 13.5 by default and the cause of the issues.

1
Cloud On

This may be a little drastic, but you could try disabling minimisation in the Webpack config for Next.js.

next.config.js:

config.optimization.minimize = false;

As per https://webpack.js.org/configuration/optimization/#optimizationminimize.

If nothing else, it may help narrow down the cause of the error you are now receiving.