PHP code example of phps-cans / harmony-graphql-tool

1. Go to this page and download the library: Download phps-cans/harmony-graphql-tool 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/ */

    

phps-cans / harmony-graphql-tool example snippets


namespace Foo;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

class MyType implements \PsCs\Harmony\Graphql\Tool\GraphqlTypeInterface {
    public $id = "foo";
    public function getId() {
        return $this->id;
    }
    static public function resolveField(MyType $value, $args, $context, ResolveInfo $info)  {
        switch ($info->fieldName) {
            case 'id':
              return $bill->getId();
            default:
              return null;
        }
    }
    static public function getType($typeRegistry): ObjectType {
        return new ObjectType([
            "name" => "MyType",
            "fields" => [
                "id" => Type::nonNull(Type::id())
            ],
            "resolveField" => [self::class, "resolveField"]
        ]);
    }
  }

namespace Foo;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\FieldDefinition;

class MyGraphqlQueryInterfaceImplementation extends MydataFinder implements \PsCs\Harmony\Graphql\Tool\GraphqlQueryInterface {

    public function getQueryField($typeRegistry): FieldDefinition {
        $that = $this;
        return FieldDefinition::create([
            "name" => "billPerYear",
            "type" => $typeRegistry->get((MyType::class)."Type"),
            'args'    => [
                'id' => Type::nonNull(Type::id())
            ],
            "resolve" => function($rootValue, $args) use ($that) {
                return $that->findOneById($args["id"]);
            }
        ]);
    }
  }

namespace Foo;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\FieldDefinition;

class MyGraphqlQueriesInterfaceImplementation extends MydataFinder implements \PsCs\Harmony\Graphql\Tool\GraphqlQueriesInterface {

    public function getQueryField($typeRegistry): array {
        $that = $this;
        return [FieldDefinition::create([
            "name" => "billPerYear",
            "type" => $typeRegistry->get((MyType::class)."Type"),
            'args'    => [
                'id' => Type::nonNull(Type::id())
            ],
            "resolve" => function($rootValue, $args) use ($that) {
                return $that->findOneById($args["id"]);
            }
        ])];
    }
  }


namespace Foo\ServiceProvider;

use Interop\Container\ServiceProvider;
use GraphQL\Type\Schema;

use Foo\MyGraphqlQueryInterfaceImplementation;
use Interop\Container\ContainerInterface as Container;

class Graphql implements ServiceProvider {
 public function getServices()
    {
        return [
            (MyGraphqlQueryInterfaceImplementation::class)."Type" => [self::class, 'getMyGraphqlQueryInterfaceImplementation'],
            (\PsCs\Harmony\Graphql\Tool\GraphqlQueryInterface::class) => [self::class, 'getGraphqlQueryQueue'],
        ]; // By convention
    }
    public static function getMyGraphqlQueryInterfaceImplementation(Container $container) {
        return new MyGraphqlQueryInterfaceImplementation();
    }

    public static function getGraphqlQueryQueue(Container $container) {
        return [
            (MyGraphqlQueryInterfaceImplementation::class)."Type"
        ];
    }
}
 
    $container = egister(new \PsCs\Harmony\Graphql\Tool\ServiceProvider\DefaultServiceProvider());
    $container->register(new \Foo\ServiceProvider\Graphql);
    $standardServer = $container->get(\GraphQL\Server\StandardServer::class);
    // ....


    $container = ew \PsCs\Harmony\Graphql\Tool\Registry\Registry($container);
    $registry->get(MyTypeFoo::class);
    //...