xpromx / laravel-graphql by xpromx

Laravel wrapper for Facebook's GraphQL
64
5
3
Package Data
Maintainer Username: xpromx
Maintainer Contact: xpromx@gmail.com (xpromx)
Package Create Date: 2017-12-11
Package Last Update: 2022-12-22
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:13:46
Package Statistics
Total Downloads: 64
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 5
Total Watchers: 3
Total Forks: 0
Total Open Issues: 1

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

For more details check: https://github.com/Folkloreatelier/laravel-graphql

Installation

1) Composer

composer require xpromx/laravel-graphql

2) Create Config File

in /config/graphql.php check the example inside this repository.

3) Edit bootstrap/app.php

uncomment the follow lines:

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

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

then add this lines in the same file

$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

Graphiql

An in-browser IDE for exploring GraphQL.

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.

Register the Types in your /config/graphql.php.

<?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:

Register the Queries in your /config/graphql.php.

<?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.


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

Query Example:

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:

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

DateType

Return the dates formated for humans

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

TimeType

Return the time formated for humans

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

HasManyType

This one return a list of the Type selected, for Relations OneToMany

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

HasOneType

For OneToOne Relations

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

MetaType

When you need to return a Json object use the MetaType field

// 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:

{
    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.

    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.

    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")
            ...
        },
        
    }
}