Giter Club home page Giter Club logo

lumen-lighthouse-graphql's Introduction

Lumen + GraphQL - by Lighthouse

This is an example project to show how to implement nuwave/lighthouse on a Lumen (Laravel) project.

Steps to reproduce

lumen new lumen-lighthouse-graphql
cd lumen-lighthouse-graphql
composer update
composer require nuwave/lighthouse
cp .env.example .env

setup your configuration folders, and lighthouse defaults folders as so:

mkdir config && mkdir app/Models && mkdir app/GraphQL && mkdir app/GraphQL/Mutations && mkdir app/GraphQL/Queries && mkdir app/GraphQL/Scalars && mkdir app/GraphQL/Directives
cp vendor/nuwave/lighthouse/config/config.php config/lighthouse.php

Helping Lumen acknowledge regarding Lighthouse:

$app->withFacades();
$app->withEloquent();
$app->configure('lighthouse');
...
$app->register(Nuwave\Lighthouse\Providers\LighthouseServiceProvider::class);

Add klaravel (Optional)

composer require ksoft/klaravel
cp vendor/ksoft/klaravel/stubs/config/ksoft.php config/ksoft.php
$app->configure('ksoft');

Laravel Passport

Im using a wrapper to be able to have Passport fully integrated on Laravel Lumen´s. For more info or extended usage

https://github.com/dusterio/lumen-passport

composer require dusterio/lumen-passport
composer require appzcoder/lumen-routes-list
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Dusterio\LumenPassport\LumenPassport;
use Laravel\Passport\Passport;
use Carbon\Carbon;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        Passport::tokensExpireIn(Carbon::now()->addDays(15));
        Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
        LumenPassport::tokensExpireIn(Carbon::now()->addYears(50), 2);
    }
}

Under AuthServiceProvider.php add this line in the boot method

// use Dusterio\LumenPassport\LumenPassport;
LumenPassport::routes($this->app, ['prefix' => 'v1/oauth']);

User model

use Laravel\Passport\HasApiTokens;

Enable Passport under Lumen:

$app->configure('auth');
...
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
// Optional if yo want to list your routes.
if ($app->environment() == 'local') {
    $app->register(Appzcoder\LumenRoutesList\RoutesCommandServiceProvider::class);
}

Now your are going to need the migrations:

php artisan make:migration create_users_table
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();

Seeder:

php artisan make:seeder UsersTableSeeder
if (!DB::table('users')->where('email', '[email protected]')->first()) {
  DB::table('users')->insert([
    'name' => 'Kiko Seijo',
    'email' => '[email protected]',
    'password' => app('hash')->make('secret'),
    // 'admin' => 1,
  ]);
}

Finish install Laravel Passport install with: This is the simplest way to setup a login method that Passport provides, there are many more https://laravel.com/docs/master/passport)

php artisan migrate --seed
php artisan passport:keys
php artisan passport:client --personal

Testing install:

mutation Login {
  login(username: "[email protected]", password: "secret") {
    user {
      id
      name
      email
    }
    token
  }
}

Should return:

{
  "data": {
    "login": {
      "user": {
        "id": "1",
        "name": "Kiko Seijo",
        "email": "[email protected]"
      },
      "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJS........your token"
    }
  }
}

Testing auth middleware from Viewer Query:

query ViewerQuery {
  viewer {
    id
    name
    email
  }
}

Add the headers to your query and adjust the token to the one you are getting from previous login:

{
  "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJS........your token"
}

And you should get:

{
  "data": {
    "viewer": {
      "id": "1",
      "name": "Kiko Seijo",
      "email": "[email protected]"
    }
  }
}

GraphQL viewer Query with a Laravel Passport bearer token using Lighthouse auth middleware


Credits

Sunnyface.com, is a software development company from Málaga, Spain. We provide quality software based on the cloud for local & international companies, providing technology solutions with the most modern programming languages.

DevOps Web development
Custom App Development Mobile aplications
Social Apps and Startups Residents mobile application
Graphic designer Freelance senior programmer


Created by Kiko Seijo

lumen-lighthouse-graphql's People

Contributors

kikoseijo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

lumen-lighthouse-graphql's Issues

Call to undefined function Nuwave\Lighthouse\Providers\config_path()

When following the steps to get lighthouse-graphql up and running, I got this error when running the migration command. I solved adding the following package that supports some laravel functions in Lumen, including config_path().

I would recommend you to add the following line to your documentation:

composer require irazasyed/larasupport:~1.0

No config.php present

on this part
cp vendor/nuwave/lighthouse/config/config.php config/lighthouse.php

file is not found, and then I check on my vendor folder to make sure:

  • the package is not have this file/folder inside vendor/nuwave/lighthouse
  • first level folders just : assets & src
  • first level files just : composer.json, LICENSE & readme.md
  • the config folder is nowhere found

using this repo on latest version till this date

the environment is using:

  • lumen 6.x (latest till this date)
  • jensseger mongodb (latest till this date)

Fresh Boilerplate Install, Cannot get it to work.

When navigated to /graphql, the following error is presented:

Argument 2 passed to Nuwave\Lighthouse\Schema\Extensions\ExtensionRequest::__construct() must be of the type string, null given, called in C:\api\vendor\nuwave\lighthouse\src\Support\Http\Controllers\GraphQLController.php on line 41

Also in GraphQL Playground the following error is presented because it loads the error page:

{
  "error": "Unexpected token < in JSON at position 0"
}

Localhost,
Windows 10,
PHP 7.2.1,
Chrome 69.0.3497.100

Con not working with Lumen 6.0.2

I using lumen 6.0.2 and can not install with this

composer require nuwave/lighthouse
Must add in app.php

$app->instance('path.config', app()->basePath() . DIRECTORY_SEPARATOR . 'config');
$app->instance('path.storage', app()->basePath() . DIRECTORY_SEPARATOR . 'storage');

and edit php

//from 
$app->run();
//to
$app->run(
    $app->make('request')
);

And then lighthouse working.

But with 3 packages

composer require ksoft/klaravel
composer require dusterio/lumen-passport
composer require appzcoder/lumen-routes-list

I facing with this error

Problem 1
    - Installation request for albertcht/lumen-helpers ^0.7.0 -> satisfiable by albertcht/lumen-helpers[v0.7].
    - Conclusion: remove laravel/lumen-framework v6.0.2
    - Conclusion: don't install laravel/lumen-framework v6.0.2
    - albertcht/lumen-helpers v0.7 requires laravel/lumen-framework ^5.1 -> satisfiable by laravel/lumen-framework[5.1.x-dev, 5.2.x-dev, 5.3.x-dev, 5.4.x-dev, 5.5.x-dev, 5.6.x-dev, 5.7.x-dev, 5.8.x-dev].
    - Can only install one of: laravel/lumen-framework[5.1.x-dev, v6.0.2].
    - Can only install one of: laravel/lumen-framework[5.2.x-dev, v6.0.2].
    - Can only install one of: laravel/lumen-framework[5.3.x-dev, v6.0.2].
    - Can only install one of: laravel/lumen-framework[5.4.x-dev, v6.0.2].
    - Can only install one of: laravel/lumen-framework[5.5.x-dev, v6.0.2].
    - Can only install one of: laravel/lumen-framework[5.6.x-dev, v6.0.2].
    - Can only install one of: laravel/lumen-framework[5.7.x-dev, v6.0.2].
    - Can only install one of: laravel/lumen-framework[5.8.x-dev, v6.0.2].
    - Installation request for laravel/lumen-framework (locked at v6.0.2, required as ^6.0) -> satisfiable by laravel/lumen-framework[v6.0.2].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.