Files
AFFiNE-Mirror/packages/backend/server/src/base/graphql/index.ts
T
Jachin e08fc5ef06 feat(server): change the playground option to GraphiQL. (#13451)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* The GraphQL interactive UI is now available only in development
environments and will not be accessible in production. This change
affects only the availability of the interactive interface; public
exports and API context types remain unchanged. Users in development can
continue to use the tool as before, while production deployments will no
longer expose the interactive UI.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: DarkSky <25152247+darkskygit@users.noreply.github.com>
2025-09-21 16:08:30 +00:00

78 lines
2.1 KiB
TypeScript

import './config';
import { join } from 'node:path';
import type { ApolloDriverConfig } from '@nestjs/apollo';
import { ApolloDriver } from '@nestjs/apollo';
import { Global, Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import type { Request, Response } from 'express';
import { NodeEnv } from '../../env';
import { Config } from '../config';
import { mapAnyError } from '../nestjs/exception';
import { GQLLoggerPlugin } from './logger-plugin';
export type GraphqlContext = {
req: Request;
res: Response;
isAdminQuery: boolean;
};
@Global()
@Module({
imports: [
GraphQLModule.forRootAsync<ApolloDriverConfig>({
driver: ApolloDriver,
useFactory: (config: Config) => {
return {
...config.graphql.apolloDriverConfig,
buildSchemaOptions: {
numberScalarMode: 'integer',
},
useGlobalPrefix: true,
graphiql: env.NODE_ENV === NodeEnv.Development,
sortSchema: true,
autoSchemaFile: join(
env.projectRoot,
env.testing
? './node_modules/.cache/schema.gql'
: './src/schema.gql'
),
path: '/graphql',
csrfPrevention: {
requestHeaders: ['content-type'],
},
context: ({
req,
res,
}: {
req: Request;
res: Response;
}): GraphqlContext => ({
req,
res,
isAdminQuery: false,
}),
plugins: [new GQLLoggerPlugin()],
formatError: (formattedError, error) => {
let ufe = mapAnyError(error);
// @ts-expect-error allow assign
formattedError.extensions = ufe.toJSON();
if (env.namespaces.canary) {
formattedError.extensions.stacktrace = ufe.stacktrace;
}
return formattedError;
},
};
},
inject: [Config],
}),
],
})
export class GqlModule {}
export * from './pagination';
export { registerObjectType } from './register';