Download the PHP package scriptle/laragraph without Composer
On this page you can find all versions of the php package scriptle/laragraph. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download scriptle/laragraph
More information about scriptle/laragraph
Files in scriptle/laragraph
Package laragraph
Short Description graphql-php wrapper for Laravel using PHP8 attributes
License MIT
Informations about the package laragraph
LaraGraph
[WIP] An elegant wrapper to webonyx/graphql-php
that uses PHP 8 attributes.
Installation
Requirements: PHP 8
composer require scriptle/laragraph
Usage
Publish the config file:
php artisan vendor:publish --provider=Scriptle\\Laragraph\\LaragraphServiceProvider
Amend the schemas to your preference.
Folder Structure
- Mutation roots live in
app/GraphQL/Mutations
- Query roots live in
app/GraphQL/Queries
- Types live in
app/GraphQL/Types
app/Models
is also traversed for models with GraphQL Type
attributes present.
Schema
Schemas are defined in config/laragraph.php
under the schemas
key.
The prefix (gql
) is prepended to all schemas. The schema key is the URL. So for v1
, the full URL is /gql/v1
.
Within the schema
key you define your GraphQL schema as usual, referencing the root types by string.
You can add custom middleware
with that key, such as an entire schema limited to admin users.
Then, you must add your root types as you defined them, like so:
Attributes
On the class, add the following attribute:
#[Type]
on its own will use the class name as the GraphQL type name.
#[Type('Person')]
will use Person
as the GraphQL type name.
#[Type('User', 'A user in the database.')]
will use User
as the GraphQL type name, plus a GraphQL introspection descriptor for this type.
Defining Fields
To define simple fields returned through Eloquent, you can simply attach them with the Type
attribute:
Note: Eloquent relationships are automatically supported this way.
Fields with a custom resolver
If your field is not database-based, or needs to be passed through a callback you can use this feature.
EITHER:
Format: resolve
+ (StudlyCase) LastName
+ Field
= resolveLastNameField
OR
Function can be named anything as long as it has the ResolvesFor
attribute. This hooks in nicely with Laravel's custom getters.
OR LASTLY:
Move the Field attribute down to the function instead, if you prefer this style.
Argument-ed Fields
Some fields, especially in queries, need arguments. The syntax is direct GraphQL definition syntax:
#[Field('lookupUser("User ID" id: String!)', 'User', 'Look up a user by ID.')]
This defines a lookupUser
field with 1 argument, id
of type String!
(with ! so it must be non-null) and with introspection description of User ID
.
The same can be done for mutations or querying fields on types.
Custom Types
Custom Types can be defined like the User model above, and can be placed in the app/GraphQL/Types
folder to keep them separate from your models. This is useful for mutation responses, or other complex response structures.
Types are referenced by their type name (as registered by the #[Type]
attribute), just with regular GraphQL. There is no need to create a variable or reference classes directly!
Caching
The config/laragraph.php
will be cached along with Laravel's regular config caching (php artisan config:cache
|| php artisan config:clear
)
You can also cache Laragraph's Type mappings to avoid processing these on each request. Use php artisan gql:cache
to cache, and php artisan gql:clear
to remove.