4

は現在、私は、このようなTypes.phpを持っています。 QueryTypeコンストラクタPersonTableモデルクラスを追加して、データベースから人物を照会できるようにしました。Zendの3は、graphQLは、タイプクラスの工場を使用してモデルのインスタンスを作成する方法を

私はモジュール\アプリケーションに工場を設定している

public function __construct(PersonTable $table) 
    { 
     $config = [ 
      'name' => 'Query', 
      'fields' => [ 
       'person' => [ 
        'type' => Types::person(), 
        'description' => 'Returns person by id', 
        'args' => [ 
         'id' => Types::nonNull(Types::id()) 
        ] 
       ], 
       'hello' => Type::string() 
      ], 
      'resolveField' => function($val, $args, $context, ResolveInfo $info) { 
       return $this->{$info->fieldName}($val, $args, $context, $info); 
      } 
     ]; 

     $this->table = $table; 
     parent::__construct($config); 
    } 

QueryType.php \ SRC \ Module.php:

/** 
* @link  http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository 
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) 
* @license http://framework.zend.com/license/new-bsd New BSD License 
*/ 

namespace Application; 

use Application\Model\PersonTable; 

use Application\Model\Person; 

use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

class Module 
{ 
    const VERSION = '3.0.2dev'; 

    public function getConfig() 
    { 
     return include __DIR__ . '/../config/module.config.php'; 
    } 

    // Add this method: 
    public function getServiceConfig() 
    { 
     return array(
      'factories' => array(
       'Application\Model\PersonTable' => function($sm) { 
        $tableGateway = $sm->get('PersonTableGateway'); 
        $table = new PersonTable($tableGateway); 
        return $table; 
       }, 
       'PersonTableGateway' => function ($sm) { 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Person()); 
        return new TableGateway('album', $dbAdapter, null, $resultSetPrototype); 
       }, 
      ), 
     ); 
    } 
} 

私は任意のフレームワークを持っていません。この例でやっています:

https://github.com/webonyx/graphql-php/tree/master/examples/01-blog

そこで質問です - 私のクレアを行う方法PersonTableインスタンスが挿入されたqueryTypeインスタンス私は何とかFactoryからPersonTableインスタンスを取得する必要がありますが、私はその方法を理解していません。

更新:

私はコントローラにのquerytypeを注入しようとすることを決めました。作成された、このような機能:

public function __construct(QueryType $queryType) 
    { 
     $this->queryType = $queryType; 
    } 

今モジュール\アプリケーション\ SRC \ Module.php getServiceConfigは次のようになります。

public function getServiceConfig() 
    { 
     return array(
      'factories' => array(
       'Application\Model\PersonTable' => function($sm) { 
        $tableGateway = $sm->get('PersonTableGateway'); 
        $table = new PersonTable($tableGateway); 
        return $table; 
       }, 
       'PersonTableGateway' => function ($sm) { 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Person()); 
        return new TableGateway('album', $dbAdapter, null, $resultSetPrototype); 
       }, 
       QueryType::class => function ($sm) { 
        return new QueryType($sm->get(PersonTable::class)); 
       } 
       // when putting in namespace does not find?????????? 
       //QueryType::class => Application\GraphQL\Type\Factories\QueryTypeFactory::class 
       //QueryType::class => \QueryTypeFactory::class 

      ), 
     ); 
    } 

しかし、私は取得エラー:

キャッチできる致命的なエラー:に渡される引数1 Application \ Controller \ IndexController :: __ construct()は、Application \ GraphQL \ Type \ QueryTypeのインスタンスでなければなりません(E:\ projektai \ php projektai \ htdocs \ graphQL_zend_3 \ vendor \ zendframework \ zend-servicemanager \ src \ Factory \ InvokableFactory.php 32行目で、E:\ projeで定義されていますktai \ php projektai \ htdocs \ graphQL_zend_3 \ module \ Application \ src \ Controller \ IndexController.p

この機能で設定した場合、どうすればいいですか?

私はコントローラに注入することができれば

は、その後、私はこのように行う予定:

$schema = new Schema([ 
       //'query' => Types::query() 
       'query' => $this->queryType 
      ]); 

は、だから私はとにかくのquerytypeのインスタンスを返すクエリ()関数を呼び出す必要はありません。

PersonTableは、QueryTypeクラスに自動的に挿入されます。

アップデート:私はasnswerと同様の工場、作成した

:モジュールで

public function __construct(QueryType $queryType) 
    { 
     $this->queryType = $queryType; 
    } 

:IndexControllerで

class QueryTypeFactory implements FactoryInterface 
{ 

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 
     return new QueryType($container->get(PersonTable::class)); 
    } 

} 

