question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: repeatedly calling middleware for each key of returning value

See original GitHub issue

Hi everyone, first I want to thank you for this great library, I have used it with Apollo server, all worked great intend of repeatedly calling, it’s calling middleware for each field returned from a handler, and args argument is empty

export const authMiddleware = async (resolve, root, args, ctx: Context, info) => {
    const mutationField = info.schema.getMutationType();
    const mutationDefinition = mutationField.getFields()[info.fieldName];

    if (mutationDefinition && mutationDefinition.auth) {
        try {
            const token = ctx.headers['authorization'].replace('Bearer ', '');
            const data = await Crypto.decodeJwt<{ id: string }>(token);
            const user = await User.findOne(data.id);

            ctx.state.auth = {
                user,
                data,
                token
            };
        } catch (e) {
            throw new AuthenticationError('unauthorized');
        }
    }

    const data = await resolve(root, args, ctx, info);

    console.log(data);
    return data;
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
henry74commented, Dec 26, 2018

You need to define your middleware to only apply to queries/mutations so it doesn’t apply to every field in a result:

const myMiddleware = {
  Query: async (resolve, root, args, context, info) => {
    console.log(`THIS WILL ONLY APPLY TO QUERIES`);
    const result = await resolve(root, args, context, info);
   return result;
  },
  Mutation: async (resolve, root, args, context, info) => {
    console.log(`THIS WILL ONLY APPLY TO MUTATIONS`);
    const result = await resolve(root, args, context, info);
    return result;
  }
};
0reactions
stale[bot]commented, Feb 9, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - .net-core middleware return blank result - Stack Overflow
What I want is that a blank page or an error message like "Invalid User Key" is shown if the user has no...
Read more >
Use Custom Middleware to Detect and Fix 404s in ASP.NET ...
This middleware will check to see if the current request matches a particular path, and will ignore (and pass through) any requests that...
Read more >
Using Middleware in .NET 5.0 to Log Requests and Responses
In any case, I needed a way to record requests and responses, and in .NET 5.0 the simplest way to do this was...
Read more >
Rate Limiting in ASP.NET Core Web API - Code Maze
Rate Limiting in ASP.NET Core Web API is the process of restricting the number of requests for a resource within a specific time...
Read more >
Top 10 Most Common Node.js Developer Mistakes - Toptal
In many asynchronous functions, the return value has almost no significance, so this approach often makes it easy to avoid such a problem....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found