Browse Source

refactor(core): Convert error translation from extension to plugin

The `graphql-extensions` package is not supposed to be a public API, and the new ApolloPlugin system supersedes it.
Michael Bromley 6 years ago
parent
commit
149e75b7ae

+ 2 - 3
packages/core/src/api/config/configure-graphql-module.ts

@@ -17,7 +17,7 @@ import { getPluginAPIExtensions } from '../../plugin/plugin-metadata';
 import { ApiSharedModule } from '../api-internal-modules';
 import { IdCodecService } from '../common/id-codec.service';
 import { IdCodecPlugin } from '../middleware/id-codec-plugin';
-import { TranslateErrorExtension } from '../middleware/translate-errors-extension';
+import { TranslateErrorsPlugin } from '../middleware/translate-errors-plugin';
 
 import { generateListOptions } from './generate-list-options';
 import {
@@ -139,10 +139,9 @@ async function createGraphQLOptions(
         },
         debug: true,
         context: (req: any) => req,
-        extensions: [() => new TranslateErrorExtension(i18nService)],
         // This is handled by the Express cors plugin
         cors: false,
-        plugins: [new IdCodecPlugin(idCodecService)],
+        plugins: [new IdCodecPlugin(idCodecService), new TranslateErrorsPlugin(i18nService)],
     } as GqlModuleOptions;
 
     /**

+ 0 - 29
packages/core/src/api/middleware/translate-errors-extension.ts

@@ -1,29 +0,0 @@
-import { Response } from 'express-serve-static-core';
-import { GraphQLError } from 'graphql';
-import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
-
-import { I18nRequest, I18nService } from '../../i18n/i18n.service';
-
-/**
- * This extension intercepts outgoing responses and translates any error messages into the
- * current request language.
- */
-export class TranslateErrorExtension implements GraphQLExtension {
-    constructor(private i18nService: I18nService) {}
-
-    willSendResponse(o: {
-        graphqlResponse: GraphQLResponse;
-        context: { req: I18nRequest; res: Response };
-    }): void | {
-        graphqlResponse: GraphQLResponse;
-        context: { req: I18nRequest; res: Response };
-    } {
-        const { graphqlResponse, context } = o;
-        if (graphqlResponse.errors) {
-            graphqlResponse.errors = graphqlResponse.errors.map(err => {
-                return this.i18nService.translateError(context.req, err as GraphQLError) as any;
-            });
-        }
-        return o;
-    }
-}

+ 25 - 0
packages/core/src/api/middleware/translate-errors-plugin.ts

@@ -0,0 +1,25 @@
+import { ApolloServerPlugin, GraphQLRequestListener } from 'apollo-server-plugin-base';
+import { GraphQLError } from 'graphql';
+
+import { I18nService } from '../../i18n/i18n.service';
+
+/**
+ * This plugin intercepts outgoing responses and translates any error messages into the
+ * current request language.
+ */
+export class TranslateErrorsPlugin implements ApolloServerPlugin {
+    constructor(private i18nService: I18nService) {}
+
+    requestDidStart(): GraphQLRequestListener {
+        return {
+            willSendResponse: requestContext => {
+                const { errors, context } = requestContext;
+                if (errors) {
+                    (requestContext.response as any).errors = errors.map(err => {
+                        return this.i18nService.translateError(context.req, err as GraphQLError) as any;
+                    });
+                }
+            },
+        };
+    }
+}