2017 © Pedro Peláez
 

library laravel-graphql

Laravel wrapper for Facebook's GraphQL

image

xpromx/laravel-graphql

Laravel wrapper for Facebook's GraphQL

  • Tuesday, July 3, 2018
  • by xpromx
  • Repository
  • 2 Watchers
  • 2 Stars
  • 48 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 17 % Grown

The README.md

This project is based of Folklore\GraphQL package, adding Helpers that make more easy the integration with Laravel., (*1)

For more details check: https://github.com/Folkloreatelier/laravel-graphql, (*2)

Installation

1) Composer

composer require xpromx/laravel-graphql

2) Create Config File

in /config/graphql.php check the example inside this repository., (*6)

3) Edit bootstrap/app.php

uncomment the follow lines:, (*7)

$app->withFacades();
$app->withEloquent();

$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);

then add this lines in the same file, (*8)

$app->configure('graphql');
$app->register(Folklore\GraphQL\LumenServiceProvider::class);

4) Create the GraphQL folder

inside this folder: /app/GraphQL with these other folders inside: /Types and /Query, (*9)

Graphiql

An in-browser IDE for exploring GraphQL., (*10)

http://project-name.test/graphiql

Types

Creating new Types = Your Laravel Models check the examples in /Examples/Type folder inside this repository. check the custom types section in this doc., (*11)

Register the Types in your /config/graphql.php., (*12)

<?php
// app/GraphQL/Type/UserType.php

namespace App\GraphQL\Type;

use Xpromx\GraphQL\Definition\Type;
use Xpromx\GraphQL\Type as BaseType;

class UserType extends BaseType
{
    protected $attributes = [
        'name' => 'UserType',
        'description' => 'A User',
        'model' => \App\User::class // Laravel Model
    ];

    public function fields()
    {
        return [
            'id' => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'The id of the user'
            ],

            'created_at' => [
                'type' => Type::date(),
                'description' => 'When the user was created'
            ],

            'updated_at' => [
                'type' => Type::date(),
                'description' => 'When the user was updated'
            ],

        ];
    }

}

Queries

Creating new Queries = Endpoints of your API. Check the examples in /Examples/Query folder inside this repository. example:, (*13)

Register the Queries in your /config/graphql.php., (*14)

<?php
// app/GraphQL/Query/UserQuery.php

namespace App\GraphQL\Query;

use Xpromx\GraphQL\Query;
use Xpromx\GraphQL\Definition\Type;

class UsersQuery extends Query
{
    protected $attributes = [
        'name' => 'UsersQuery',
        'description' => 'A Users Query'
    ];

    public function type()
    {
        return Type::connection('user'); // UserType
    }

}

Query Arguments

these are the filters you can apply automatically for your Queries. In order to use advance filters you have to register the types in your graphql.php config., (*15)


'types' => [ 'Filter' => 'Xpromx\GraphQL\Filter\FilterType', 'FilterCondition' => 'Xpromx\GraphQL\Filter\FilterConditionEnum', ]

Query Example:, (*16)

users(
    id: 1,
    limit: 30,
    page: 2,
    hasRelation: 'user',
    doesntHaveRelation: 'comments',
    orderBy: 'id DESC',
    filter: [{field: "email", condition:CONTAINS, value:"@gmail.com"}]
)
{
    nodes {
        ...
    }

    pageInfo {
        ...
    }

}

Filters conditions:

  • GT
  • GTE
  • LT
  • LTE
  • EQUAL
  • CONTAINS
  • NOT_CONTAINS
  • STARTS_WITH
  • ENDS_WTIH
  • IN
  • NOT_IN
  • NOT_EQUAL
  • NULL
  • NOT_NULL

ConnectionType

Will create the connection for the Type selected, this connection will simplify the queries and adapt the results to http://graphql.org/ standars. format:, (*17)

{
    userQuery(page:1, limit:20){
        nodes{
            id,
            first_name
            email
            ...
        },
        pageInfo{
            current_page
            total
        }
    }
}

DateType

Return the dates formated for humans, (*18)

'updated_at' => [
    'type' => Type::date(),
    'description' => 'When the user was updated'
]

TimeType

Return the time formated for humans, (*19)

'duration' => [
    'type' => Type::time(),
    'description' => 'Movie duration'
]

HasManyType

This one return a list of the Type selected, for Relations OneToMany, (*20)

// UserType.php
'comments' => Type::hasMany('comment')

HasOneType

For OneToOne Relations, (*21)

// CommentType
'user' => Type::hasOne('user')

MetaType

When you need to return a Json object use the MetaType field, (*22)

// UserType
'meta' => [
    'type' => Type::meta(),
    'description' => 'Extra information about this user'
]

PageInfoType

Return the pagination fields, this one is automatically applied in the ConnectionType, and the fields are:, (*23)

{
    pageInfo
    {
        current_page,
        next_page,
        prev_page,
        last_page,
        per_page,
        total
    }
}

DateField

You can use a custom date field to have a default format and also be able to change the format from the query., (*24)

    public function fields()
    {
        return [
            'updated_at' => Type::dateField($field='updated_at', $format='M j, Y'),
        ]
    }
{
    userQuery(page:1, limit:20){
        nodes{
            id,
            first_name
            created_at(format:"Y-m-d")
            ...
        },

    }
}

TimeField

You can use a custom time field to have a default format and also be able to change the format from the query., (*25)

    public function fields()
    {
        return [
            'updated_at' => Type::timeField($field='updated_at', $format='H:i'),
        ]
    }
{
    userQuery(page:1, limit:20){
        nodes{
            id,
            first_name
            created_at(format:"H:i:s")
            ...
        },

    }
}

The Versions

03/07 2018

dev-master

9999999-dev https://github.com/xpromx/laravel-graphql

Laravel wrapper for Facebook's GraphQL

  Sources   Download

MIT

The Requires

 

by Avatar xpromx

laravel framework lumen