| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import { ApolloServerPlugin, GraphQLRequestListener, GraphQLServerContext } from '@apollo/server';
- import { DocumentNode, GraphQLNamedType, isUnionType } from 'graphql';
- import { Instrument } from '../../common/instrument-decorator';
- import { AssetStorageStrategy } from '../../config/asset-storage-strategy/asset-storage-strategy';
- import { ConfigService } from '../../config/config.service';
- import { GraphqlValueTransformer } from '../common/graphql-value-transformer';
- /**
- * Transforms outputs so that any Asset instances are run through the {@link AssetStorageStrategy.toAbsoluteUrl}
- * method before being returned in the response.
- */
- @Instrument()
- export class AssetInterceptorPlugin implements ApolloServerPlugin {
- private graphqlValueTransformer: GraphqlValueTransformer;
- private readonly toAbsoluteUrl: AssetStorageStrategy['toAbsoluteUrl'] | undefined;
- constructor(private configService: ConfigService) {
- const { assetOptions } = this.configService;
- if (assetOptions.assetStorageStrategy.toAbsoluteUrl) {
- this.toAbsoluteUrl = assetOptions.assetStorageStrategy.toAbsoluteUrl.bind(
- assetOptions.assetStorageStrategy,
- );
- }
- }
- async serverWillStart(service: GraphQLServerContext): Promise<void> {
- this.graphqlValueTransformer = new GraphqlValueTransformer(service.schema);
- }
- async requestDidStart(): Promise<GraphQLRequestListener<any>> {
- return {
- willSendResponse: async requestContext => {
- const { document } = requestContext;
- if (document) {
- const { body } = requestContext.response;
- const req = requestContext.contextValue.req;
- if (body.kind === 'single') {
- this.prefixAssetUrls(req, document, body.singleResult.data);
- }
- }
- },
- };
- }
- private prefixAssetUrls(request: any, document: DocumentNode, data?: Record<string, unknown> | null) {
- const typeTree = this.graphqlValueTransformer.getOutputTypeTree(document);
- const toAbsoluteUrl = this.toAbsoluteUrl;
- if (!toAbsoluteUrl || !data) {
- return;
- }
- this.graphqlValueTransformer.transformValues(typeTree, data, (value, type) => {
- if (!type) {
- return value;
- }
- const isAssetType = this.isAssetType(type);
- const isUnionWithAssetType = isUnionType(type) && type.getTypes().find(t => this.isAssetType(t));
- if (isAssetType || isUnionWithAssetType) {
- if (value && !Array.isArray(value)) {
- if (value.preview) {
- value.preview = toAbsoluteUrl(request, value.preview);
- }
- if (value.source) {
- value.source = toAbsoluteUrl(request, value.source);
- }
- }
- }
- return value;
- });
- }
- private isAssetType(type: GraphQLNamedType): boolean {
- const assetTypeNames = ['Asset', 'SearchResultAsset'];
- return assetTypeNames.includes(type.name);
- }
- }
|