mathsgod/gql-query-builder-php 问题修复 & 功能扩展

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

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

mathsgod/gql-query-builder-php

最新稳定版本:1.0.0

Composer 安装命令:

composer require mathsgod/gql-query-builder-php

包简介

A PHP library to build GraphQL queries

README 文档

README

PHP Composer

A simple helper function to build GraphQL queries, mutations and subscriptions use PHP.

This project is based on GraphQL Query Builder for Node.js.

Install

composer require mathsgod/gql-query-builder-php

Usage

use function GQLQueryBuilder\query;
use function GQLQueryBuilder\mutation;
use function GQLQueryBuilder\subscription;

$query=query($options);
$mutation=mutation($options);
$subscription=subscription($options);

Options

options is ["operation","field","variables"] or an array of options

Examples

  1. Query
  2. Query (with variables)
  3. Query (with nested fields selection)
  4. Query (with custom argument name)
  5. Query (with required variables)
  6. Query (with empty fields)
  7. Mutation
  8. Mutation (with required variables)
  9. Subscription

Query:

$query=query([
    "operation"=>"thoughts",
    "fields"=>['id', 'name', 'thought']
]);

print_r($query);

// output:
/*
Array
(
    [query] => query { thoughts { id name thought } }
    [variables] => Array
        (
        )
)
*/

Query (with variables):

$query=query([
    "operation"=>"thoughts",
    "fields"=>['id', 'name', 'thought'],
    "variables"=>[
        "id"=>1,
    ]
]);

print_r($query);

// output:
/*
Array
(
    [query] => query ($id: Int) { thoughts(id: $id) { id name thought } }
    [variables] => Array
        (
            [id] => 1
        )
)
*/

Query (with nested fields selection):

$query = query([
    "operation" => "orders",
    "fields" => [
        "id",
        "amount",
        "user" => [
            "id",
            "name",
            "email",
            "address" => [
                "city",
                "country",
            ]
        ]
    ]
]);

// output:
/*
Array
(
    [query] => query { orders { id, amount, user { id, name, email, address { city, country } } } }
    [variables] => Array
        (
        )

)
*/

Query (with custom argument name):

$query = query([
    "operation" => "someoperation",
    "fields" => [
        [
            "operation" => "nestedoperation",
            "fields" => ['field1'],
            "variables" => [
                "id2" => [
                    "name" => "id",
                    "type" => "ID",
                    "value" => 123
                ]
            ],
        ]
    ],
    "variables" => [
        "id" => [
            "name" => "id",
            "type" => "ID",
            "value" => 456
        ]
    ]
]);

/*
Array
(
    [query] => query($id: ID, $id2: ID) { someoperation(id: $id) { nestedoperation (id: $id2)  { field1 }  } }
    [variables] => Array
        (
            [id] => 456
            [id2] => 123
        )

)
*/

Query (with required variables)

$query=query([
    "operation"=>"userLogin",
    "variables"=>[
        "email"=>[
            "value"=>"jon.doe@example.com",
            "required"=>true
        ],
        "password"=>[
            "value"=>"123456",
            "required"=>true
        ]
    ],
    "fields"=>["userId","token"]
]);

/*

Array
(
    [query] => query($email: String!, $password: String!) { userLogin(email: $email, password: $password) { userId, token } }
    [variables] => Array
        (
            [email] => jon.doe@example.com
            [password] => 123456
        )

)

*/

Query (with empty fields):

$query = query([
    [
        "operation" => "getFilteredUsersCount",
    ],
    [
        "operation" => "getAllUsersCount",
        "fields" => []
    ],
    [
        "operation" => "getFilteredUsers",
        "fields" => [
            "count" => []
        ]
    ]
]);

print_r($query);

/*
Array
(
    [query] => query { getFilteredUsersCount getAllUsersCount getFilteredUsers { count } }
    [variables] => Array
        (
        )

)
*/

Mutation:

$mutation=mutation([
    "operation"=>"createThought",
    "variables"=>[
        "name"=>"John Doe",
        "thought"=>"Hello World"
    ],
    "fields"=>["id","name","thought"]
]);

print_r($mutation);

/*

Array
(
    [query] => mutation($name: String, $thought: String) { createThought(name: $name, thought: $thought) { id name thought } }
    [variables] => Array
        (
            [name] => John Doe
            [thought] => Hello World
        )

)

*/

Mutation (with required variables):

$query = mutation([
    "operation" => "userSignup",
    "variables" => [
        "name" => [
            "value" => "Jon Doe",
        ],
        "email" => [
            "value" => "jon.doe@example.com", "required" => true
        ],
        "password" => [
            "value" => "123456", "required" => true
        ],
    ],
    "fields" => ["userId"]
]);

print_r($query);

/*

Array
(
    [query] => mutation($name: String, $email: String!, $password: String!) { userSignup(name: $name, email: $email, password: $password) { userId } }
    [variables] => Array
        (
            [name] => Jon Doe
            [email] => jon.doe@example.com
            [password] => 123456
        )
)
    
*/

Subscription:

$query = subscription([
    "operation" => "thoughtCreate",
    "variables" => [
        "name" => "Tyrion Lannister",
        "thought" => "I drink and I know things."
    ],
    "fields" => ["id"]
]);

print_r($query);

/*

Array
(
    [query] => subscription($name: String, $thought: String) { thoughtCreate(name: $name, thought: $thought) { id } }
    [variables] => Array
        (
            [name] => Tyrion Lannister
            [thought] => I drink and I know things.
        )
)
    
*/

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-03