定制 mathsgod/graphql-php-loader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

mathsgod/graphql-php-loader

Composer 安装命令:

composer require mathsgod/graphql-php-loader

包简介

A php loader for webonyx graphql

README 文档

README

PHP Composer

graphql-php-loader

A simple loader to generate Schema for webonyx/graphql-php

Usage

Use file structure to create and design graphql schema.

  1. Create a folder "graphql" at your document root.
  2. Create a folder "Query" in "graphql" folder.
  3. Now you can create field for Query

Init

use GraphQL\Loader;
use GraphQL\Type\Schema;

$loader=new Loader();

$config = SchemaConfig::create()
    ->setQuery($loader->queryType())
    ->setMutation($loader->mutationType());

$schema = new Schema($config);

Example

Simple query

Create a file hello.php in "graphql/Query" folder

graphql
+-- Query
|  +-- hello.php

Query/hello.php

return [
    "type"=>"String",
    "resolve"=>function($root,$args,$context){
        return "world!";
    }
];

It equivalent to build to following schema

type Query{
    hello:String
}

Custom type

If you want to use custom object type, just create type file at graphql folder

graphql
+-- Query
|  +-- me.php
+-- User.php

Query/me.php

return [
    "type"=>"User",
    "resolve"=>function($root,$args,$context){
        return $context->me; //return object
    }
];

User.php

return [
    "fields"=>[
        "first_name"=>"String",
        "last_name"=>"String"
    ]
];

It equivalent to build to following schema

type Query{
    me:User
}

type User{
    first_name:String
    last_name:String
}

Fields of custom type

If you want to create custom fields for custom object type, just create folder for the custom object

example

User has multiple phone number

graphql/User.php

return [
    "fields"=>[
        "first_name"=>"String",
        "last_name"=>"String"
    ]
];
/* 
no need create phone in fields,
by create file phone.php in User folder, it auto generate fields in User type
*/

create phone.php in folder "User"

graphql/User/phone.php

return [
    "type"=>"[String]",
    "resolve"=>function($user,$args,$context){
        return $user->getPhones(); //return multi phone
    }
];
graphql
+-- Query
|  +-- me.php
+-- User
|  +-- phone.php
+-- User.php

It equivalent to build to following schema

type Query{
    me:User
}

type User{
    first_name:String
    last_name:String
    phone:[String]
}

Sub folder structure

graphql
+-- Query
|  +-- User
|      +-- list.php
|  +-- Invoice
|      +-- list.php
+-- User.php
+-- Invoice.php

now you can query by following

query{
    User{
        list{
            first_name
            last_name
        }
    }
    Invoice{
        list{
            invoice_no
        }
    }
}

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固