1. Go to this page and download the library: Download maiscraft/graphql 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/ */
maiscraft / graphql example snippets
namespace App\Domain\Entity;
use Maiscraft\GraphQL\Entity\EntityObject;
class Article extends EntityObject
{
public function __construct(array $data = [])
{
parent::__construct($data);
}
// id: int → 自动推断为 ID 类型,无需 #[Field(type: 'ID')]
public function getId(): ?int
{
return $this->getData('id') ?: null;
}
public function getTitle(): string
{
return $this->getData('title') ?? '';
}
// is* 方法 → is_published 布尔字段
public function isPublished(): bool
{
return $this->getData('status') === 'published';
}
}
namespace App\Domain\Entity;
use Maiscraft\GraphQL\Annotation\Type;
#[Type(description: '文章')]
class Article
{
public function __construct(
private readonly ?int $id = null,
private readonly string $title = '',
) {}
public function getId(): ?int { return $this->id; }
public function getTitle(): string { return $this->title; }
}
namespace App\Domain\ValueObject;
use Maiscraft\GraphQL\Annotation\Type;
#[Type(description: '发布状态')]
enum PublishStatus
{
case DRAFT;
case PUBLISHED;
case ARCHIVED;
}
namespace App\DTO;
class CreateArticleDTO
{
public function __construct(
public readonly string $title,
public readonly string $slug,
public readonly ?string $description = null,
) {}
}
namespace App\GraphQL;
use App\Domain\Entity\Article;
use App\DTO\CreateArticleDTO;
use Maiscraft\GraphQL\Annotation\Query;
use Maiscraft\GraphQL\Annotation\Mutation;
use Maiscraft\GraphQL\Annotation\Arg;
use Maiscraft\GraphQL\Contract\ResolverInterface;
class ArticleResolver implements ResolverInterface
{
public function __construct(
private ArticleRepositoryInterface $repository
) {}
// id: int → 自动推断为 ID! 类型,无需 #[Arg(type: 'ID!')]
#[Query(description: 'Get an article by ID')]
public function article(int $id): ?Article
{
return $this->repository->findById($id);
}
// 返回类型以 Pagination 结尾 → 自动追加分页参数 page/perPage
#[Query(description: 'Get paginated articles')]
public function articles(): ArticlePagination
{
return $this->repository->paginate();
}
#[Mutation(description: 'Create a new article')]
public function createArticle(CreateArticleDTO $input): Article
{
return $this->repository->save(new Article(
title: $input->title,
slug: $input->slug,
description: $input->description,
));
}
// id: int → 自动推断为 ID! 类型
#[Mutation(description: 'Delete an article')]
public function deleteArticle(int $id): bool
{
return $this->repository->delete($id);
}
}
// productId 不是 "id",不会自动推断,需显式声明
#[Query(description: 'Get product variants')]
public function productVariants(#[Arg(type: 'ID!')] int $productId): array
{
return $this->repository->getVariants($productId);
}
// 自动推断(无需注解)
#[Query]
public function article(int $id): ?Article { ... } // → ID!
public function getId(): ?int { ... } // → ID
// 需显式注解(非 id 名称)
#[Query]
public function productVariants(#[Arg(type: 'ID!')] int $productId): array { ... }
#[Field(type: 'ID')]
public function getCategoryId(): ?int { ... }