Skip to content

Commit d9c34a0

Browse files
committed
Merge branch 'features/middleware' into features/next
2 parents 1007a46 + 2f5f30e commit d9c34a0

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

graphql/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
# Execute GraphQL queries.
104104
from .execution import ( # no import order
105105
execute,
106+
MiddlewareManager,
107+
middlewares
106108
)
107109

108110
# Validate GraphQL queries.
@@ -206,6 +208,8 @@
206208
'print_ast',
207209
'visit',
208210
'execute',
211+
'MiddlewareManager',
212+
'middlewares',
209213
'specified_rules',
210214
'validate',
211215
'GraphQLError',

graphql/execution/tests/test_executor.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,21 +566,62 @@ def resolver(*_):
566566
def test_middleware():
567567
doc = '''{
568568
ok
569+
not_ok
569570
}'''
570571

571572
class Data(object):
572573

573574
def ok(self):
574575
return 'ok'
575576

577+
def not_ok(self):
578+
return 'not_ok'
579+
576580
doc_ast = parse(doc)
577581

578582
Type = GraphQLObjectType('Type', {
579583
'ok': GraphQLField(GraphQLString),
584+
'not_ok': GraphQLField(GraphQLString),
580585
})
581-
middlewares = MiddlewareManager(lambda *_: None)
586+
587+
def reversed_middleware(next, *args, **kwargs):
588+
p = next(*args, **kwargs)
589+
return p.then(lambda x: x[::-1])
590+
591+
middlewares = MiddlewareManager(reversed_middleware)
592+
result = execute(GraphQLSchema(Type), doc_ast, Data(), middlewares=middlewares)
593+
assert result.data == {'ok': 'ko', 'not_ok': 'ko_ton'}
594+
595+
596+
def test_middleware_class():
597+
doc = '''{
598+
ok
599+
not_ok
600+
}'''
601+
602+
class Data(object):
603+
604+
def ok(self):
605+
return 'ok'
606+
607+
def not_ok(self):
608+
return 'not_ok'
609+
610+
doc_ast = parse(doc)
611+
612+
Type = GraphQLObjectType('Type', {
613+
'ok': GraphQLField(GraphQLString),
614+
'not_ok': GraphQLField(GraphQLString),
615+
})
616+
617+
class MyMiddleware(object):
618+
def resolve(self, next, *args, **kwargs):
619+
p = next(*args, **kwargs)
620+
return p.then(lambda x: x[::-1])
621+
622+
middlewares = MiddlewareManager(MyMiddleware())
582623
result = execute(GraphQLSchema(Type), doc_ast, Data(), middlewares=middlewares)
583-
assert result.data == {'ok': None}
624+
assert result.data == {'ok': 'ko', 'not_ok': 'ko_ton'}
584625

585626

586627
def test_middleware_wrong():

graphql/graphql.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# one operation.
2929
def graphql(schema, request_string='', root_value=None, context_value=None,
3030
variable_values=None, operation_name=None, executor=None,
31-
return_promise=False):
31+
return_promise=False, middlewares=None):
3232
try:
3333
source = Source(request_string, 'GraphQL request')
3434
ast = parse(source)
@@ -46,7 +46,8 @@ def graphql(schema, request_string='', root_value=None, context_value=None,
4646
operation_name=operation_name,
4747
variable_values=variable_values or {},
4848
executor=executor,
49-
return_promise=return_promise
49+
return_promise=return_promise,
50+
middlewares=middlewares,
5051
)
5152
except Exception as e:
5253
return ExecutionResult(

0 commit comments

Comments
 (0)