Skip to content

Commit

Permalink
Merge pull request #24 from TomHAnderson/feature/custom-doctrine-types
Browse files Browse the repository at this point in the history
Feature/custom doctrine types
  • Loading branch information
TomHAnderson authored Sep 8, 2018
2 parents 15b6d38 + 3dee2e3 commit 7499984
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
27 changes: 27 additions & 0 deletions docs/custommappingtypes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Custom Mapping Types
====================

Doctrine allows `Custom Mapping Types <https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/custom-mapping-types.html>`_
To support Custom Mapping Types you must implement ''ZF\Doctrine\GraphQL\Type\CustomTypeInterface''
on your Custom Doctrine Mapping Type field. This must return a scalar GraphQL type for the custom type.

Additionally you must create a custom GraphQL type for the field for handling serialization, etc.
See ''ZF\Doctrine\GraphQL\Type\DateTimeType'' for an example of a custom GraphQL type.

Add the new custom GraphQL type to your configuration::

'zf-doctrine-graphql-type' => [
'invokables' => [
'datetime_microsecond' => Types\GraphQL\DateTimeMicrosecondType::class,
],
],


.. role:: raw-html(raw)
:format: html

.. note::
Authored by `API Skeletons <https://apiskeletons.com>`_. All rights reserved.


:raw-html:`<script async src="https://www.googletagmanager.com/gtag/js?id=UA-64198835-4"></script><script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', 'UA-64198835-4');</script>`
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ schema, if you wish, allowing deep GraphQL queries on your data with a single en
hydrators
queries
events
custommappingtypes
graphiql
internals

Expand Down
2 changes: 1 addition & 1 deletion docs/use.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This example merges work from a factory into the example. Moving the `$containe
and injecting them into an RPC object will yield a working example.

.. code-block:: php
<?php
use Exception;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
Expand Down
10 changes: 8 additions & 2 deletions src/AbstractAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\SharedEventManagerInterface;
use GraphQL\Type\Definition\Type;
use Doctrine\DBAL\Types\Type as ORMType;
use ZF\Doctrine\GraphQL\Type\CustomTypeInterface;

/**
* Enable caching of build() resources
Expand Down Expand Up @@ -110,9 +112,13 @@ protected function mapFieldType(string $fieldType)
$graphQLType = Type::listOf(Type::string());
break;
default:
// @codeCoverageIgnoreStart
// Do not process unknown for now
$graphQLType = null;

$ormType = ORMType::getType($fieldType);
if ($ormType instanceof CustomTypeInterface) {
$graphQLType = $ormType->mapGraphQLFieldType();
}
// @codeCoverageIgnoreStart
break;
}
// @codeCoverageIgnoreEnd
Expand Down
13 changes: 13 additions & 0 deletions src/Type/CustomTypeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* To use GraphQL with a custom Doctrine type
* implement this interface on the custom doctrine Type
*/

namespace ZF\Doctrine\GraphQL\Type;

interface CustomTypeInterface
{
public function mapGraphQLFieldType();
}
10 changes: 7 additions & 3 deletions src/Type/EntityTypeAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use ZF\Doctrine\GraphQL\Criteria\CriteriaManager;
use ZF\Doctrine\GraphQL\Field\FieldResolver;
use ZF\Doctrine\GraphQL\Event;
use Doctrine\DBAL\Types\Type as ORMType;
use ZF\Doctrine\GraphQL\Type\CustomTypeInterface;

final class EntityTypeAbstractFactory extends AbstractAbstractFactory implements
AbstractFactoryInterface
Expand Down Expand Up @@ -299,9 +301,11 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
}

$graphQLType = $this->mapFieldType($fieldMetadata['type']);
// Override for datetime
if ($fieldMetadata['type'] == 'datetime') {
$graphQLType = $container->get(TypeManager::class)->get(DateTime::class);

// Override for custom fields
$typeManager = $container->get(TypeManager::class);
if ($typeManager->has($fieldMetadata['type'])) {
$graphQLType = $typeManager->get($fieldMetadata['type']);
}

if ($graphQLType && $classMetadata->isIdentifier($fieldMetadata['fieldName'])) {
Expand Down

0 comments on commit 7499984

Please sign in to comment.