Skip to content

Commit 2710da4

Browse files
committed
Implement memoization on get_argument_values.
1 parent e58cce9 commit 2710da4

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

graphql/core/execution/base.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class ExecutionContext(object):
2727
Namely, schema of the type system that is currently executing,
2828
and the fragments defined in the query document"""
2929

30-
__slots__ = 'schema', 'fragments', 'root', 'operation', 'variables', 'errors', 'request_context'
30+
__slots__ = 'schema', 'fragments', 'root', 'operation', 'variables', 'errors', 'request_context', \
31+
'argument_values_cache'
3132

3233
def __init__(self, schema, root, document_ast, operation_name, args, request_context):
3334
"""Constructs a ExecutionContext object from the arguments passed
@@ -70,6 +71,17 @@ def __init__(self, schema, root, document_ast, operation_name, args, request_con
7071
self.variables = variables
7172
self.errors = errors
7273
self.request_context = request_context
74+
self.argument_values_cache = {}
75+
76+
def get_argument_values(self, field_def, field_ast):
77+
k = field_def, field_ast
78+
result = self.argument_values_cache.get(k)
79+
80+
if not result:
81+
result = self.argument_values_cache[k] = get_argument_values(field_def.args, field_ast.arguments,
82+
self.variables)
83+
84+
return result
7385

7486

7587
class ExecutionResult(object):

graphql/core/execution/executor.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,7 @@ def _resolve_field(self, execution_context, parent_type, source, field_asts):
157157

158158
# Build a dict of arguments from the field.arguments AST, using the variables scope to
159159
# fulfill any variable references.
160-
# TODO: find a way to memoize, in case this field is within a list type.
161-
args = get_argument_values(
162-
field_def.args, field_ast.arguments, execution_context.variables
163-
)
160+
args = execution_context.get_argument_values(field_def, field_ast)
164161

165162
# The resolve function's optional third argument is a collection of
166163
# information about the current execution state.

0 commit comments

Comments
 (0)