を私はコンストラクタを持っています。私はこのファクトリを使用してPHP:

public function getServiceConfig() 
    { 
     return array(
      'factories' => array(
       'Application\Model\PersonTable' => function($sm) { 
        $tableGateway = $sm->get('PersonTableGateway'); 
        $table = new PersonTable($tableGateway); 
        return $table; 
       }, 
       'PersonTableGateway' => function ($sm) { 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Person()); 
        return new TableGateway('album', $dbAdapter, null, $resultSetPrototype); 
       }, 
//    QueryType::class => function ($sm) { 
//     //return new QueryType($sm->get(PersonTable::class)); 
// 
//    } 

       //QueryType::class => Application\GraphQL\Type\Factories\QueryTypeFactory::class 
       //QueryType::class => \QueryTypeFactory::class 
       QueryType::class => QueryTypeFactory::class 

      ), 
     ); 
    } 

それは単に動作しませんが、私はエラーを取得する:

キャッチできる致命的なエラー:アプリケーション\コントローラ\ IndexControllerに渡される引数1 :: __構築物()は、のインスタンスでなければなりませんApplication \ GraphQL \ Type \ QueryTypeは指定されていませんが、E:\ projektai \ php projektai \ htdocs \ graphQL_zend_3 \ vendor \ zendframework \ zend-servicemanager \ src \ Factory \ InvokableFactory.phpの32行目で、E:\ projektai \ php projektai \ htdocs \ graphQL_zend_3 \ module \ Application \ src \ Controller \ IndexController.phpオンライン

私もこの方法で試しました:

$queryTypeFactory = new QueryTypeFactory(); 

      // GraphQL schema to be passed to query executor: 
      $schema = new Schema([ 
       //'query' => Types::query() 
       //'query' => $this->queryType 
       // 'query' => $queryType 
       'query' => $queryTypeFactory() 
      ]); 

しかし、$ queryTypeFactory()にはパラメータ$ containerが必要です。それは私が望むものではないと思います。パラメータを渡さずにインスタンスを作成できるはずです。

factories配列のQueryType :: classをキーとして使用しても構いません。設定されている完全な名前空間で作成されます。

use Application\GraphQL\Type\QueryType; 

また、インデックスコントローラでは、このuse文も呼び出します。

答えて

1
<?php 
namespace Application\Service\Factory; 

use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\Factory\FactoryInterface; 
use Application\Service\CurrencyConverter; 
use Application\Service\PurchaseManager; 

/** 
* This is the factory for PurchaseManager service. Its purpose is to instantiate the 
* service and inject its dependencies. 
*/ 
class PurchaseManagerFactory implements FactoryInterface 
{ 
    public function __invoke(ContainerInterface $container, 
       $requestedName, array $options = null) 
    { 
     // Get CurrencyConverter service from the service manager. 
     $currencyConverter = $container->get(CurrencyConverter::class); 

     // Instantiate the service and inject dependencies. 
     return new PurchaseManager($currencyConverter); 
    } 
} 

上記のコードでは、Zend \ ServiceManager \ Factory \ FactoryInterfaceインターフェイスを実装するPurchaseManagerFactoryクラスがあります。ファクトリクラスにはオブジェクトをインスタンス化することを目標とする__invoke()メソッドがあります。このメソッドには、サービスマネージャである$ container引数があります。 $ containerを使用すると、サービスマネージャからサービスを取得し、インスタンス化されるサービスのコンストラクタメソッドに渡すことができます。

+0

私は同様の例を作ったが、うまくいかなかった。私は質問を更新する –

関連する問題