定制 shish/gqla 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shish/gqla

Composer 安装命令:

composer require shish/gqla

包简介

A set of annotations for generating graphql APIs

关键字:

README 文档

README

A GraphQL schema generator based on PHP Attributes

Because when your webapp already has objects for internal use, you shoudn't need to rewrite or duplicate everything in order to query those objects via graphql :)

The schemas generated here are based on https://github.com/webonyx/graphql-php, so once you've generated a schema, look at the docs over there for how to make use of it

Example:

use GQLA\Type;
use GQLA\Field;
use GQLA\Query;
use GQLA\Mutation;

// To create a new GraphQL Type, expose a PHP class
#[Type]
class Post {
    // To add fields to that type, expose PHP class properties
    #[Field]
    public string $title;
    #[Field]
    public string $body;

    // If you don't want the field to be part of your API,
    // don't expose it
    public int $author_id;

    // If your field is more complicated than a property,
    // expose a PHP function and it will be called as needed
    #[Field(name: "author")]
    public function get_author(): User {
        return User::by_id($this->author_id);
    }

    // To add a new query or mutation, you can extend
    // the base Query or Mutation types
    #[Query(name: "posts", type: "[Post!]!")]
    public static function search_posts(string $text): array {
        // SELECT * FROM posts WHERE text LIKE "%{$text}%";
    }
}

#[Type]
class User {
    #[Field]
    public string $name;
}

#[Type]
class Comment {
    #[Field]
    public string $text;
    public int $post_id;
    public int $author_id;
    #[Field]
    public function author(): User {
        return User::by_id($this->author_id);
    }
    #[Field(deprecationReason: "Use author subfield")]
    public function author_name(): string {
        return User::by_id($this->author_id)->name;
    }

    // Note that even if the Comment class comes from a third-party
    // plugin, it can still add a new "comments" field onto the
    // first-party "Post" object type.
    #[Field(extends: "Post", type: "[Comment!]!")]
    public function comments(Post $post): array {
        // SELECT * FROM comments WHERE post_id = {$post->id}
    }
}

Then new \GQLA\Schema(); will search for all annotated objects and return a graphql schema like:

type Post {
    title: String!
    body: String!
    author: User!
    comments: [Comment!]!
}

type User {
    name: String!
}

type Comment {
    text: String!
    author: User!
    author_name: String @deprecated(reason: "Use author subfield")
}

type Query {
    search_posts(text: String!): [Post!]!
}

So you can send a query like

{
    search_posts(text: "Hello") {
        title
        body
        author {
            name
        }
        comments {
            text
        }
    }
}

And get a response like

{
    "data": {
        "posts": [
            {
                "title": "Hello world!",
                "body": "This is the first post in my blog",
                "author": {
                    "name": "Shish",
                },
                "comments": [
                    { "text": "Nice first post!" },
                    { "text": "It works :D" },
                ],
            }
        ]
    }
}

API

These annotations have several common parameters:

  • name: Give a specific name to this type / field (default: Use the class / property / function name)
  • type: Give a specific type to the field (default: infer this from the PHP-types-to-GraphQL-types map)
    • Note that if your PHP type is array, then you must specify type with something more specific for GraphQL, for example:
      #[Field(type: "[String!]!")]
      function get_tags(): array {
          return ["foo", "bar"];
      }
  • args: Override the inferred types for any function arguments
    • As with type, note that this is required whenever a PHP function accepts an array as input, for example:
      #[Field(args: ["tags" => "[String!]!"])]
      function get_first_post_with_tags(array $tags): Post {
          return ...;
      }
  • extends: By default, an exposed field on an exposed object will be added as a field of that object. You can also extend your other objects (eg, a "likes" plugin could add a number_of_likes field onto your BlogPost object)
  • description: Add a description to your GraphQL schema for anybody who wants to develop client apps
  • deprecationReason: Mark this field as deprecated

shish/gqla 适用场景与选型建议

shish/gqla 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.87k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2023 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「graphql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 shish/gqla 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 shish/gqla 我们能提供哪些服务?
定制开发 / 二次开发

基于 shish/gqla 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 30.87k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-01