Skip to content

A quick and easy way to build prototype GraphQL queries and mutations for your Laravel models and controllers.

License

GPL-3.0, GPL-3.0 licenses found

Licenses found

GPL-3.0
LICENSE
GPL-3.0
license.md
Notifications You must be signed in to change notification settings

blwsh/laravel-graphqlable

Repository files navigation

LaravelGraphQLable

Quick start guide

Simply add the GraphQLable trait to your models or the GraphQL macro to your routes to add them to your schema. fields are automatically mapped to graphql types or can be configured to use a custom graphql field map for models and routes.

Installation

Via Composer

$ composer require uniben/laravelgraphqlable

Usage

Models, GraphQL Types and controllers

Types

LaravelGraphQLable automatically generates GraphQL Types by searching for models which use the GraphQLQueryableTrait

Simply add the GraphQLQueryableTrait trait to your model and query the graphql endpoint at /graphql. LaravelGraphQLable also comes shipped with graphql-playground which you can access at /graphql-playrgound

You may customise what properties, methods and relations your grapql model type exposes using the method graphQLQueryable, graphQLMutatable and graphQLRelations.

Exposing fields for your graphql type
public static function graphQLQueryable() {
    return [
        'id',
        'created_at',
        'updated_at'
    ];
}

Note: Relations you add to the graphQLRelations array must use the GraphQLQueryableTrait

You may also declare mutations for your model using graphQLMutatable. Simply override the public static method and return an array of strings which reference methods that should be included as mutations for the type.

Exposing type mutations
public static function graphQLMutatable(): array {
    return [
        'sayHello'
    ]
}

public function sayHello() {
    return $this->name . ' says hello!';
}
Exposing relations for your graphql type
public static function graphQLRelations(): array {
    return [
        'page'
    ];
}

public function page() {
    $this->hasOne(Page::class);
}
Changing the default GraphQL Type name

The default name the GraphQL type uses is the pluralised version of the model name.

E.g:

class User {
    use GraphQLQueryableTrait;
}

Generates a GraphQL Type called Users.

An example graphql User model query may look something like this:

query { Users { id, name, created_at }}

Controllers

You may declare special routes in your web.php file or api.php file which return a model that uses the GraphQLQueryableTrait

Adding a query to the GraphQL schema via a controller

The route
Route::any('sayHello', 'UsersController@sayHello')->graphQL(App\User::class, 'query');

What's happening there?

First we define a route using the regular Laravel method.

Route::get('sayHello', 'UsersController@sayHello')

We then use the GraphQL query macro which tells LaravelGraphQLable to add this controller method (or closure) to our GraphQLSchema

...->graphQL(App\User::class, 'query');

For the first parameter we provide the class path of the GraphQL Type we wish to return. In the example above we reference a Laravel model which uses the GraphQLQueryableTrait

We do not need to retrieve the type for the model. As long as it uses the trait, it is done automatically.

For the second parameter, we specify if the type should be a query or mutation.

Overriding a controller type name

A route which uses the graphQL macro generates a graphQL type which has a default name of {controller}{Method}{Type} (If your controller name has the word Controller as the suffix it is removed)

If you want to override the default name generated for your type, simply add the name macro to your route.

->graphQL(...)->name('SayHello');

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email ben@blwatson.com instead of using the issue tracker.

Credits

License

license. Please see the license file for more information.

About

A quick and easy way to build prototype GraphQL queries and mutations for your Laravel models and controllers.

Resources

License

GPL-3.0, GPL-3.0 licenses found

Licenses found

GPL-3.0
LICENSE
GPL-3.0
license.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages