PHP code example of epalshin / object-to-graphql

1. Go to this page and download the library: Download epalshin/object-to-graphql library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

epalshin / object-to-graphql example snippets



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
      }
    ]
  ]
]);