2017 © Pedro Peláez
 

library graphql-mapper

A GraphQL model mapper

image

arthem/graphql-mapper

A GraphQL model mapper

  • Wednesday, August 16, 2017
  • by arthem
  • Repository
  • 4 Watchers
  • 32 Stars
  • 94 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

[ABANDONED] GraphQL Mapper

Build Status SensioLabsInsight Scrutinizer Code Quality, (*1)

This library allows to build a GraphQL schema based on your model. It depends on the GraphQL PHP implementation, (*2)

See graphql-mapper-demo for a full working example!, (*3)

Installation

This is installable via Composer as arthem/graphql-mapper:, (*4)

composer require arthem/graphql-mapper

Setup / Configuration

Create your schema:, (*5)

# /path/to/your/mapping/file.yml

interfaces:
    Character:
        resolve:
            handler: doctrine
        model: AppBundle\Entity\Character
        description: A character in the Star Wars Trilogy
        fields:
            id:
                type: Int!
                description: The id of the character.
            name:
                type: String!
                description: The name of the character.
            friends:
                type: "[Character]"
                description: The friends of the character, or an empty list if they have none.
            appearsIn:
                type: "[Episode]"
                description: Which movies they appear in.

types:
    Episode:
        description: One of the films in the Star Wars Trilogy
        values:
            NEWHOPE:
                value: 4
                description: Released in 1977.
            EMPIRE:
                value: 5
                description: Released in 1980.
            JEDI:
                value: 6
                description: Released in 1983.

    Human:
        resolve:
            handler: doctrine
        model: AppBundle\Entity\Human
        description: A humanoid creature in the Star Wars universe.
        interfaces: Character
        fields:
            id:
                description: The id of the human.
            name:
                description: The name of the human.
            friends:
                type: "[Character]"
                description: The friends of the human, or an empty list if they have none.
            appearsIn:
                type: "[Episode]"
                description: Which movies they appear in.
            homePlanet:
                description: The home planet of the human, or null if unknown.

    Droid:
        resolve:
            handler: doctrine
        model: AppBundle\Entity\Droid
        description: A mechanical creature in the Star Wars universe.
        interfaces: Character
        fields:
            id:
                description: The id of the droid.
            name:
                description: The name of the droid.
            friends:
                type: "[Character]"
                description: The friends of the droid, or an empty list if they have none.
            appearsIn:
                type: "[Episode]"
                description: Which movies they appear in.
            primaryFunction:
                description: The primary function of the droid.

query:
    fields:
        hero:
            resolve:
                method: getHero
            type: Character
            args:
                episode:
                    description: If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.
                    type: Episode
        human:
            type: Human
            args:
                id:
                    description: id of the human
                    type: String!
        droid:
            type: Droid
            args:
                id:
                    description: id of the droid
                    type: String!
        date:
            type: "[String]"
            description: The current time
            resolve:
                function: getdate
                no_args: true

mutation:
    fields:
        createDroid:
            type: Droid
            resolve:
                method: createDroid
            args:
                id:
                    type: Int!
                    description: The id of the droid.
                name:
                    type: String!
                    description: The name of the droid.
                primaryFunction:
                    type: String
                    description: The primary function of the droid.
                appearsIn:
                    type: "[Episode]"
                    description: Which movies they appear in.

NB: listOf types must be wrapped by quotes type: "[User]", (*6)

Usage

// entry.php
use Arthem\GraphQLMapper\GraphQLManager;
use Arthem\GraphQLMapper\SchemaSetup;
use Arthem\GraphQLMapper\Exception\QueryException;

// bootstrap.php
require_once '../vendor/autoload.php';

// replace with mechanism to retrieve Doctrine EntityManager in your app
$entityManager = getEntityManager();

// GraphQL part
$paths          = ['/path/to/your/mapping/file.yml'];
$schemaFactory  = SchemaSetup::createDoctrineYamlSchemaFactory($paths, $entityManager);
$graphQLManager = new GraphQLManager($schemaFactory);

try {
    $data = $graphQLManager->query($_POST['query']);
    echo json_encode($data);
} catch (QueryException $e) {
    echo json_encode($e);
}

Ready to query:, (*7)

curl -XPOST 'http://localhost/entry.php' -d 'query=query FooBar {
    luke: hero(episode: EMPIRE) {
        id,
        name,
        friends {
            id, name
        }
    },
    droid(id: "2001") {
        primaryFunction
    }
}'

Custom Resolver

Resolvers are responsible for creating function (Closure) to resolve the data. The way to use a specific factory is to define the handler key in the resolve node. Internal handlers are: property, callable and doctrine., (*8)

But you can define your own!, (*9)

Create your CustomResolver that implements Arthem\GraphQLMapper\Schema\Resolve\ResolverInterface, (*10)

Then register it to the SchemaFactory:, (*11)

$schemaFactory  = SchemaSetup::createDoctrineYamlSchemaFactory($paths, $entityManager);
$schemaFactory->addResolver(new CustomResolver());

Custom Guesser

TODOC, (*12)

License

Released under the MIT License., (*13)

The Versions

16/08 2017

dev-master

9999999-dev https://github.com/4rthem/graphql-mapper

A GraphQL model mapper

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur de Moulins

model mapping graphql

20/05 2016

0.0.2

0.0.2.0 https://github.com/4rthem/graphql-mapper

A GraphQL model mapper

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur de Moulins

model mapping graphql

06/03 2016

0.0.1

0.0.1.0 https://github.com/4rthem/graphql-mapper

A GraphQL model mapper

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur de Moulins

model mapping graphql