mathsgod/gql-query-builder-php

用于构建 GraphQL 查询的 PHP 库

1.0.0 2023-04-03 08:53 UTC

This package is auto-updated.

Last update: 2024-09-19 05:58:45 UTC


README

PHP Composer

一个简单的辅助函数,用于使用 PHP 构建 GraphQL 查询、变更和订阅。

该项目基于 Node.js 的 GraphQL Query Builder

安装

composer require mathsgod/gql-query-builder-php

用法

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

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

选项

options 是 ["operation","field","variables"] 或一个选项数组

示例

  1. 查询
  2. 查询(带变量)
  3. 查询(带嵌套字段选择)
  4. 查询(带自定义参数名)
  5. 查询(带必需变量)
  6. 查询(带空字段)
  7. 变更
  8. 变更(带必需变量)
  9. 订阅

查询

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

print_r($query);

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

查询(带变量)

$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 = 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 = 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=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 = query([
    [
        "operation" => "getFilteredUsersCount",
    ],
    [
        "operation" => "getAllUsersCount",
        "fields" => []
    ],
    [
        "operation" => "getFilteredUsers",
        "fields" => [
            "count" => []
        ]
    ]
]);

print_r($query);

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

)
*/

变更

$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
        )

)

*/

变更(带必需变量)

$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
        )
)
    
*/

订阅

$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.
        )
)
    
*/