@@ -371,4 +371,76 @@ def execute_sdl(schema)
371371 GRAPHQL
372372 )
373373 end
374+
375+ context 'with a filter' do
376+ let ( :schema ) do
377+ product = Class . new ( base_object ) do
378+ graphql_name 'Product'
379+
380+ field :upc , String , null : false
381+ end
382+
383+ query_obj = Class . new ( base_object ) do
384+ graphql_name 'Query'
385+
386+ field :product , product , null : true
387+ end
388+
389+ Class . new ( base_schema ) do
390+ query query_obj
391+ end
392+ end
393+ let ( :filter ) do
394+ class PermissionWhitelist
395+ def call ( _schema_member , context )
396+ context [ :user_role ] == :admin
397+ end
398+ end
399+
400+ PermissionWhitelist . new
401+ end
402+ let ( :context ) { { user_role : :admin } }
403+ let ( :executed_with_context ) do
404+ schema . execute ( '{ _service { sdl } }' , only : filter , context : context )
405+ end
406+ let ( :executed_without_context ) { schema . execute ( '{ _service { sdl } }' , only : filter ) }
407+
408+ it 'passes context to filters' do
409+ expect ( executed_with_context [ 'data' ] [ '_service' ] [ 'sdl' ] ) . to match_sdl (
410+ <<~GRAPHQL ,
411+ type Product {
412+ upc: String!
413+ }
414+
415+ type Query {
416+ product: Product
417+ }
418+ GRAPHQL
419+ )
420+ end
421+
422+ it 'works without context' do
423+ expect ( executed_without_context [ 'errors' ] ) . to (
424+ match_array (
425+ [
426+ include ( 'message' => "Field '_service' doesn't exist on type 'Query'" ) ,
427+ ] ,
428+ ) ,
429+ )
430+ end
431+
432+ context 'when not authorized' do
433+ let ( :context ) { { user_role : :foo } }
434+
435+ it 'returns an error message' do
436+ expect ( executed_with_context [ 'errors' ] ) . to (
437+ match_array (
438+ [
439+ include ( 'message' => "Field '_service' doesn't exist on type 'Query'" ) ,
440+ ] ,
441+ ) ,
442+ )
443+ end
444+ end
445+ end
374446end
0 commit comments