shish/gqla

生成 GraphQL APIs 的注解集合

v1.0.0 2024-02-25 14:48 UTC

This package is auto-updated.

Last update: 2024-08-31 14:04:53 UTC


README

基于 PHP 属性的 GraphQL 模式生成器

因为当您的 web 应用已经有用于内部使用的对象时,您不需要重写或复制所有内容,以便通过 GraphQL 查询这些对象 :)

此处生成的模式基于 https://github.com/webonyx/graphql-php,因此一旦您生成了一个模式,请查看那里的文档以了解如何使用它

示例

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

然后 new \GQLA\Schema(); 将搜索所有注解对象并返回一个 GraphQL 模式,例如

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!]!
}

因此您可以发送一个查询,例如

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

并得到一个响应,例如

{
    "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

这些注解有几个常用参数

  • name:为此类型/字段提供特定的名称(默认:使用类/属性/函数名称)
  • type:为此字段提供特定的类型(默认:从 PHP 类型到 GraphQL 类型的映射中推断)
    • 请注意,如果您的 PHP 类型是 array,则必须使用更具体的 GraphQL 类型指定 type,例如
      #[Field(type: "[String!]!")]
      function get_tags(): array {
          return ["foo", "bar"];
      }
  • args:覆盖任何函数参数的推断类型
    • 与类型一样,请注意,每当 PHP 函数接受数组作为输入时,都需要此参数,例如
      #[Field(args: ["tags" => "[String!]!"])]
      function get_first_post_with_tags(array $tags): Post {
          return ...;
      }
  • extends:默认情况下,公开对象上的公开字段将被添加为该对象的字段。您还可以扩展其他对象(例如,“喜欢”插件可以为您添加一个 number_of_likes 字段到您的 BlogPost 对象)
  • description:为希望开发客户端应用程序的人添加 GraphQL 模式的描述
  • deprecationReason:将此字段标记为已弃用