feat: use apollo playground in debug mode (#3295)
This commit is contained in:
@ -21,6 +21,8 @@ import { Workspace } from 'src/core/workspace/workspace.entity';
|
|||||||
import { WorkspaceFactory } from 'src/workspace/workspace.factory';
|
import { WorkspaceFactory } from 'src/workspace/workspace.factory';
|
||||||
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
||||||
import { handleExceptionAndConvertToGraphQLError } from 'src/filters/utils/global-exception-handler.util';
|
import { handleExceptionAndConvertToGraphQLError } from 'src/filters/utils/global-exception-handler.util';
|
||||||
|
import { renderApolloPlayground } from 'src/workspace/utils/render-apollo-playground.util';
|
||||||
|
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GraphQLConfigService
|
export class GraphQLConfigService
|
||||||
@ -29,13 +31,14 @@ export class GraphQLConfigService
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly tokenService: TokenService,
|
private readonly tokenService: TokenService,
|
||||||
private readonly exceptionHandlerService: ExceptionHandlerService,
|
private readonly exceptionHandlerService: ExceptionHandlerService,
|
||||||
|
private readonly environmentService: EnvironmentService,
|
||||||
private readonly moduleRef: ModuleRef,
|
private readonly moduleRef: ModuleRef,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
createGqlOptions(): YogaDriverConfig {
|
createGqlOptions(): YogaDriverConfig {
|
||||||
const exceptionHandlerService = this.exceptionHandlerService;
|
const exceptionHandlerService = this.exceptionHandlerService;
|
||||||
|
const isDebugMode = this.environmentService.isDebugMode();
|
||||||
return {
|
const config: YogaDriverConfig = {
|
||||||
context: ({ req }) => ({ req }),
|
context: ({ req }) => ({ req }),
|
||||||
autoSchemaFile: true,
|
autoSchemaFile: true,
|
||||||
include: [CoreModule],
|
include: [CoreModule],
|
||||||
@ -90,6 +93,14 @@ export class GraphQLConfigService
|
|||||||
resolvers: { JSON: GraphQLJSON },
|
resolvers: { JSON: GraphQLJSON },
|
||||||
plugins: [],
|
plugins: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (isDebugMode) {
|
||||||
|
config.renderGraphiQL = () => {
|
||||||
|
return renderApolloPlayground();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSchema(
|
async createSchema(
|
||||||
|
|||||||
@ -1,32 +1,47 @@
|
|||||||
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs';
|
import { YogaDriverConfig } from '@graphql-yoga/nestjs';
|
||||||
import { GraphQLError } from 'graphql';
|
import { GraphQLError } from 'graphql';
|
||||||
import GraphQLJSON from 'graphql-type-json';
|
import GraphQLJSON from 'graphql-type-json';
|
||||||
import { maskError } from 'graphql-yoga';
|
import { maskError } from 'graphql-yoga';
|
||||||
|
|
||||||
import { handleExceptionAndConvertToGraphQLError } from 'src/filters/utils/global-exception-handler.util';
|
import { handleExceptionAndConvertToGraphQLError } from 'src/filters/utils/global-exception-handler.util';
|
||||||
|
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||||
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
||||||
import { MetadataModule } from 'src/metadata/metadata.module';
|
import { MetadataModule } from 'src/metadata/metadata.module';
|
||||||
|
import { renderApolloPlayground } from 'src/workspace/utils/render-apollo-playground.util';
|
||||||
|
|
||||||
export const metadataModuleFactory = async (
|
export const metadataModuleFactory = async (
|
||||||
|
environmentService: EnvironmentService,
|
||||||
exceptionHandlerService: ExceptionHandlerService,
|
exceptionHandlerService: ExceptionHandlerService,
|
||||||
): Promise<YogaDriverConfig> => ({
|
): Promise<YogaDriverConfig> => {
|
||||||
context: ({ req }) => ({ req }),
|
const config: YogaDriverConfig = {
|
||||||
driver: YogaDriver,
|
context: ({ req }) => ({ req }),
|
||||||
autoSchemaFile: true,
|
autoSchemaFile: true,
|
||||||
include: [MetadataModule],
|
include: [MetadataModule],
|
||||||
resolvers: { JSON: GraphQLJSON },
|
renderGraphiQL() {
|
||||||
plugins: [],
|
return renderApolloPlayground({ path: 'metadata' });
|
||||||
path: '/metadata',
|
|
||||||
maskedErrors: {
|
|
||||||
maskError(error: GraphQLError, message, isDev) {
|
|
||||||
if (error.originalError) {
|
|
||||||
return handleExceptionAndConvertToGraphQLError(
|
|
||||||
error.originalError,
|
|
||||||
exceptionHandlerService,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return maskError(error, message, isDev);
|
|
||||||
},
|
},
|
||||||
},
|
resolvers: { JSON: GraphQLJSON },
|
||||||
});
|
plugins: [],
|
||||||
|
path: '/metadata',
|
||||||
|
maskedErrors: {
|
||||||
|
maskError(error: GraphQLError, message, isDev) {
|
||||||
|
if (error.originalError) {
|
||||||
|
return handleExceptionAndConvertToGraphQLError(
|
||||||
|
error.originalError,
|
||||||
|
exceptionHandlerService,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return maskError(error, message, isDev);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
if (environmentService.isDebugMode()) {
|
||||||
|
config.renderGraphiQL = () => {
|
||||||
|
return renderApolloPlayground({ path: 'metadata' });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
};
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import { WorkspaceMigrationRunnerModule } from 'src/workspace/workspace-migratio
|
|||||||
import { WorkspaceMigrationModule } from 'src/metadata/workspace-migration/workspace-migration.module';
|
import { WorkspaceMigrationModule } from 'src/metadata/workspace-migration/workspace-migration.module';
|
||||||
import { metadataModuleFactory } from 'src/metadata/metadata.module-factory';
|
import { metadataModuleFactory } from 'src/metadata/metadata.module-factory';
|
||||||
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
import { ExceptionHandlerService } from 'src/integrations/exception-handler/exception-handler.service';
|
||||||
|
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||||
|
|
||||||
import { DataSourceModule } from './data-source/data-source.module';
|
import { DataSourceModule } from './data-source/data-source.module';
|
||||||
import { FieldMetadataModule } from './field-metadata/field-metadata.module';
|
import { FieldMetadataModule } from './field-metadata/field-metadata.module';
|
||||||
@ -16,8 +17,8 @@ import { RelationMetadataModule } from './relation-metadata/relation-metadata.mo
|
|||||||
imports: [
|
imports: [
|
||||||
GraphQLModule.forRootAsync<YogaDriverConfig>({
|
GraphQLModule.forRootAsync<YogaDriverConfig>({
|
||||||
driver: YogaDriver,
|
driver: YogaDriver,
|
||||||
inject: [ExceptionHandlerService],
|
|
||||||
useFactory: metadataModuleFactory,
|
useFactory: metadataModuleFactory,
|
||||||
|
inject: [EnvironmentService, ExceptionHandlerService],
|
||||||
}),
|
}),
|
||||||
DataSourceModule,
|
DataSourceModule,
|
||||||
FieldMetadataModule,
|
FieldMetadataModule,
|
||||||
|
|||||||
@ -0,0 +1,25 @@
|
|||||||
|
interface ApolloPlaygroundOptions {
|
||||||
|
path?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const renderApolloPlayground = ({
|
||||||
|
path = 'graphql',
|
||||||
|
}: ApolloPlaygroundOptions = {}) => {
|
||||||
|
return `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<body style="margin: 0; overflow-x: hidden; overflow-y: hidden">
|
||||||
|
<div id="sandbox" style="height:100vh; width:100vw;"></div>
|
||||||
|
<script src="https://embeddable-sandbox.cdn.apollographql.com/_latest/embeddable-sandbox.umd.production.min.js"></script>
|
||||||
|
<script>
|
||||||
|
new window.EmbeddedSandbox({
|
||||||
|
target: "#sandbox",
|
||||||
|
// Pass through your server href if you are embedding on an endpoint.
|
||||||
|
// Otherwise, you can pass whatever endpoint you want Sandbox to start up with here.
|
||||||
|
initialEndpoint: "http://localhost:3000/${path}",
|
||||||
|
});
|
||||||
|
// advanced options: https://www.apollographql.com/docs/studio/explorer/sandbox#embedding-sandbox
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user