-
Notifications
You must be signed in to change notification settings - Fork 820
Closed
Description
Empty middleware adds 5 times overhead.
import timeit
import graphene
class Track(graphene.ObjectType):
track_id = graphene.ID()
title = graphene.String()
class Album(graphene.ObjectType):
album_id = graphene.ID()
title = graphene.String()
tracks = graphene.List(Track)
@staticmethod
def resolve_tracks(self, args, context, info):
return [
Track(track_id=i, title=i) for i in range(10)
]
class Query(graphene.ObjectType):
albums = graphene.List(Album)
@staticmethod
def resolve_albums(self, args, context, info):
return [
Album(album_id=i, title=i) for i in range(300)
]
schema = graphene.Schema(query=Query)
class TestMiddleware(object):
def resolve(self, next, root, args, context, info):
return next(root, args, context, info)
def execute1():
return schema.execute('''
query {
albums {
tracks {
title
}
}
}
''',
middleware=[TestMiddleware()]
)
def execute2():
return schema.execute('''
query {
albums {
tracks {
title
}
}
}
''',
)
if __name__ == '__main__':
print('with empty middleware: ' + str(timeit.timeit(execute1, number=1)))
print('without middleware: ' + str(timeit.timeit(execute2, number=1)))
Outputs:
with empty middleware: 0.8273355230048764
without middleware: 0.17398804999538697
I think this is not acceptable. I found that problem at wrapping every resolve function in Promise on creating middlewares chain.
https://github.com/graphql-python/graphql-core/blob/master/graphql/execution/middleware.py#L49
At my use cases removing promise wrap from chain not breaks functionality. Even resolvers retuns Promise. Do we realy need this code?
I can make pull request If my conclusions is correct.
Fercho191 and larsblumberg
Metadata
Metadata
Assignees
Labels
No labels