supliu/laravel-graphql 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

supliu/laravel-graphql

最新稳定版本:1.0.1

Composer 安装命令:

composer require supliu/laravel-graphql

包简介

GraphQL with Laravel Framework

README 文档

README

Laravel GraphQL

Latest Stable Version Total Downloads Latest Unstable Version License

The objective of this project is to facilitate the integration of the webonyx/graphql-php with the Laravel Framework

How to install

Use composer to install this package

composer require supliu/laravel-graphql

Execute a publish with artisan command:

php artisan vendor:publish --provider="Supliu\LaravelGraphQL\ServiceProvider"

How to use

You must create your Query and Mutation classes and register on config/graphql.php so that GraphQL can read.

'queries' => [
    'detailHero' => \App\GraphQL\Queries\DetailHero::class
],

'mutations' => [
    'updateHero' => \App\GraphQL\Mutations\UpdateHero::class
]

Query

Below is an example of a Query class that returns the data of a Star Wars hero:

<?php

namespace App\GraphQL\Queries;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use Supliu\LaravelGraphQL\Query;

class DetailHero extends Query
{
    /**
     * @return array
     */
    protected function args(): array
    {
        return [
            'id' => Type::nonNull(Type::int())
        ];
    }
    
    /**
     * @return Type
     */
    protected function typeResult(): Type
    {
        return new ObjectType([
            'name' => 'HeroQueryResult',
            'fields' => [
                'name' => Type::string()
            ]
        ]);
    }

    /**
     * @return mixed
     */
    protected function resolve($root, $args, $context, $info)
    {
        return Hero::find($args['id']);
    }
}

Mutation

Below is an example of a Mutation class that returns if update worked:

<?php

namespace App\GraphQL\Mutations;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use Supliu\LaravelGraphQL\Mutation;

class UpdateHero extends Mutation
{
    protected function typeResult(): Type
    {
        return new ObjectType([
            'name' => 'UpdateHeroResult',
            'fields' => [
                'error' => Type::boolean(),
                'message' => Type::string()
            ]
        ]);
    }

    /**
     * @return array
     */
    protected function args(): array
    {
        return [
            'id' => Type::nonNull(Type::int())
            'name' => Type::nonNull(Type::string())
        ];
    }

    /**
     * @return mixed
     */
    protected function resolve($root, $args, $context, $info)
    {
        Hero::find($args['id'])->update([
          'name' => $args['name']
        ]);
    
        return [
            'error' => false,
            'message' => 'Updated!'
        ];
    }
}

License

The Laravel GraphQL is open-sourced project licensed under the MIT license.

统计信息

  • 总下载量: 247
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 34
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 34
  • Watchers: 2
  • Forks: 2
  • 开发语言: Blade

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-01