Download the PHP package bonfirecc/laravel-graphql without Composer
On this page you can find all versions of the php package bonfirecc/laravel-graphql. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-graphql
Laravel GraphQL
Core code is from Rabing laravel-graphql
Uses Facebook GraphQL with Laravel 5. It is based on the PHP implementation here. You can find more information about GraphQL in the GraphQL Introduction on the React blog or you can read the GraphQL specifications. 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.
Installation
Dependencies:
Installation:
1- Require the package via Composer in your composer.json
.
2- Run Composer to install or update the new requirement.
or
3- Add the service provider to your app/config/app.php
file
4- Add the facade to your app/config/app.php
file
5- Publish the configuration file
6- Review the configuration file
Usage
- Schemas
- Creating a query
- Creating a mutation
- Adding validation to mutation
- File uploads
Advanced Usage
- Authorization
- Privacy
- Query variables
- Custom field
- Eager loading relationships
- Type relationship query
- Pagination
- Batching
- Enums
- Unions
- Interfaces
- Input Object
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:
Creating a query
First you need to create a type. The Eloquent Model is only required, if specifying relations.
NB! The selectable
key is required, if it's a non-database field or not a relation
Add the type to the config/graphql.php
configuration file
You could also add the type with the GraphQL
Facade, in a service provider for example.
Then you need to define a query that returns this type (or a list). You can also specify arguments that you can use in the resolve method.
Add the query to the config/graphql.php
configuration file
And that's it. You should be able to query GraphQL with a request to the url /graphql
(or anything you choose in your config). Try a GET request with the following query
input
For example, if you use homestead:
Creating a mutation
A mutation is like any other query. It accepts arguments (which will be used to do the mutation) and returns an object of a certain type.
For example, a mutation to update the password of a user. First you need to define the Mutation:
As you can see in the resolve
method, you use the arguments to update your model and return it.
You should then add the mutation to the config/graphql.php
configuration file:
You should then be able to use the following query on your endpoint to do the mutation:
if you use homestead:
Adding validation to a mutation
It is possible to add validation rules to a mutation. It uses the Laravel Validator
to perform validation against the $args
.
When creating a mutation, you can add a method to define the validation rules that apply by doing the following:
Alternatively, you can define rules on each argument:
When you execute a mutation, it will return any validation errors that occur. Since the GraphQL specification defines a certain format for errors, the validation errors are added to the error object as a extra validation
attribute. To find the validation error, you should check for the error with a message
equals to 'validation'
, then the validation
attribute will contain the normal errors messages returned by the Laravel Validator:
The validation errors returned can be customised by overriding the validationErrorMessages
method on the mutation. This method should return an array of custom validation messages in the same way documented by Laravel's validation. For example, to check an email
argument doesn't conflict with any existing data, you could perform the following -
Note: the keys should be in field_name
.validator_type
format so you can return specific errors per validation type.
``
File uploads
For uploading new files just use UploadType
. This support of uploading files is base on https://github.com/jaydenseric/graphql-multipart-request-spec
so you have to upload them as multipart form:
WARNING: when you are uploading files, Laravel will use FormRequest - it means that middlewares which are changing request, will not have any effect...
Advanced usage
- Authorization
- Privacy
- Query variables
- Custom field
- Eager loading relationships
- Type relationship query
- Pagination
- Batching
- Enums
- Unions
- Interfaces
All versions of laravel-graphql with dependencies
illuminate/support Version 5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*
webonyx/graphql-php Version ~0.10.0
laravel/framework Version 5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*