We call convertExceptionToGraphQLError in the exception handler for http exceptions but we don't take into account those that already are graphqlErrors and because of that the logic of convertExceptionToGraphql is to fallback to a 500. Now if the exception is a BaseGraphqlError (custom graphql error we throw in the code), we throw them directly. BEFORE <img width="957" alt="Screenshot 2024-07-12 at 15 33 03" src="https://github.com/user-attachments/assets/22ddae13-4996-4ad3-8f86-dd17c2922ca8"> AFTER <img width="923" alt="Screenshot 2024-07-12 at 15 32 01" src="https://github.com/user-attachments/assets/d3d6db93-6d28-495c-a4b4-ba4e47d45abd"> --------- Co-authored-by: Charles Bochet <charles@twenty.com>
62 lines
2.4 KiB
TypeScript
62 lines
2.4 KiB
TypeScript
import { YogaDriverConfig } from '@graphql-yoga/nestjs';
|
|
import GraphQLJSON from 'graphql-type-json';
|
|
|
|
import { useCachedMetadata } from 'src/engine/api/graphql/graphql-config/hooks/use-cached-metadata';
|
|
import { useThrottler } from 'src/engine/api/graphql/graphql-config/hooks/use-throttler';
|
|
import { MetadataGraphQLApiModule } from 'src/engine/api/graphql/metadata-graphql-api.module';
|
|
import { useGraphQLErrorHandlerHook } from 'src/engine/core-modules/graphql/hooks/use-graphql-error-handler.hook';
|
|
import { DataloaderService } from 'src/engine/dataloaders/dataloader.service';
|
|
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service';
|
|
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
|
import { ExceptionHandlerService } from 'src/engine/integrations/exception-handler/exception-handler.service';
|
|
import { renderApolloPlayground } from 'src/engine/utils/render-apollo-playground.util';
|
|
|
|
export const metadataModuleFactory = async (
|
|
environmentService: EnvironmentService,
|
|
exceptionHandlerService: ExceptionHandlerService,
|
|
dataloaderService: DataloaderService,
|
|
workspaceSchemaCacheStorage: CacheStorageService,
|
|
): Promise<YogaDriverConfig> => {
|
|
const config: YogaDriverConfig = {
|
|
autoSchemaFile: true,
|
|
include: [MetadataGraphQLApiModule],
|
|
renderGraphiQL() {
|
|
return renderApolloPlayground({ path: 'metadata' });
|
|
},
|
|
resolvers: { JSON: GraphQLJSON },
|
|
plugins: [
|
|
useThrottler({
|
|
ttl: environmentService.get('API_RATE_LIMITING_TTL'),
|
|
limit: environmentService.get('API_RATE_LIMITING_LIMIT'),
|
|
identifyFn: (context) => {
|
|
return context.req.user?.id ?? context.req.ip ?? 'anonymous';
|
|
},
|
|
}),
|
|
useGraphQLErrorHandlerHook({
|
|
exceptionHandlerService,
|
|
}),
|
|
useCachedMetadata({
|
|
cacheGetter: workspaceSchemaCacheStorage.get.bind(
|
|
workspaceSchemaCacheStorage,
|
|
),
|
|
cacheSetter: workspaceSchemaCacheStorage.set.bind(
|
|
workspaceSchemaCacheStorage,
|
|
),
|
|
operationsToCache: ['ObjectMetadataItems'],
|
|
}),
|
|
],
|
|
path: '/metadata',
|
|
context: () => ({
|
|
loaders: dataloaderService.createLoaders(),
|
|
}),
|
|
};
|
|
|
|
if (environmentService.get('DEBUG_MODE')) {
|
|
config.renderGraphiQL = () => {
|
|
return renderApolloPlayground({ path: 'metadata' });
|
|
};
|
|
}
|
|
|
|
return config;
|
|
};
|