https://github.com/palshin/php-object-to-graphql
Utility helper for generating GraphQL types with webonyx/graphql-php
https://github.com/palshin/php-object-to-graphql
graphql php
Last synced: 3 months ago
JSON representation
Utility helper for generating GraphQL types with webonyx/graphql-php
- Host: GitHub
- URL: https://github.com/palshin/php-object-to-graphql
- Owner: palshin
- License: mit
- Created: 2021-06-04T06:08:00.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-04T13:32:59.000Z (about 5 years ago)
- Last Synced: 2025-12-14T10:39:59.395Z (7 months ago)
- Topics: graphql, php
- Language: PHP
- Homepage:
- Size: 41 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# ObjectToGraphQL
I noticed that in my own code (I use [Lighthouse PHP](https://github.com/nuwave/lighthouse) for implementation GraphQL API) quite often I first describe the input type in the schema, then I describe the object for this type (DTO). It creates a sense of repetition and makes type system maintenance more difficult, so I decided to add the ability to programmatically generate types for the schema by its type declaration in code. So in my project I expanded [this recommendation](https://lighthouse-php.com/5/digging-deeper/adding-types-programmatically.html#native-php-types) for third case: DTO for custom resolvers.
## Installation
You can install the package via composer:
```bash
composer require epalshin/object-to-graphql
```
## Usage
```php
use GraphQL\Type\Definition\ObjectType;
use Palshin\ObjectToGraphQL\ObjectToGraphQL;
use Palshin\ObjectToGraphQL\Attributes\GraphQLArrayType;
use Palshin\ObjectToGraphQL\Attributes\GraphQLObjectType;
#[GraphQLObjectType(typeCategory: ObjectToGraphQL::TYPE_CATEGORY_INPUT)]
class ProductCreateDTO
{
public string $name;
public string $description;
public float $price;
public bool $isPublic;
#[GraphQLArrayType(ObjectToGraphQL::STRING, allowsNull: false)]
public array $photoUrls;
public ?ProductCategoryCreateDTO $category;
}
class ProductCategoryCreateDTO
{
public string $name;
public string $description;
public int $sortOrder;
}
$objectToGraphQL = new ObjectToGraphQL();
[ $productCreateDto, $productCategoryCreateDto ] = $objectToGraphQL->getObjectTypes(ProductCreateDTO::class);
// and now you can register $objectType in your schema
$mutationType = new ObjectType([
'name' => 'Mutation',
'fields' => [
'productCreate' => [
'type' => $product,
'args' => [
'input' => $productCreateDto,
],
'resolve' => function($rootValue, $args) {
// TODO
}
]
]
]);
```
The resulting GraphQL schema will be:
```graphql
input ProductCreateDTOInput {
name: String!
description: String!
price: Float!
isPublic: Boolean!
photoUrls: [String!]!
category: ProductCategoryCreateDTOInput
}
input ProductCategoryCreateDTOInput {
name: String!
description: String!
sortOrder: Int!
}
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Eugene Palshin](https://github.com/palshin)
- [All Contributors](../../contributors)
## TODO
- [x] Add cyclic addition of types to schema
- [x] Add processing for union types
- [x] Add processing for type category and suffixes parameters passing through attribute
- [x] Add strict mode for early error detection
- [x] Add custom exceptions
- [ ] Write more tests
- [ ] Add possibility for adding description for generated types
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.