Skip to content

This package contains some tool to be able to implement graphql easily

Notifications You must be signed in to change notification settings

phps-cans/harmony-graphql-tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 

Repository files navigation

harmony-graphql-tool

This package try to make the using of webonyx's Graphql implementation easyer when using PSR 11's container. To be easyer you must use ServiceProvider

It provides :

  • A Type Registry. This is intented to register Type and not query. What it does? It ask to the container if the Type asked by object is registered, else it use the Type name asked as a Classname (removing Type at the end of the Type asked if it exist). If the Type as a class using his name, it check if it implements GraphqlTypeInterface, if it does it use this type.
  • GraphqlQueryInterface: All query's object should implements this interface to be able to be register into the schema
  • GraphqlTypeInterface: All type's object should implements this interface to be discovered by the Type Registry
  • DefaultServiceProvider : A service provider which create a graphql's server / a schema automatically

How to use

Using ServiceProvider

You must register the identifier's name list of instance of your query object (implementing \PsCs\Harmony\Graphql\Tool\GraphqlQueryInterface) under the name PsCs\Harmony\Graphql\Tool\GraphqlQueryInterface. By example:

  • First define your TypeClass:
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"]
        ]);
    }
  }
  • Secondly, create your Query object:
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"]);
            }
        ]);
    }
  }

Alternatively, you can create an object which returns multiple query:

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"]);
            }
        ])];
    }
  }
  • Then create your serviceProvider
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"
        ];
    }
}

Then register service provider needed:

    $container = require_once("container.php");
    $container->register(new \PsCs\Harmony\Graphql\Tool\ServiceProvider\DefaultServiceProvider());
    $container->register(new \Foo\ServiceProvider\Graphql);
    $standardServer = $container->get(\GraphQL\Server\StandardServer::class);
    // ....

Without service provider

This package attempt to make easy the use of the library with service provider. If you do not use the service provider, this package cannot register automatically instances. You still can use the Registry class:

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

TODO

Implements type discovery using doctrine's annotation

About

This package contains some tool to be able to implement graphql easily

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages