mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-11-04 01:47:45 +03:00 
			
		
		
		
	Merge pull request #622 from jkimbo/docs-middleware-update
[Docs] Functional middleware example
This commit is contained in:
		
						commit
						92f6a496c5
					
				| 
						 | 
				
			
			@ -3,7 +3,7 @@ Middleware
 | 
			
		|||
 | 
			
		||||
You can use ``middleware`` to affect the evaluation of fields in your schema.
 | 
			
		||||
 | 
			
		||||
A middleware is any object that responds to ``resolve(*args, next_middleware)``.
 | 
			
		||||
A middleware is any object or function that responds to ``resolve(next_middleware, *args)``.
 | 
			
		||||
 | 
			
		||||
Inside that method, it should either:
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -40,3 +40,32 @@ And then execute it with:
 | 
			
		|||
.. code:: python
 | 
			
		||||
 | 
			
		||||
    result = schema.execute('THE QUERY', middleware=[AuthorizationMiddleware()])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Functional example
 | 
			
		||||
------------------
 | 
			
		||||
 | 
			
		||||
Middleware can also be defined as a function. Here we define a middleware that
 | 
			
		||||
logs the time it takes to resolve each field
 | 
			
		||||
 | 
			
		||||
.. code:: python
 | 
			
		||||
 | 
			
		||||
    from time import time as timer
 | 
			
		||||
 | 
			
		||||
    def timing_middleware(next, root, info, **args):
 | 
			
		||||
        start = timer()
 | 
			
		||||
        return_value = next(root, info, **args)
 | 
			
		||||
        duration = timer() - start
 | 
			
		||||
        logger.debug("{parent_type}.{field_name}: {duration} ms".format(
 | 
			
		||||
            parent_type=root._meta.name if root else '',
 | 
			
		||||
            field_name=info.field_name,
 | 
			
		||||
            duration=round(duration * 1000, 2)
 | 
			
		||||
        ))
 | 
			
		||||
        return return_value
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
And then execute it with:
 | 
			
		||||
 | 
			
		||||
.. code:: python
 | 
			
		||||
 | 
			
		||||
    result = schema.execute('THE QUERY', middleware=[timing_middleware])
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user