GraphQL middleware issue

Hi!

Using v 4.17.0 of Strapi I’m trying to use custom middleware for my GraphQL mutations, I have this:

....
        'Mutation.updateManifestEntry': {
          middlewares: [
            'api::manifest-entry.manifest-entry-log-middleware',
            async (next, parent, args, context, info) => {
              console.log('This is a test!')
              return next(parent, args, context, info);
            },
          ]
        },
....

And this is my file src/api/manifest-entry/middlewares/manifestEntryLogMiddleware.ts:

import { Strapi } from "@strapi/strapi";

export default (options, { strapi: Strapi }) => {
  return async (next, parent, args, context, info) => {
    console.log('manifest-entry-log-middleware.ts')

    return next(parent, args, context, info)
  };
};

It clearly states this in the docs:

The only difference between the GraphQL and REST implementations is that the config key becomes options .

But clearly thats not working as I get this back:

          [
    {
        "message": "Cannot destructure property 'strapi' of 'undefined' as it is undefined.",
        "locations": [
            {
                "line": 3,
                "column": 3
            }
        ],
        "path": [
            "updateManifestEntry"
        ],
        "extensions": {
            "code": "INTERNAL_SERVER_ERROR",
            "exception": {
                "stacktrace": [
                    "TypeError: Cannot destructure property 'strapi' of 'undefined' as it is undefined.",
                    "    at exports.default (/Users/boriskamp/Documents/bk-development/todays-portal-strapi/dist/src/api/manifest-entry/middlewares/manifestEntryLogMiddleware.js:3:39)",
                    "    at /Users/boriskamp/Documents/bk-development/todays-portal-strapi/node_modules/@strapi/plugin-graphql/dist/server/index.js:249:51",
                    "    at fieldDefinition.resolve (/Users/boriskamp/Documents/bk-development/todays-portal-strapi/node_modules/@strapi/plugin-graphql/dist/server/index.js:274:42)",
                    "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
                ]
            }
        }
    }
]

So what am I doing wrong here and how can we make this more clear in the docs?

anybody? This is an issue that is still there.

Hi boriskamp,

i have the exact same problem (strapi v. 4.20.1) and error in my GraphQL playground. Well, your last post was a month ago. Have you found a solution to this problem in the meantime?

If I find a solution, I’ll post it here.

Hi @Manalishi

No I have not found a solution yet…

Hi all, I got around this issue using the same structure when we use it directly in middlware array, example:

/**
 * `add-client-into-barbershop` middleware
 */

import { errors } from "@strapi/utils";
import { Context } from "koa";
import * as _ from "lodash";

export default async (next, parent, args, context: Context, info) => {
  // If you want to do something before reaching DB
  const resp = await next(parent, args, context, info);
 // If you want to do something after reaching DB

  const { hasAlreadyThisClient, barbershop, clientId, barbershopId } =
    parent || {};

  if (resp && barbershop && !hasAlreadyThisClient) {
    await strapi.entityService.update(
      "api::barbershop.barbershop",
      barbershopId,
      {
        data: { clients: [...barbershop.clients, clientId] },
        populate: "clients",
      }
    );
  }

  return resp;
};

OBS: strapi variable is available everywhere!