Zend 3, graphQL - how to instantiate a model using factory in a type class

I currently have these Types.php:

namespace Application\GraphQL;

use Application\GraphQL\Type\NodeType;

use Application\GraphQL\Type\QueryType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\Type;
use Application\GraphQL\Type\PersonType;

/**
 * Class Types
 *
 * Acts as a registry and factory for your types.
 *
 * As simplistic as possible for the sake of clarity of this example.
 * Your own may be more dynamic (or even code-generated).
 *
 * @package GraphQL\Examples\Blog
 */
class Types
{

    private static $query;
    private static $person;
    private static $node;


    public static function person()
    {
        return self::$person ?: (self::$person = new PersonType());
    }

    /**
     * @return QueryType
     */
    public static function query()
    {
        return self::$query ?: (self::$query = new QueryType());
    }

    /**
     * @return NodeType
     */
    public static function node()
    {
        return self::$node ?: (self::$node = new NodeType());
    }

    /**
     * @return \GraphQL\Type\Definition\IDType
     */
    public static function id()
    {
        return Type::id();
    }

    /**
     * @return \GraphQL\Type\Definition\StringType
     */
    public static function string()
    {
        return Type::string();
    }

    /**
     * @param Type $type
     * @return NonNull
     */
    public static function nonNull($type)
    {
        return new NonNull($type);
    }
}

The query () function creates an instance of QueryType. I added the PersonTable QueryType constructor model class so that it can query people from the database.

QueryType.php

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);
    }

I created factories in the module \ Application \ 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);
                },
            ),
        );
    }
}

I am doing this example which has no framework:

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

So the question is how to create a queryType instance with an injected PersonTable instance? I have to somehow get an instance of PersonTable from the factory instance, but I don't understand how to do this.

Update:

I decided to try injecting QueryType into the controller. Created such a function:

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

\Application\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:\projektai\php projektai\htdocs\graphQL_zend_3\module\Application\src\Controller\IndexController.p

, ?

, :

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

query(), QueryType.

PersonTable QueryType.

Update:

factory, asnswer:

class QueryTypeFactory implements FactoryInterface
{

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

}

IndexController :

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

Module.php factory:

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

            ),
        );
    }

, :

: 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:\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. , , . .

, QueryType:: . , :

use Application\GraphQL\Type\QueryType;

use.

+4
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);
    }
}

PurchaseManagerFactory, Zend\ServiceManager\ Factory\FactoryInterface. factory __invoke(), . $container, -. $container .

+1

Source: https://habr.com/ru/post/1668087/


All Articles