How to find out which mutation is being requested when configuring "Context"

I would like to add conditional function on context depending on which mutation is being called.
How would I do that efficiently?
When configuring context, I have access to the request body and all the graphql request information is stored at “req.body.query”, which also contains what I need.
To make use of this, I would have to parse with the “parse” function from “graphql” module. But I dont think this is efficient, cuz now the same request is basically getting parsed twice(once by me and once by Apollo Server). Also the parsed result is not very user friendly with all the nested values.

Is there a clean way to know what mutation is being requested?

Hi @Sihoon_Kim, good question!

Can you be a bit more specific about the problem you’re trying to solve? I have one idea to share, but it might not be helpful depending on what you’re trying to accomplish.

Since we can modify context after it’s been created, we can use a plugin’s requestDidStart hook, where we have access to the parsed document, operationName, etc.

const server = new ApolloServer({
  // ...other AS constructor options
  plugins: [{
    async requestDidStart({ request, context }) {
      const { operationName } = request;
      // update `context` however you see fit
    }
  }]
});

Hope that’s helpful!

1 Like

Hey @trevor.scheer, thanks for fast reply!

It is a good start but unfortunately it does not fully solve my problem. I would like to know exactly which mutations have been requested with which input(I could get the input data when configuring Context from request).
The problem with operationName is that it can always change according to the client, so it is not ideal for my use case.

I am working on decoupling my backend into event driven monolith using a message broker, NATS Streaming. So the business logic in each mutations will be split into small business logics, events. Now, I need these mutation information because I would like to log all mutations(commands) as events. These mutation events are basically a special type of event that initiates “event chain”. So yea, I would love to record what caused the event chain.

I still think plugins are the solution you’re looking for. In a later hook, we have access to the parsed operation document itself.

import { ApolloServer } from "apollo-server-express";

const server = new ApolloServer({
  plugins: [
    {
      async requestDidStart() {
        return {
          async didResolveOperation({ operation, request }) {
            operation.name;
            request.variables;
          },
        };
      },
    },
  ],
});

I recommend you read through the plugin docs if you have any more questions, I think you’ll find everything you need.

1 Like

Thanks! finally got what I wanted. I just went a little deeper with operation

      {
        requestDidStart() {
          return {
            didResolveOperation({ operation, request, context }) {
              console.log({
                //@ts-ignore
                name: operation.selectionSet.selections[0].name.value,
                varialbles: request.variables,
              })
            },
          }
        },
      },
1 Like

Ah, of course - I misunderstood what you meant by mutation name but now I see what you were after. Thanks for sharing your solution and glad I could help!

1 Like