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
{
private static $query;
private static $person;
private static $node;
public static function person()
{
return self::$person ?: (self::$person = new PersonType());
}
public static function query()
{
return self::$query ?: (self::$query = new QueryType());
}
public static function node()
{
return self::$node ?: (self::$node = new NodeType());
}
public static function id()
{
return Type::id();
}
public static function string()
{
return Type::string();
}
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:
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';
}
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));
}
),
);
}
:
: 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' => $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 => 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();
$schema = new Schema([
'query' => $queryTypeFactory()
]);
$queryTypeFactory() $container. , , . .
, QueryType:: . , :
use Application\GraphQL\Type\QueryType;
use.