1. Go to this page and download the library: Download rozbehsharahi/graphql3 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rozbehsharahi / graphql3 example snippets
use GraphQL\Type\Schema;
use RozbehSharahi\Graphql3\Registry\SchemaRegistry;
/** @var SchemaRegistry $schemaRegistry */
$schemaRegistry->registerCreator(fn() => new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'noop' => [
'type' => Type::string(),
'resolve' => fn () => 'noop',
],
],
]),
]))
use RozbehSharahi\Graphql3\Registry\SchemaRegistry;
use RozbehSharahi\Graphql3\Type\NoopQueryType;
/** @var SchemaRegistry $schemaRegistry */
$schemaRegistry->registerCreator(fn () => (new NoopQueryType());
namespace Your\Extension;
use GraphQL\Type\Schema;
use RozbehSharahi\Graphql3\Registry\SchemaRegistry;
use RozbehSharahi\Graphql3\Setup\SetupInterface;
use RozbehSharahi\Graphql3\Type\QueryType;
class GraphqlSetup implements SetupInterface
{
public function __construct(
protected SchemaRegistry $schemaRegistry,
protected QueryType $queryType
) {
}
public function setup(): void
{
$this->schemaRegistry->registerCreator(fn() => new Schema([
'query' => $this->queryType,
]));
}
}
declare(strict_types=1);
namespace Your\Extension;
use GraphQL\Type\Definition\Type;
use RozbehSharahi\Graphql3\Domain\Model\GraphqlNode;
use RozbehSharahi\Graphql3\Domain\Model\GraphqlNodeCollection;
use RozbehSharahi\Graphql3\Type\QueryTypeExtenderInterface;
class ExampleQueryTypeExtender implements QueryTypeExtenderInterface
{
public function extend(GraphqlNodeCollection $nodes): GraphqlNodeCollection
{
return $nodes->add(
GraphqlNode::create('someNode')
->withType(Type::string())
->withResolver(fn () => 'Hello World')
);
}
}
declare(strict_types=1);
namespace Your\Extension;
use RozbehSharahi\Graphql3\Builder\RecordListNodeBuilder;
use RozbehSharahi\Graphql3\Builder\RecordNodeBuilder;
use RozbehSharahi\Graphql3\Domain\Model\GraphqlNodeCollection;
use RozbehSharahi\Graphql3\Type\QueryTypeExtenderInterface;
class ExampleQueryTypeExtender implements QueryTypeExtenderInterface
{
public function __construct(
protected RecordNodeBuilder $recordNodeBuilder,
protected RecordListNodeBuilder $recordListNodeBuilder
) {
}
public function extend(GraphqlNodeCollection $nodes): GraphqlNodeCollection
{
return $nodes
->add($this->recordListNodeBuilder->for('sys_log')->build())
->add($this->recordNodeBuilder->for('sys_log')->build())
;
}
}
namespace Your\Extension;
use GraphQL\Type\Schema;
use RozbehSharahi\Graphql3\Registry\SchemaRegistry;
use RozbehSharahi\Graphql3\Setup\SetupInterface;
use RozbehSharahi\Graphql3\Type\QueryType;
class GraphqlSetup implements SetupInterface
{
public function __construct(
protected SchemaRegistry $schemaRegistry,
protected QueryType $queryType
) {
}
public function setup(): void
{
$this->schemaRegistry->registerCreator(fn() => new Schema([
'query' => $this->queryType,
]));
}
}
namespace Your\Extension;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Schema;
use RozbehSharahi\Graphql3\Builder\RecordTypeBuilder;
use RozbehSharahi\Graphql3\Domain\Model\Record;
use RozbehSharahi\Graphql3\Registry\SchemaRegistry;
use RozbehSharahi\Graphql3\Setup\SetupInterface;
class GraphqlSetup implements SetupInterface
{
public function __construct(
protected SchemaRegistry $schemaRegistry,
protected RecordTypeBuilder $recordTypeBuilder
) {
}
public function setup(): void
{
$this->schemaRegistry->registerCreator(fn() => new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'page' => [
'type' => $this->recordTypeBuilder->for('pages')->build(),
'resolve' => fn () => Record::create('pages', [
'uid' => 1,
'title' => 'A hard coded page, which should be loaded by a resolver'
]),
],
],
]),
]));
}
}
declare(strict_types=1);
namespace Your\Extension;
use RozbehSharahi\Graphql3\Builder\RecordTypeBuilderExtenderInterface;
use RozbehSharahi\Graphql3\Domain\Model\GraphqlNode;
use RozbehSharahi\Graphql3\Domain\Model\GraphqlNodeCollection;
use RozbehSharahi\Graphql3\Domain\Model\Tca\TableConfiguration;
class Md5PageTypeExtender implements RecordTypeBuilderExtenderInterface
{
public function supportsTable(TableConfiguration $tableConfiguration): bool
{
return 'pages' === $tableConfiguration->getName();
}
public function extendNodes(
TableConfiguration $tableConfiguration,
GraphqlNodeCollection $nodes
): GraphqlNodeCollection {
return $nodes->add(
GraphqlNode::create('md5')->withResolver(fn ($page) => md5(json_encode($page, JSON_THROW_ON_ERROR)))
);
}
}
declare(strict_types=1);
namespace Your\Extension;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Schema;
use RozbehSharahi\Graphql3\Builder\RecordNodeBuilder;
use RozbehSharahi\Graphql3\Registry\SchemaRegistry;
use RozbehSharahi\Graphql3\Setup\SetupInterface;
class GraphqlSetup implements SetupInterface
{
public function __construct(
protected SchemaRegistry $schemaRegistry,
protected RecordNodeBuilder $recordNodeBuilder
) {
}
public function setup(): void
{
$this->schemaRegistry->registerCreator(fn() => new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'page' => $this->recordNodeBuilder->for('pages')->build()->toArray()
],
]),
]));
}
}
declare(strict_types=1);
namespace Your\Extension;
use RozbehSharahi\Graphql3\Builder\RecordListNodeBuilder;
use RozbehSharahi\Graphql3\Domain\Model\GraphqlNodeCollection;
use RozbehSharahi\Graphql3\Type\QueryTypeExtenderInterface;
class ExampleQueryTypeExtender implements QueryTypeExtenderInterface
{
public function __construct(
protected RecordListNodeBuilder $recordListNodeBuilder
) {
}
public function extend(GraphqlNodeCollection $nodes): GraphqlNodeCollection
{
return $nodes
->add($this->recordListNodeBuilder->for('sys_log')->build())
;
}
}
namespace Your\Extension;
use RozbehSharahi\Graphql3\Domain\Model\JwtUser;
use RozbehSharahi\Graphql3\Domain\Model\Record;
use RozbehSharahi\Graphql3\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class PageVoter implements VoterInterface
{
public function vote(TokenInterface $token, mixed $subject, array $attributes): int
{
if (!$subject instanceof Record || $subject->getTable()->getName() !== 'pages') {
return self::ACCESS_ABSTAIN;
}
return $token->getUser() instanceof JwtUser ? self::ACCESS_GRANTED : self::ACCESS_DENIED;
}
}