Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscriptions can take both subscribe/resolve in it's definition, the applyMiddlewareToField overlooks that #561

Open
leiyangyou opened this issue Jan 1, 2023 · 1 comment

Comments

@leiyangyou
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch graphql-middleware@6.1.33 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/graphql-middleware/dist/applicator.js b/node_modules/graphql-middleware/dist/applicator.js
index c80326c..ba4c704 100644
--- a/node_modules/graphql-middleware/dist/applicator.js
+++ b/node_modules/graphql-middleware/dist/applicator.js
@@ -21,46 +21,43 @@ function parseField(field) {
 }
 function applyMiddlewareToField(field, options, middleware) {
   const parsedField = parseField(field);
-  if (utils.isMiddlewareWithFragment(middleware) && parsedField.resolve && parsedField.resolve !== graphql.defaultFieldResolver) {
-    return {
-      ...parsedField,
-      fragment: middleware.fragment,
-      fragments: middleware.fragments,
-      resolve: wrapResolverInMiddleware(parsedField.resolve, middleware.resolve)
-    };
-  } else if (utils.isMiddlewareWithFragment(middleware) && parsedField.subscribe) {
-    return {
-      ...parsedField,
-      fragment: middleware.fragment,
-      fragments: middleware.fragments,
-      subscribe: wrapResolverInMiddleware(parsedField.subscribe, middleware.resolve)
-    };
-  } else if (utils.isMiddlewareResolver(middleware) && parsedField.resolve && parsedField.resolve !== graphql.defaultFieldResolver) {
-    return {
-      ...parsedField,
-      resolve: wrapResolverInMiddleware(parsedField.resolve, middleware)
-    };
-  } else if (utils.isMiddlewareResolver(middleware) && parsedField.subscribe) {
-    return {
-      ...parsedField,
-      subscribe: wrapResolverInMiddleware(parsedField.subscribe, middleware)
-    };
-  } else if (utils.isMiddlewareWithFragment(middleware) && !options.onlyDeclaredResolvers) {
-    return {
-      ...parsedField,
-      fragment: middleware.fragment,
-      fragments: middleware.fragments,
-      resolve: wrapResolverInMiddleware(graphql.defaultFieldResolver, middleware.resolve)
-    };
-  } else if (utils.isMiddlewareResolver(middleware) && !options.onlyDeclaredResolvers) {
-    return {
-      ...parsedField,
-      resolve: wrapResolverInMiddleware(graphql.defaultFieldResolver, middleware)
-    };
+
+  const resolve = options.onlyDeclaredResolvers ?
+    (field.resolve !== graphql_1.defaultFieldResolver ? parsedField.resolve : null) :
+    (field.resolve || graphql_1.defaultFieldResolver)
+
+  const subscribe = parsedField.subscribe
+
+  if (resolve || subscribe) {
+    let update = null
+    let middlewareResolve = null
+
+    if (utils.isMiddlewareWithFragment(middleware)) {
+      update = {}
+      update.fragment = middleware.fragment
+      update.fragments = middleware.fragments
+      middlewareResolve = middleware.resolve
+    } else if (utils.isMiddlewareResolver(middleware)) {
+      update = {}
+      middlewareResolve = middleware
+    }
+
+    if (middlewareResolve) {
+      if (resolve) {
+        update.resolve = wrapResolverInMiddleware(resolve, middlewareResolve)
+      }
+      if (subscribe) {
+        update.subscribe = wrapResolverInMiddleware(subscribe, middlewareResolve)
+      }
+      return {...parsedField, ...update}
+    } else {
+      return parsedField
+    }
   } else {
-    return { ...parsedField, resolve: graphql.defaultFieldResolver };
+    return parsedField
   }
 }
+
 function applyMiddlewareToType(type, options, middleware) {
   const fieldMap = type.getFields();
   if (utils.isMiddlewareFunction(middleware)) {

This issue body was partially generated by patch-package.

@cuzzlor
Copy link

cuzzlor commented Aug 11, 2023

Thanks for this, it appears to work for me. Would you consider putting up a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants