wwwision/types-graphql
Maintainers
Requires
- php: >=8.1
- webmozart/assert: ^1.11
- wwwision/types: ^1.5
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.1
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^4.0.x-dev
Suggests
None
Provides
None
Conflicts
None
Replaces
None
MIT 0b424d4d817649b1459e06380b603bc128ed703b
- bwaidelich <b.waidelich.woop@wwwision.de>
README
Integration for the wwwision/types package that allows to generate GraphQL schemas from PHP code
Usage
This package can be installed via composer:
composer require wwwision/types-graphql
To generate a GraphQL schema, create a class with at least one public method with a Query attribute:
final class SomeApi { #[Query] public function ping(string $input): string { return strtoupper($input); } }
Now, this API can be used to create a GraphQL schema:
// ... $generator = new GraphQLGenerator(); $schema = $generator->generate(SomeApi::class)->render(); assert($schema === 'type Query { ping(input: String!): String! } ');
Advanced types
If you API class refers to more complex types (using attributes from the wwwision/types package) in method parameters or return types, they will be added to the schema too.
Type constraints
The GraphQL schema does not have a notion of advanced type constraints. But the GraphQLGenerator can turn them into graphql-constraint directives that can be interpreted by consumers. It also adds the constraint rules to descriptions of the corresponding fields.
Custom resolvers
With version 1.2.0 custom functions can be registered that extend the behavior of types dynamically.
Note The signature of the custom resolver closure has to contain the extended type as first argument and specify the return type, for example:
new CustomResolver('SomeObject', 'someCustomField', fn (SomeObject $thisIsRequired, string $thisIsOptional): bool => true)
Contribution
Contributions in the form of issues or pull requests are highly appreciated
License
See LICENSE
