Graphql-yoga: Middleware triggered multiple times

Created on 28 Jan 2019  路  9Comments  路  Source: dotansimha/graphql-yoga

The Middleware functions seems to be being triggered multiple times and hence causing performance issues

to reproduce, create a middle ware and feed it to the GraphQLServer constructor

image

in the middle-ware definition, go ahead and console.log debug messages

and you will see it being triggered multiple times per request

statustale

Most helpful comment

In case someone encounters this problem in the future, the reason for this is described in prisma-labs/graphql-middleware#33
The middleware is called for every single field resolver, so to fire it only once you need to explicitly assign it to Query and Mutation resolvers only:

const server = new GraphQLServer({
  // ...
  middlewares: [{
    Query: authMiddleware,
    Mutation: authMiddleware,
  }]
})

All 9 comments

I'm having exactly the same behavior don't know why...but not in all of them some is called only once... must be some internal bug.

same here

@Mercurial i just noticed that the middleware functions are getting triggered not only for the fields itself but also in the fields it has individually, that's weird. I don't know if that's a bug or a feature (oh that sounded like a joke).

same problem here

Due to inactivity of this issue we have marked it stale. It will be closed if no further activity occurs.

Hey :wave:, It seems like this issue has been inactive for some time. In need for maintaining clear overview of the issues concerning the latest version of graphql-yoga we'll close it.
Feel free to reopen it at any time if you believe we should futher discuss its content. :slightly_smiling_face:

async function demoMiddleware(resolve, root, args, context, info) {
    console.count('abcd')
    let data = resolve(root, args, context, info)
    return data
}

I am also facing the same issue right now
Weird behaviour is when I call my query from graphql-playground it is calling middleware 11 times and when I call my query from React-Apollo client it gets called 5 times.

In case someone encounters this problem in the future, the reason for this is described in prisma-labs/graphql-middleware#33
The middleware is called for every single field resolver, so to fire it only once you need to explicitly assign it to Query and Mutation resolvers only:

const server = new GraphQLServer({
  // ...
  middlewares: [{
    Query: authMiddleware,
    Mutation: authMiddleware,
  }]
})

@ialexi-bl That worked for me, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

frederikhors picture frederikhors  路  4Comments

asci picture asci  路  3Comments

cj picture cj  路  3Comments

bellomusodiq picture bellomusodiq  路  3Comments

AhmadEl-Banna picture AhmadEl-Banna  路  5Comments