2020use Overblog \GraphQLBundle \Event \Events ;
2121use Overblog \GraphQLBundle \Event \ExecutorContextEvent ;
2222use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
23+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
2324
2425class Executor
2526{
26- private $ schema ;
27+ /**
28+ * @var Schema[]
29+ */
30+ private $ schemas ;
2731
2832 /**
2933 * @var EventDispatcherInterface|null
@@ -36,14 +40,20 @@ class Executor
3640 /** @var ErrorHandler|null */
3741 private $ errorHandler ;
3842
39- public function __construct (Schema $ schema , EventDispatcherInterface $ dispatcher = null , $ throwException = false , ErrorHandler $ errorHandler = null )
43+ public function __construct (EventDispatcherInterface $ dispatcher = null , $ throwException = false , ErrorHandler $ errorHandler = null )
4044 {
41- $ this ->schema = $ schema ;
4245 $ this ->dispatcher = $ dispatcher ;
4346 $ this ->throwException = (bool ) $ throwException ;
4447 $ this ->errorHandler = $ errorHandler ;
4548 }
4649
50+ public function addSchema ($ name , Schema $ schema )
51+ {
52+ $ this ->schemas [$ name ] = $ schema ;
53+
54+ return $ this ;
55+ }
56+
4757 public function setMaxQueryDepth ($ maxQueryDepth )
4858 {
4959 /** @var QueryDepth $queryDepth */
@@ -70,16 +80,18 @@ public function setThrowException($throwException)
7080 return $ this ;
7181 }
7282
73- public function execute (array $ data , array $ context = [])
83+ public function execute (array $ data , array $ context = [], $ schemaName = null )
7484 {
7585 if (null !== $ this ->dispatcher ) {
7686 $ event = new ExecutorContextEvent ($ context );
7787 $ this ->dispatcher ->dispatch (Events::EXECUTOR_CONTEXT , $ event );
7888 $ context = $ event ->getExecutorContext ();
7989 }
8090
91+ $ schema = $ this ->getSchema ($ schemaName );
92+
8193 $ executionResult = GraphQL::executeAndReturnResult (
82- $ this -> schema ,
94+ $ schema ,
8395 isset ($ data [ParserInterface::PARAM_QUERY ]) ? $ data [ParserInterface::PARAM_QUERY ] : null ,
8496 $ context ,
8597 $ context ,
@@ -93,4 +105,27 @@ public function execute(array $data, array $context = [])
93105
94106 return $ executionResult ;
95107 }
108+
109+ /**
110+ * @param string|null $name
111+ *
112+ * @return Schema
113+ */
114+ public function getSchema ($ name = null )
115+ {
116+ if (empty ($ this ->schemas )) {
117+ throw new \RuntimeException ('At least one schema should be declare. ' );
118+ }
119+
120+ if (null === $ name ) {
121+ $ schema = array_values ($ this ->schemas )[0 ];
122+ } else {
123+ if (!isset ($ this ->schemas [$ name ])) {
124+ throw new NotFoundHttpException (sprintf ('Could not found "%s" schema. ' , $ name ));
125+ }
126+ $ schema = $ this ->schemas [$ name ];
127+ }
128+
129+ return $ schema ;
130+ }
96131}
0 commit comments