PHP code example of bonfirecc / laravel-graphql

1. Go to this page and download the library: Download bonfirecc/laravel-graphql library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bonfirecc / laravel-graphql example snippets


BonfireCC\GraphQL\GraphQLServiceProvider::class,

'GraphQL' => 'BonfireCC\GraphQL\Support\Facades\GraphQL',

	namespace App\GraphQL\Type;

	use GraphQL\Type\Definition\Type;
	use BonfireCC\GraphQL\Support\Type as GraphQLType;

    class UserType extends GraphQLType {

        protected $attributes = [
            'name'          => 'User',
            'description'   => 'A user',
            'model'         => UserModel::class,
        ];

        public function fields()
        {
            return [
                'id' => [
                    'type' => Type::nonNull(Type::string()),
                    'description' => 'The id of the user',
                    'alias' => 'user_id', // Use 'alias', if the database column is different from the type name
                ],
                'email' => [
                    'type' => Type::string(),
                    'description' => 'The email of user',
                ],
                // Uses the 'getIsMeAttribute' function on our custom User model
                'isMe' => [
                    'type' => Type::boolean(),
                    'description' => 'True, if the queried user is the current user',
                    'selectable' => false, // Does not try to query this from the database
                ]
            ];
        }


        // If you want to resolve the field yourself, you can declare a method
        // with the following format resolve[FIELD_NAME]Field()
        protected function resolveEmailField($root, $args)
        {
            return strtolower($root->email);
        }

    }


	'types' => [
		'user' => 'App\GraphQL\Type\UserType'
	]


	GraphQL::addType('App\GraphQL\Type\UserType', 'user');


	namespace App\GraphQL\Query;

	use GraphQL;
	use GraphQL\Type\Definition\Type;
	use BonfireCC\GraphQL\Support\Query;    
	use App\User;

	class UsersQuery extends Query {

		protected $attributes = [
			'name' => 'Users query'
		];

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

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

		public function resolve($root, $args)
		{
			if(isset($args['id']))
			{
				return User::where('id' , $args['id'])->get();
			}
			else if(isset($args['email']))
			{
				return User::where('email', $args['email'])->get();
			}
			else
			{
				return User::all();
			}
		}

	}


    'schemas' => [
		'default' => [
		    'query' => [
                'users' => 'App\GraphQL\Query\UsersQuery'
            ],
            // ...
		]
	]

	namespace App\GraphQL\Mutation;

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

	class UpdateUserPasswordMutation extends Mutation {

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

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

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

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

			$user->password = bcrypt($args['password']);
			$user->save();

			return $user;
		}

	}


    'schemas' => [
		'default' => [
		    'mutation' => [
                'updateUserPassword' => 'App\GraphQL\Mutation\UpdateUserPasswordMutation'
            ],
            // ...
		]
	]

	namespace App\GraphQL\Mutation;

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

	class UpdateUserEmailMutation extends Mutation {

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

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

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

		public function rules(array $args = [])
		{
			return [
				'id' => ['


	class UpdateUserEmailMutation extends Mutation {

		//...

		public function args()
		{
			return [
				'id' => [
					'name' => 'id',
					'type' => Type::string(),
					'rules' => ['


    public function validationErrorMessages ($args = [])
    {
        return [
            'name.se enter your email address',
            'email.email' => 'Please enter a valid email address',
            'email.exists' => 'Sorry, this email address is already in use',                     
        ];
    }



	use GraphQL;
	use GraphQL\Type\Definition\Type;
	use BonfireCC\GraphQL\Support\UploadType;
	use BonfireCC\GraphQL\Support\Mutation;

	class UserProfilePhotoMutation extends Mutation
	{

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

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

		public function args()
		{
			return [
				'profilePicture' => [
					'name' => 'profilePicture',
					'type' => new UploadType($this->attributes['name']),
					'rules' => ['
bash
$ php artisan vendor:publish --provider="BonfireCC\GraphQL\GraphQLServiceProvider"

config/graphql.php