PHPackages                             dmavrin/graphql-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. dmavrin/graphql-laravel

ActiveProject[Framework](/categories/framework)

dmavrin/graphql-laravel
=======================

Laravel wrapper for PHP GraphQL

5.1.4(5y ago)0150MITPHPPHP &gt;= 7.1

Since Mar 27Pushed 5y agoCompare

[ Source](https://github.com/dmavrin/graphql-laravel)[ Packagist](https://packagist.org/packages/dmavrin/graphql-laravel)[ RSS](/packages/dmavrin-graphql-laravel/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (8)Versions (53)Used By (0)

Laravel GraphQL
===============

[](#laravel-graphql)

[![Latest Stable Version](https://camo.githubusercontent.com/0169144623818e8a58d199cb3d5b0d246ecccced18155c534b0b38643e5041aa/68747470733a2f2f706f7365722e707567782e6f72672f726562696e672f6772617068716c2d6c61726176656c2f762f737461626c65)](https://packagist.org/packages/rebing/graphql-laravel)[![Build Status](https://camo.githubusercontent.com/0c1a148e1bb401d1d27c9d5a5475f85c5c40c4a531a9609de02c6af031c1ecc4/68747470733a2f2f7472617669732d63692e6f72672f726562696e672f6772617068716c2d6c61726176656c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/rebing/graphql-laravel)[![Style CI](https://camo.githubusercontent.com/a46057caff358df15600d56b1e33cac05e86c7cdd1572e4e307c35e0f1a6cdf2/68747470733a2f2f7374796c6563692e696f2f7265706f732f36383539353331362f736869656c64)](https://styleci.io/repos/68595316)[![License](https://camo.githubusercontent.com/d87eb1a0fa4a1b11555d255a6e9d341f20de6983e951730f3246a61d8f93abbc/68747470733a2f2f706f7365722e707567782e6f72672f726562696e672f6772617068716c2d6c61726176656c2f6c6963656e7365)](https://packagist.org/packages/rebing/graphql-laravel)[![Get on Slack](https://camo.githubusercontent.com/15aa806a5685b67b0cf43579dc41f6aca3a52b9ecab9a7db4c3711ed74c77629/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736c61636b2d6a6f696e2d6f72616e67652e737667)](https://join.slack.com/t/rebing-graphql/shared_invite/enQtNTE5NjQzNDI5MzQ4LWVjMTMxNzIyZjBlNTFhZGQ5MDVjZDAwZDNjODA3ODE2NjdiOGJkMjMwMTZkZmNhZjhiYTE1MjEyNDk0MWJmMzk)

Uses Facebook GraphQL with Laravel 5.5+. It is based on the PHP implementation [here](https://github.com/webonyx/graphql-php). You can find more information about GraphQL in the [GraphQL Introduction](http://facebook.github.io/react/blog/2015/05/01/graphql-introduction.html) on the [React](http://facebook.github.io/react) blog or you can read the [GraphQL specifications](https://facebook.github.io/graphql/). This is a work in progress.

This package is compatible with Eloquent models or any other data source.

- Allows creating **queries** and **mutations** as request endpoints
- Custom **middleware** can be defined for each query/mutation
- Queries return **types**, which can have custom **privacy** settings.
- The queried fields will have the option to be retrieved **dynamically** from the database with the help of the `SelectFields` class.

It offers following features and improvements over the original package by [Folklore](https://github.com/Folkloreatelier/laravel-graphql):

- Per-operation authorization
- Per-field callback defining its visibility (e.g. hiding from unauthenticated users)
- `SelectFields` abstraction available in `resolve()`, allowing for advanced eager loading and thus dealing with n+1 problems
- Pagination support
- Server-side support for [query batching](https://blog.apollographql.com/batching-client-graphql-queries-a685f5bcd41b)

Installation
------------

[](#installation)

#### Dependencies:

[](#dependencies)

- [Laravel 5.5+](https://github.com/laravel/laravel) or [Lumen](https://github.com/laravel/lumen)
- [GraphQL PHP](https://github.com/webonyx/graphql-php)

#### Installation:

[](#installation-1)

**-** Require the package via Composer

```
composer require rebing/graphql-laravel
```

##### Laravel 5.5+

[](#laravel-55)

**1.** Laravel 5.5+ will autodiscover the package, for older versions add the following service provider

```
Rebing\GraphQL\GraphQLServiceProvider::class,
```

and alias

```
'GraphQL' => 'Rebing\GraphQL\Support\Facades\GraphQL',
```

in your `config/app.php` file.

**2.** Publish the configuration file

```
$ php artisan vendor:publish --provider="Rebing\GraphQL\GraphQLServiceProvider"
```

**3.** Review the configuration file

```
config/graphql.php

```

##### Lumen (experimental!)

[](#lumen-experimental)

**1.** Add the following service provider to the `bootstrap/app.php` file

```
$app->register(Rebing\GraphQL\GraphQLLumenServiceProvider::class);
```

**2.** Publish the configuration file

```
$ php artisan graphql:publish
```

**3.** Add the configuration to the `bootstrap/app.php` file *Important:* this needs to be before the registration of the service provider

```
$app->configure('graphql');
...
$app->register(Rebing\GraphQL\GraphQLLumenServiceProvider::class);
```

**4.** Review the configuration file

```
config/graphql.php

```

Usage
-----

[](#usage)

- [Schemas](#schemas)
- [Creating a query](#creating-a-query)
- [Creating a mutation](#creating-a-mutation)
- [Adding validation to mutation](#adding-validation-to-mutation)
- [File uploads](#file-uploads)

##### Advanced Usage

[](#advanced-usage)

- [Authorization](docs/advanced.md#authorization)
- [Privacy](docs/advanced.md#privacy)
- [Query variables](docs/advanced.md#query-variables)
- [Custom field](docs/advanced.md#custom-field)
- [Eager loading relationships](docs/advanced.md#eager-loading-relationships)
- [Type relationship query](docs/advanced.md#type-relationship-query)
- [Pagination](docs/advanced.md#pagination)
- [Batching](docs/advanced.md#batching)
- [Enums](docs/advanced.md#enums)
- [Unions](docs/advanced.md#unions)
- [Interfaces](docs/advanced.md#interfaces)
- [Input Object](docs/advanced.md#input-object)
- [JSON Columns](docs/advanced.md#json-columns)

### Schemas

[](#schemas)

Schemas are required for defining GraphQL endpoints. You can define multiple schemas and assign different **middleware** to them, in addition to the global middleware. For example:

```
'schema' => 'default_schema',

'schemas' => [
    'default' => [
        'query' => [
            'example_query' => ExampleQuery::class,
        ],
        'mutation' => [
            'example_mutation'  => ExampleMutation::class,
        ],
    ],
    'user' => [
        'query' => [
            'profile' => App\GraphQL\Query\ProfileQuery::class
        ],
        'mutation' => [

        ],
        'middleware' => ['auth'],
    ],
],
```

### Creating a query

[](#creating-a-query)

First you need to create a type. The Eloquent Model is only required, if specifying relations.

> **Note:** The `selectable` key is required, if it's a non-database field or not a relation

```
