lastdragon-ru/lara-asp-graphql-printer

为 Laravel 提供的出色包集合 - GraphQL 打印机。

6.4.2 2024-09-20 13:09 UTC

README

一个独立的(独立于 Laravel 和 Lighthouse)包,允许您以高度定制的方式打印 GraphQL 模式和查询,例如您可以选择缩进大小,仅打印使用/想要的所有类型,仅打印一个类型,仅打印使用/想要的所有指令(标准打印机无法实现),甚至检查 Schema/查询中使用了哪些类型/指令。

需求

安装

composer require lastdragon-ru/lara-asp-graphql-printer

使用

有两种主要方法:Printer::print()Printer::export()。`print()` 将仅打印当前类型,而 `export()` 将打印当前类型以及所有使用的类型/指令。

<?php declare(strict_types = 1);

use GraphQL\Utils\BuildSchema;
use LastDragon_ru\LaraASP\Dev\App\Example;
use LastDragon_ru\LaraASP\GraphQLPrinter\Printer;
use LastDragon_ru\LaraASP\GraphQLPrinter\Settings\DefaultSettings;

$schema   = BuildSchema::build(
    <<<'GRAPHQL'
    type Query {
        a: A
    }

    type A @a {
        id: ID!
        b: [B!]
    }

    type B @b {
        id: ID!
    }

    directive @a on OBJECT
    directive @b on OBJECT
    GRAPHQL,
);
$type     = $schema->getType('A');
$settings = new DefaultSettings();
$printer  = new Printer($settings, null, $schema);

assert($type !== null);

Example::raw($printer->print($type), 'graphql');
Example::raw($printer->export($type), 'graphql');

$printer->print($type)

type A
@a
{
    b: [B!]
    id: ID!
}

$printer->export($type)

type A
@a
{
    b: [B!]
    id: ID!
}

directive @a
on
    | OBJECT

directive @b
on
    | OBJECT

type B
@b
{
    id: ID!
}

定制

请参阅

  • Settings 目录以查看内置设置;
  • Settings 接口以查看所有支持的设置;
  • DirectiveResolver 接口以定义您自己的方式查找所有可用的指令及其定义;

筛选

注意

默认情况下,内置/内部类型/指令不会打印,如果您需要它们,您应该通过类型/指令定义筛选器允许它们。

打印机允许筛选出类型和指令。这可能有助于完全排除它们从模式中。筛选也适用于查询。请注意,类型筛选仅当模式已知时才会起作用(模式是必需的,以确定参数节点类型)。对于某些 AST 节点类型,其类型也可能需要。

<?php declare(strict_types = 1);

use GraphQL\Language\Parser;
use GraphQL\Utils\BuildSchema;
use LastDragon_ru\LaraASP\Dev\App\Example;
use LastDragon_ru\LaraASP\GraphQLPrinter\Contracts\DirectiveFilter;
use LastDragon_ru\LaraASP\GraphQLPrinter\Contracts\TypeFilter;
use LastDragon_ru\LaraASP\GraphQLPrinter\Printer;
use LastDragon_ru\LaraASP\GraphQLPrinter\Settings\DefaultSettings;

$typeFilter      = new class() implements TypeFilter {
    #[Override]
    public function isAllowedType(string $type, bool $isStandard): bool {
        return $type !== 'Forbidden';
    }
};
$directiveFilter = new class() implements DirectiveFilter {
    #[Override]
    public function isAllowedDirective(string $directive, bool $isStandard): bool {
        return $directive !== 'forbidden';
    }
};

$schema = BuildSchema::build(
    <<<'GRAPHQL'
    type Query {
        allowed: Boolean @forbidden @allowed
        forbidden: Forbidden
    }

    type Forbidden {
        id: ID!
    }

    directive @allowed on FIELD_DEFINITION
    directive @forbidden on FIELD_DEFINITION
    GRAPHQL,
);
$query  = Parser::parse(
    <<<'GRAPHQL'
    query {
        allowed
        forbidden {
            id
        }
    }
    GRAPHQL,
);

$settings = (new DefaultSettings())
    ->setDirectiveFilter($directiveFilter)
    ->setTypeFilter($typeFilter);
$printer  = new Printer($settings, null, $schema);

Example::raw($printer->print($schema), 'graphql');
Example::raw($printer->print($query), 'graphql');

$printer->print($schema)

directive @allowed
on
    | FIELD_DEFINITION

type Query {
    allowed: Boolean
    @allowed
}

$printer->print($query)

query {
    allowed
}

Laravel/Lighthouse

强烈推荐使用 lara-asp-graphql 包在 Laravel/Lighthouse 应用程序中使用 Printer

测试断言

assertGraphQLExportableEquals

导出并比较两个 GraphQL 模式/类型/节点等。

阅读更多.

assertGraphQLPrintableEquals

打印并比较两个 GraphQL 模式/类型/节点等。

阅读更多.

升级

请遵循 升级指南

贡献

此包是 Laravel 精美包集合的一部分。请使用 主仓库报告问题,发送 拉取请求,或 提问