کاپو/graphql-php

使用Curl从PHP消耗GraphQL

dev-master 2020-01-15 14:28 UTC

This package is not auto-updated.

Last update: 2024-10-03 13:18:41 UTC


README

由Kappo Bike编写的Composer库,用于使用Curl从PHP消耗GraphQL

使用composer安装

    composer require kappo/graphql-php

查询示例

使用getMaster.graphql文件创建此路径

/inc/queries/getMaster.graphql

使用格式化字符串以动态包含参数。

由零个或多个指令组成:普通字符(除%)直接复制到结果中,以及转换规格,其中每个规格都导致提取其自己的参数。这适用于sprintf()和printf()。[ https://php.ac.cn/manual/es/function.sprintf.php ]

    query {  
        listMaster %s {
            items{
                name
                createdAt
            }
        }
    }

在Master.php上创建一个类,扩展Kappo\Graphql

use Kappo\Graphql;

class Master extends Graphql
{
    
    public function __construct(){
        parent::__construct("[YOUR_ENDPOINT]","[YOUR_APYKEY]");
    }

    public function get(){
        
        $result = $this->gql_exec(array(
            'type' => 'query'
            ,'file' => file_get_contents(__DIR__.'/inc/queries/getMaster.graphql',true)
            ,'params' => array(
                'filter' => array(
                    "reference:" => "{eq:".$this->String("app")."}",
                    "stage:"     => "{eq:".$this->String("qa")."}",
                    "isActive:"  => "{eq:".$this->String("Yes")."}"
                )
            )
        ));

        print_r($result);

    }
}

$master = new Master();
$master->Get();

使用测试php文件

    php Master.php