2017 © Pedro Peláez
 

library doctrine-graphql

Use Doctrine entities as GraphQL types

image

rahuljayaraman/doctrine-graphql

Use Doctrine entities as GraphQL types

  • Monday, August 7, 2017
  • by rahul.jayaraman
  • Repository
  • 4 Watchers
  • 8 Stars
  • 3,698 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 6 Open issues
  • 2 Versions
  • 8 % Grown

The README.md

WARNING: This repo is just an experiment & is not maintained. Please use at your own risk. Suggest using https://github.com/Ecodev/graphql-doctrine instead

Doctrine GraphQL Mapper

Builds GraphQL types out of doctrine entities, (*1)

Installation

$> curl -sS https://getcomposer.org/installer | php
$> php composer.phar require rahuljayaraman/doctrine-graphql

Requirements

PHP >=5.4, (*2)

Overview

The mapper builds GraphQL types out of doctrine entities. It's built on top of webonyx/graphql-php and maps doctrine entities to an ObjectType graph at runtime., (*3)

Here's an example. Consider the following schema, (*4)

Employee
│
└───Company (getCompanies)
|
└───User (getUser)

We can extract the type for Employee by using, (*5)

Mapper::extractType(Employee::class)

Associations are recursively extracted as well. So $employee->getCompanies() would return ListOf Type Company & $employee->getUser() would return Type User, (*6)

NOTE: This library is not responsible for any of your GraphQL setup like setting up routes, root nodes etc.

TODO

Usage

Setup

Setup accepts 3 args. Doctrine's EntityManager, a setter method & a getter method to a type store (a data structure which stores types)., (*7)

//Setup code, I use this in a laravel service provider
use RahulJayaraman\DoctrineGraphQL\Mapper;
use Doctrine\ORM\EntityManager;

Mapper::setup(
    app(EntityManager::class),
    function ($typeName, $type) {
        Cache::add($type, $typeName);
    },
    function ($typeName) {
        return Cache::get($typeName);
    }
);

Cache above could be replaced by any store. Eg. using Folkloreatelier/laravel-graphql's store, (*8)

use Folklore\GraphQL\Support\Facades\GraphQL;

Mapper::setup(
    app(EntityManager::class),
    function ($typeName, $type) {
        GraphQL::addType($type, $typeName);
    },
    function ($typeName) {
        return GraphQL::type($typeName);
    }
);

Extract type

To extract the type, (*9)

Mapper::extractType(Entity::class);

We could place it here if using with Folkloreatelier/laravel-graphql., (*10)

public function type()
{
   return Mapper::extractType(Entity::class);
}

Default resolver

For now, given a field name, say fieldName, the mapper will look for a getFieldName getter method on the entity. There are plans to allow customization here., (*11)

Register additional fields

For registering additional fields, one can use the RegisterField annotation., (*12)

RegisterField accepts name, type and args., (*13)

name accepts a string., (*14)

type accepts either an internal type or any of the extracted entities., (*15)

args accepts an array of tuples in the form of {{string, type}}, (*16)

Here's an example, (*17)

use RahulJayaraman\DoctrineGraphQL\Annotations\RegisterField;


/**
 * getEmployee
 *
 * @RegisterField(name="CustomName" type="Employee", args={{"slug", "string"}})
 */
public function getEmployee($slug)
{
    return ...
}

Blacklist fields

Fields can be blacklisted using the BlacklistField annotation. Here's an example., (*18)

use RahulJayaraman\DoctrineGraphQL\Annotations\BlacklistField;

/**
 * @var string
 * @ORM\Column(type="string")
 * @BlacklistField()
 */
private $password;

Complementary Tools

The Versions

07/08 2017

dev-master

9999999-dev

Use Doctrine entities as GraphQL types

  Sources   Download

The Requires

 

by Rahul Jayaraman

27/07 2017

v0.1.0

0.1.0.0

Use Doctrine entities as GraphQL types

  Sources   Download

The Requires

 

by Rahul Jayaraman