lastdragon-ru/lara-asp-graphql-printer

The Awesome Set of Packages for Laravel - The GraphQL Printer.

5.0.0-beta.1 2023-08-28 10:30 UTC

This package is auto-updated.

Last update: 2023-09-08 06:12:19 UTC


README

Independent (from Laravel and Lighthouse) package that allow you to print GraphQL Schema and Queries in highly customized way eg you can choose indent size, print only used/wanted/all types, print only one type, print used/wanted/all directives (it is not possible with standard printer) and even check which types/directives are used in the Schema/Query.

Requirements

Requirement Constraint Supported by
PHP ^8.2 HEAD ⋯ 3.0.0
^8.1 HEAD ⋯ 3.0.0
^8.0 4.6.0 ⋯ 3.0.0
webonyx/graphql-php ^15.4.0 HEAD ⋯ 4.2.1
^15.2.4 4.2.0 ⋯ 4.0.0
^14.11.9 3.0.0

Installation

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

Usage

There are two primary methods: Printer::print() and Printer::export(). The print() will print the current type only, meanwhile export() will print the current type and all used types/directives:

<?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');

The $printer->print($type) is:

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

The $printer->export($type) is:

directive @a
on
    | OBJECT

directive @b
on
    | OBJECT

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

type B
@b
{
    id: ID!
}

Customization

Please see:

  • Settings directory to see built-in settings;
  • Settings interface to see all supported settings;
  • DirectiveResolver interface to define your own way to find all available directives and their definitions;

Filtering

The Printer allows filter out types and directives. This may be useful to exclude them from the schema completely. Filtering also works for queries. Please note that types filtering will work only if the schema is known (the schema is required to determine the type of argument nodes). For some AST node types, their type may also be required.

<?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 {
    public function isAllowedType(string $type, bool $isStandard): bool {
        return $type !== 'Forbidden';
    }
};
$directiveFilter = new class() implements DirectiveFilter {
    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');

The $printer->print($schema) is:

directive @allowed
on
    | FIELD_DEFINITION

type Query {
    allowed: Boolean
    @allowed
}

The $printer->print($query) is:

query {
    allowed
}

Laravel/Lighthouse

It is highly recommended to use lara-asp-graphql package to use the Printer within the Laravel/Lighthouse application.

Testing Assertions

Package also provides few great GraphQL Assertions:

Name Description
assertGraphQLPrintableEquals Prints and compares two GraphQL schemas/types/nodes/etc.
assertGraphQLExportableEquals Exports and compares two GraphQL schemas/types/nodes/etc.

Contributing

This package is the part of Awesome Set of Packages for Laravel. Please use the main repository to report issues, send pull requests, or ask questions.