worksome / graphql-helpers
This is my package graphql-helpers
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/support: ^13.0
- jawira/case-converter: ^3.6
- webonyx/graphql-php: ^15.37
Requires (Dev)
- larastan/larastan: ^3.11
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^11.0
- pestphp/pest: ^5.1
- pestphp/pest-plugin-laravel: ^5.0
- phpstan/phpstan: ^2.3
- worksome/coding-style: ^7.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A collection of GraphQL helpers for GraphQL PHP.
Installation
You can install the package via composer:
composer require worksome/graphql-helpers
Usage
Enum Type Registration
The PhpEnumType class can be used to override the GraphQL\Type\Definition\EnumType class with automatic case conversion.
enum MyEnum { case CaseOne; } new \Worksome\GraphQLHelpers\Definition\PhpEnumType(MyEnum::class);
The PhpEnumType class will automatically use the description from the GraphQL\Type\Definition\Description
attribute if it is present.
The CasesDescribedBy attribute
If you require the use of a custom method to retrieve the description, you can use the #[CasesDescribedBy] attribute.
#[\Worksome\GraphQLHelpers\Definition\Attributes\CasesDescribedBy(describer: 'description')] enum MyEnum { case CaseOne; public function description(): string { return 'The First Case!'; } }
The GraphQLName attribute
Each case's GraphQL value name is its case name converted to UPPER_SNAKE_CASE. To use a different name, for example to
keep an existing value that no PascalCase case name converts to, add the #[GraphQLName] attribute to the case.
enum ContractType: string { #[\Worksome\GraphQLHelpers\Definition\Attributes\GraphQLName('CONTRACT_TYPE_W_2')] case ContractTypeW2 = 'W-2'; // CONTRACT_TYPE_W_2 rather than CONTRACT_TYPE_W2 }
The override is used when serializing and parsing values, by the GraphQLConvertable concern and by the
HandlesEnumConversions testing concern. A case with an override only accepts that name. An override that matches
another case's GraphQL name throws a LogicException.
PHPStan Rules
The package ships PHPStan rules that report attribute mistakes at analysis time, rather than when the GraphQL type is built. They also cover enums that are never registered as a GraphQL type.
| Identifier | Reports |
|---|---|
worksome.uniqueEnumGraphQLNames |
Enum cases that share a GraphQL name, from a #[GraphQLName] or the converted case name. |
worksome.validEnumGraphQLName |
A #[GraphQLName] that isn't a valid GraphQL enum value name (e.g. W-2, __TYPE or null). |
worksome.literalEnumGraphQLName |
A #[GraphQLName] given something other than a literal string, which can't be checked. |
worksome.enumCasesDescribedBy |
A #[CasesDescribedBy] describer method that is missing, has a required parameter or doesn't return string or null, or a describer name that isn't a literal string. |
The rules are registered automatically through
phpstan/extension-installer; without the installer, include
vendor/worksome/graphql-helpers/extension.neon in your PHPStan configuration.
Enum Concerns
GraphQLConvertable
The GraphQLConvertable concern is used to easily convert an enum instance to its GraphQL value within your codebase.
enum MyEnum { use \Worksome\GraphQLHelpers\Definition\Concerns\GraphQLConvertable; case CaseOne; } MyEnum::CaseOne->toGraphQLValue(); // CASE_ONE
GraphQLDescribable
The GraphQLDescribable concern is used to easily retrieve the description of an enum instance using the value from a GraphQL\Type\Definition\Description attribute.
enum MyEnum { use \Worksome\GraphQLHelpers\Definition\Concerns\GraphQLDescribable; #[\GraphQL\Type\Definition\Description('The First Case!')] case CaseOne; } MyEnum::CaseOne->description(); // The First Case!
Testing Enums
The HandlesEnumConversions concern adds support for quickly converting an enum to its GraphQL value.
enum MyEnum { case CaseOne; } // In Pest uses(\Worksome\GraphQLHelpers\Testing\Concerns\HandlesEnumConversions::class); $this->enumToGraphQL(MyEnum::CaseOne); // CASE_ONE
Testing
composer test
Changelog
Please see GitHub Releases for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.