Skip to content

Commit

Permalink
GraphQL api
Browse files Browse the repository at this point in the history
  • Loading branch information
saqueib committed Aug 16, 2017
0 parents commit f2c63a8
Show file tree
Hide file tree
Showing 101 changed files with 14,004 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
APP_NAME=qTwitter
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* text=auto
*.css linguist-vendored
*.scss linguist-vendored
*.js linguist-vendored
CHANGELOG.md export-ignore
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
/.idea
/.vagrant
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
.env
*.DS_Store
40 changes: 40 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}

/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
65 changes: 65 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];

/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
parent::report($exception);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}

/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}

return redirect()->guest(route('login'));
}
}
45 changes: 45 additions & 0 deletions app/GraphQL/Mutation/CreateUserMutation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\GraphQL\Mutation;

use GraphQL;
use App\User;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Mutation;

class CreateUserMutation extends Mutation {

protected $attributes = [
'name' => 'createUser'
];

public function type()
{
return GraphQL::type('User');
}

public function rules()
{
return [
'email' => 'required|email|unique:users',
'name' => 'required|min:2',
'password' => 'required|min:6',
'avatar' => 'url'
];
}

public function args()
{
return [
'name' => ['name' => 'name', 'type' => Type::string()],
'email' => ['name' => 'email', 'type' => Type::string()],
'password' => ['name' => 'password', 'type' => Type::string()],
'avatar' => ['name' => 'avatar', 'type' => Type::string()],
];
}

public function resolve($root, $args)
{
return User::create($args);
}
}
38 changes: 38 additions & 0 deletions app/GraphQL/Mutation/DeleteUserMutation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\GraphQL\Mutation;

use GraphQL;
use App\User;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Mutation;

class DeleteUserMutation extends Mutation {

protected $attributes = [
'name' => 'deleteUser'
];

public function type()
{
return GraphQL::type('User');
}

public function args()
{
return [
'id' => ['name' => 'id', 'type' => Type::nonNull(Type::int())]
];
}

public function resolve($root, $args)
{
if( $user = User::findOrFail($args['id']) ) {
$user->delete();
return $user;
}

return null;
}

}
60 changes: 60 additions & 0 deletions app/GraphQL/Mutation/UpdateUserMutation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\GraphQL\Mutation;

use GraphQL;
use App\User;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Mutation;

class UpdateUserMutation extends Mutation {

protected $attributes = [
'name' => 'updateUser'
];

public function type()
{
return GraphQL::type('User');
}

public function args()
{
return [
'id' => ['name' => 'id', 'type' => Type::nonNull(Type::int())],
'name' => ['name' => 'name', 'type' => Type::string()],
'email' => ['name' => 'email', 'type' => Type::string()],
'password' => ['name' => 'password', 'type' => Type::string()],
];
}

public function rules()
{
return [
'email' => 'email|unique:users',
'name' => 'min:2',
'password' => 'min:6',
'avatar' => 'url'
];
}

public function resolve($root, $args)
{
$user = User::find($args['id']);

if(! $user)
{
return null;
}

// update user
$fields = isset($args['password'])
? array_merge($args, ['password' => bcrypt($args['password'])])
: $args;

$user->update($fields);

return $user;
}

}
51 changes: 51 additions & 0 deletions app/GraphQL/Query/TweetsQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace App\GraphQL\Query;

use GraphQL;
use App\Tweet;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Query;

class TweetsQuery extends Query {

protected $attributes = [
'name' => 'tweets'
];

public function type()
{
return Type::listOf(GraphQL::type('Tweet'));
}

public function args()
{
return [
'id' => ['name' => 'id', 'type' => Type::int()],
'first' => ['name' => 'first', 'type' => Type::int()],
];
}

public function resolve($root, $args)
{

$tweet = new Tweet;

// check for limit
if( isset($args['first']) ) {
$tweet = $tweet->limit($args['first'])->latest('id');
}

if(isset($args['id']))
{
$tweet = $tweet->where('id' , $args['id']);
}

if(isset($args['email']))
{
$tweet = $tweet->where('email', $args['email']);
}

return $tweet->get();
}

}
Loading

0 comments on commit f2c63a8

Please sign in to comment.