Download the PHP package rebing/graphql-laravel-select-fields without Composer
On this page you can find all versions of the php package rebing/graphql-laravel-select-fields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rebing/graphql-laravel-select-fields
More information about rebing/graphql-laravel-select-fields
Files in rebing/graphql-laravel-select-fields
Package graphql-laravel-select-fields
Short Description SelectFields (Eloquent eager loading) for rebing/graphql-laravel - optimizes GraphQL queries by analysing requested fields to build minimal SELECT and eager-load only the needed relations.
License MIT
Informations about the package graphql-laravel-select-fields
SelectFields for graphql-laravel
Optimizes GraphQL queries backed by Eloquent models. Analyzes the GraphQL
request's field selection to generate minimal SELECT columns and eager-load
only the requested relations - preventing N+1 queries and over-fetching.
This is an optional companion package for rebing/graphql-laravel.
Requirements
- PHP ^8.2
- Laravel 12+
- rebing/graphql-laravel 10.0.0+
Installation
The package auto-discovers its service provider. No manual registration is needed. On boot it:
- Registers a
ResolverParameterInjectorso thatClosureandSelectFieldstype-hints in resolver methods work automatically. - Replaces the core pagination types with SelectFields-aware subclasses that
implement
WrapTypeand mark metadata fields as non-selectable.
Quick Start
1. Add model to your Type:
2. Use $getSelectFields in your Query:
When a client queries { users { id email posts { title } } },
SelectFields generates:
Only the requested columns are fetched, and the posts relation is
eager-loaded in a single query.
Usage
Resolver Injection Patterns
SelectFields can be injected into your resolve() method in two ways.
Closure (lazy - recommended)
The SelectFields instance is only constructed when you call
$getSelectFields(). If your resolver has an early return path (cache hit,
authorization check), you avoid the cost of walking the query plan.
Class (eager)
The SelectFields instance is constructed before your resolver runs.
Type Configuration
The model Attribute
The model attribute on your Type's $attributes array is required for
SelectFields to work. It enables:
- Table-qualified column names (
"users"."id"instead of"id") - Automatic primary key inclusion in SELECT
- Eloquent relation traversal for eager loading
Without model, SelectFields operates in a degraded mode - no table
qualification, no primary key inclusion, no relation detection.
Field Configuration Keys
These keys can be placed in the arrays returned by your Type's fields() method:
| Key | Type | Default | Purpose |
|---|---|---|---|
selectable |
bool |
true |
Whether to include the field in SQL SELECT. Set to false for computed/virtual fields that have no database column (e.g. accessors). |
is_relation |
bool |
true |
Whether sub-fields represent an Eloquent relationship. Set to false for JSON columns or cast arrays. |
always |
string\|string[] |
- | Additional columns always included in SELECT when this field is requested. Useful for computed properties that depend on other columns. |
query |
Closure |
- | Custom query callback applied to the Eloquent eager-loading query for this relation. |
alias |
string\|Closure\|Expression |
field name | Maps a GraphQL field name to a different database column or relation method name. |
Eager Loading Relationships
The profile and posts relations must also exist on the User Eloquent model.
If some fields are required for the relation to load or for validation, you can
define an always attribute that will add the given attributes to select.
The attribute can be a comma separated string or an array of attributes to always include.
At this point we have a profile and a post type as expected for any model:
Custom Relation Queries
You can specify a query callback that will be applied to the Eloquent
eager-loading query for a relation:
Pagination
Pagination will be used if a query or mutation returns a PaginationType.
Note that unless you use resolver middleware, you will have to manually supply both the limit and page values:
Query posts(limit:10,page:1){data{id},total,per_page} might return:
Note that you need to add the extra data object when you request paginated
resources, as the returned data gives you the paginated resources in a data
object at the same level as the returned pagination metadata.
Simple Pagination
Simple Pagination
will be used if a query or mutation returns a SimplePaginationType.
SimplePaginationType exposes the following fields: data (the paginated
items), per_page, current_page, from, to, and has_more_pages. Unlike
full pagination, total and last_page are not available.
Cursor Pagination
Cursor Pagination
will be used if a query or mutation returns a CursorPaginationType.
CursorPaginationType exposes the following fields: data (the paginated
items), per_page, previous_cursor (String, nullable), and next_cursor
(String, nullable).
Pagination type auto-replacement
This package automatically replaces the core pagination types with
SelectFields-aware subclasses. The subclasses implement WrapType (so
SelectFields can traverse into the paginated data) and mark metadata fields like
total, per_page, etc. as selectable: false (so they are not included in
SQL SELECT).
If you have set a custom pagination type in your config, the package will not override it.
JSON Columns
When using JSON columns in your database, the field won't be defined as a
"relationship", but rather a simple column with nested data. Use the
is_relation attribute to tell SelectFields not to treat it as an Eloquent
relation:
Wrap Types
If you use SelectFields in a query that returns a
wrap type, your wrapper
class must implement the WrapType marker interface. This tells
SelectFields to look through the wrapper's data field to find the underlying
model type and generate the correct SELECT/WITH clauses.
The package's pagination types already implement this interface. Custom
pagination classes configured via the pagination_type,
simple_pagination_type, or cursor_pagination_type config keys must also
implement it.
Abstract Types (Unions and Interfaces)
When using SelectFields with union or interface types, custom query callbacks
on relation fields defined in member/concrete types are supported. SelectFields
will match the concrete type at eager-load time and apply the callback
automatically.
Note: When a query includes inline fragments on multiple member types that each request different relations, SelectFields will merge all requested relations into the eager-load set.
Note: For union types, SelectFields cannot determine the concrete type at
query-build time, so it uses SELECT * instead of selecting specific columns.
API Reference
SelectFields
WrapType
SelectFieldsParameterInjector
Implements Rebing\GraphQL\Support\Contracts\ResolverParameterInjector. This
is registered automatically by the service provider. You only need to interact
with it if you're building a custom SelectFields subclass:
Known Limitations
- Resolving fields via aliases will only resolve them once, even if the fields have different arguments (Issue).
License
MIT
All versions of graphql-laravel-select-fields with dependencies
illuminate/contracts Version ^12.0|^13.0
rebing/graphql-laravel Version ^10.0.0