-
-
Notifications
You must be signed in to change notification settings - Fork 674
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
Why are globalMiddlewares getting called so many times in one request? #198
Comments
Just create a guard and place it in context object: if (context.dataLoaderInitialized) {
return next();
}
context.dataLoaderInitialized = true;
// rest of your dataloader initialization code I think I could add a
Yes, global middlewares are run for every single resolver. That's because
For now it's not possible to disable global middlewares for field resolvers. I am planing to revise the middlewares feature and allow for placing |
I have no choice but to disable usage of global middlewares and use this workaround for the error logger since calling it hundreds of times per request is just too much:
I use the
|
const server = new ApolloServer({
schema,
formatError: err => {
ServerLogger.error('GqlErrorLogger', err);
return formatArgumentValidationError(err);
},
}); Unfortunately, schema is not aware of the execution, so I'm not able in TypeGraphQL to make So closing this issue as scoping middlewares is now tracked by #200 🔒 |
@abdiu34567 If you need to put a logic one time per whole client request, do it in context initialization phase of your graphql server library. |
I am using apollo server 4. Can you provide an example pls? app.use( await new Promise((resolve) => |
Please refer to Apollo Server docs then to find out how to initialize the GraphQL context. |
I've created two global middlewares,
ErrorLoggerMiddleware
andDataLoaderMiddleware
. Both are getting called about hundreds of times in just one request.I don't think it is okay. It delays the response for 2- 3 seconds since the dataloader middleware will create several dataloader instances every time it gets called. Is that how global middlewares supposed to work?
Update
I just checked
createSimpleFieldResolver
in resolvers/create.js. I found that theErrorLoggerMiddleware
got called on every single object field in the response data.That's not ideal in my situation. I use
@FieldResolver()
to return product's specifications. Most products have around 40-50 spec attributes and every attribute is an objects of its own with around 5 fields. I fetch 10 products' specifications per request. That's why the global middlewares get called hundreds of times. I also use@UseMiddleware()
in the resolver. It only gets called once per request. I thought it would be the same for global middlewares.Is there any workaround for this? I think there should be an option to disable running global middlewares in
@FieldResolver()
.Example Resolver
Associated package:
The text was updated successfully, but these errors were encountered: