PHPackages                             lastdragon-ru/lara-asp-graphql - 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. [API Development](/categories/api)
4. /
5. lastdragon-ru/lara-asp-graphql

ActiveLibrary[API Development](/categories/api)

lastdragon-ru/lara-asp-graphql
==============================

This package provides highly powerful `@searchBy`, `@sortBy`, `@stream` directives for Lighthouse. The `@searchBy` directive provides basic conditions like `=`, `&gt;`, `&lt;`, etc, relations, `not (&lt;condition&gt;)`, enums, and custom operators support. All are strictly typed so you no need to use `Mixed` type anymore.

10.3.0(2mo ago)5112.7k—4.9%21MITPHPPHP ^8.3|^8.4

Since May 16Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/LastDragon-ru/lara-asp-graphql)[ Packagist](https://packagist.org/packages/lastdragon-ru/lara-asp-graphql)[ Docs](https://github.com/LastDragon-ru/php-packages)[ RSS](/packages/lastdragon-ru-lara-asp-graphql/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (40)Versions (80)Used By (1)

(Laravel) GraphQL Extensions for Lighthouse
===========================================

[](#laravel-graphql-extensions-for-lighthouse)

This package provides highly powerful [`@searchBy`](docs/Directives/@searchBy.md), [`@sortBy`](docs/Directives/@sortBy.md), [`@stream`](docs/Directives/@stream.md) directives for [lighthouse-php](https://lighthouse-php.com/). The [`@searchBy`](docs/Directives/@searchBy.md) directive provides basic conditions like `=`, `>`, `=8.0.0``0.11.0 ⋯ 0.5.0`Laravel`^12.0.1``HEAD ⋯ 9.0.0``^11.0.8``8.1.1 ⋯ 8.0.0``^11.0.0``7.2.0 ⋯ 6.2.0``^10.34.0``7.2.0 ⋯ 6.2.0``^10.0.0``6.1.0 ⋯ 2.1.0``^9.21.0``5.6.0 ⋯ 5.0.0-beta.1``^9.0.0``5.0.0-beta.0 ⋯ 0.12.0``^8.22.1``3.0.0 ⋯ 0.5.0`Lighthouse`^6.54.0``HEAD ⋯ 9.0.0``^6.36.0``8.1.1 ⋯ 8.0.0``^6.5.0``7.2.0 ⋯ 5.0.0-beta.0``^6.0.0``4.6.0 ⋯ 4.0.0``^5.68.0``3.0.0 ⋯ 2.0.0``^5.8.0``1.1.2 ⋯ 0.13.0``^5.6.1``0.12.0` , `0.11.0``^5.4``0.10.0 ⋯ 0.5.0`Installation
============

[](#installation)

```
composer require lastdragon-ru/lara-asp-graphql
```

Configuration
=============

[](#configuration)

Config can be used, for example, to customize supported operators for each type. Before this, you need to publish it via the following command, and then you can edit `config/lara-asp-graphql.php`.

```
php artisan vendor:publish --provider=LastDragon_ru\\LaraASP\\GraphQL\\PackageProvider --tag=config
```

Directives
==========

[](#directives)

[`@searchBy`](docs/Directives/@searchBy.md)
-------------------------------------------

[](#searchby)

Probably the most powerful directive to provide search (`where` conditions) for your GraphQL queries.

[Read more](docs/Directives/@searchBy.md).

[`@sortBy`](docs/Directives/@sortBy.md)
---------------------------------------

[](#sortby)

Probably the most powerful directive to provide sort (`order by` conditions) for your GraphQL queries.

[Read more](docs/Directives/@sortBy.md).

[`@stream` 🧪](docs/Directives/@stream.md)
-----------------------------------------

[](#stream-)

Unlike the `@paginate` (and similar) directive, the `@stream` provides a uniform way to perform Offset/Limit and Cursor pagination of Eloquent/Query/Scout builders. Filtering and sorting enabled by default via [`@searchBy`](docs/Directives/@searchBy.md) and [`@sortBy`](docs/Directives/@sortBy.md) directives.

[Read more](docs/Directives/@stream.md).

[`@type`](docs/Directives/@type.md)
-----------------------------------

[](#type)

Converts scalar into GraphQL Type. Similar to Lighthouse's `@scalar` directive, but uses Laravel Container to resolve instance and also supports PHP enums.

[Read more](docs/Directives/@type.md).

Scalars
=======

[](#scalars)

Important

You should register the Scalar before use, it can be done via [`AstManipulator`](./src/Utils/AstManipulator.php) (useful while AST manipulation), [`TypeRegistry`](https://lighthouse-php.com/master/digging-deeper/adding-types-programmatically.html#using-the-typeregistry), or as a custom scalar inside the Schema:

```
scalar JsonString
@type(
    class: "LastDragon_ru\\LaraASP\\GraphQL\\Scalars\\JsonStringType"
)
```

[`JsonString`](docs/Scalars/JsonString.md)
------------------------------------------

[](#jsonstring)

Represents [JSON](https://json.org) string.

[Read more](docs/Scalars/JsonString.md).

Scout
=====

[](#scout)

[Scout](https://laravel.com/docs/scout) is also supported 🤩. You just need to add [`@search`](https://lighthouse-php.com/master/api-reference/directives.html#search) directive to an argument. Please note that available operators depend on [Scout itself](https://laravel.com/docs/scout#where-clauses).

Please note that if the [`@search`](https://lighthouse-php.com/master/api-reference/directives.html#search) directive added, the generated query will expect the Scout builder only. So recommended using non-nullable `String!` type to avoid using the Eloquent builder (it will happen if the search argument missed or `null`; see also [lighthouse#2465](https://github.com/nuwave/lighthouse/issues/2465).

Input type auto-generation
==========================

[](#input-type-auto-generation)

The type used with the Builder directives like `@searchBy`/`@sortBy` may be Explicit (when you specify the `input` name `field(where: InputTypeName @searchBy): [Object!]!`) or Implicit (when the `_` used, `field(where: _ @searchBy): [Object!]!`). They are processing a bit differently.

For Explicit type, all fields except unions and marked as ignored (if supported by the directive) will be included.

For Implicit type, the following rules are applied (in this order; concrete directive may have differences, please check its docs):

- Union? - exclude
- Has `Operator` of the concrete directive? - include
- Has `Nuwave\Lighthouse\Support\Contracts\FieldResolver`?
    - Yes
        - Is `Nuwave\Lighthouse\Schema\Directives\RelationDirective`? - Include if is the `Object` or list of `Object`
        - Is `Nuwave\Lighthouse\Schema\Directives\RenameDirective`? - Include if allowed, is `scalar`/`enum` (not `Object`), and no arguments
        - Otherwise - exclude
    - No
        - Is `Object` or has arguments - exclude
        - Otherwise - include
- Ignored (if supported)? - exclude

When converting the field, some of the original directives will be copied into the newly generated field. For the Explicit type, all directives except operators of other directives will be copied. For Implicit type, you can use [`Config::$allowedDirectives`](src/Builder/Config.php#L11-L29 "\LastDragon_ru\LaraASP\GraphQL\Builder\Config::$allowedDirectives") setting to control. Be aware of directive locations - the package doesn't perform any checks to ensure that the copied directive allowed on `INPUT_FIELD_DEFINITION`, it just copies it as is.

Builder field/column name
=========================

[](#builder-fieldcolumn-name)

By default `@searchBy`/`@sortBy` will convert nested/related fields into dot string: eg `{user: {name: asc}}` will be converted into `user.name`. You can redefine this behavior by [`BuilderFieldResolver`](./src/Builder/Contracts/BuilderFieldResolver.php):

```
// AppProvider

$this->app->bind(
    LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderFieldResolver::class,
    MyBuilderFieldResolver::class,
);
```

Builder type detection
======================

[](#builder-type-detection)

Directives like `@searchBy`/`@sortBy` have a unique set of operators and other features for each type of Builder (Eloquent/Scout/etc). Detection of the current Builder works fine for standard Lighthouse directives like `@all`, `@paginated`, `@search`, etc and relies on proper type hints of Relations/Queries/Resolvers. You may get `BuilderUnknown` error if the type hint is missed or the union type is used.

```
