rabelos-coder/php-graphql-client 问题修复 & 功能扩展

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

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

rabelos-coder/php-graphql-client

最新稳定版本:1.0.28

Composer 安装命令:

composer require rabelos-coder/php-graphql-client

包简介

A PHP library that simplifies the process of interacting with GraphQL API's by providing simple client.

README 文档

README

Latest Version Software License Total Downloads

PHP Client for GraphQL

Main features

  • Client with file attachment support
  • Easy query/mutation execution
  • Simple array results for mutation and queries
  • Powerful object results for mutation and queries

Installation

Via composer:

composer require rabelos-coder/php-graphql-client

Documentation

Instantiate a client

You can instantiate a simple client.

Simple Client:

<?php
$client = new \RabelosCoder\GraphQL\Client('https://your-domain/graphql');

Using the GraphQL Client

You can use the client to execute queries and mutations and get the results.

<?php

/**
 * Query Example
 */
$query = <<<'GQL'
query GetFooBar($idFoo: String, $idBar: String) {
  foo(id: $idFoo) {
    id_foo
    bar (id: $idBar) {
      id_bar
    }
  }
}
GQL;

$variables = [
    'idFoo' => 'foo',
    'idBar' => 'bar',
];

/** @var \RabelosCoder\GraphQL\Client $client */
$request = $client->query($query, $variables);

try {
    // returns response array
    $response = $request->send();

    return $response;
} catch (\Exception $e) {
    // Returns exception message
}

/**
 * Mutation Example
 */
$mutation = <<<'GQL'
mutation ($foo: ObjectInput!){
  CreateObjectMutation (object: $foo) {
    status
  }
}
GQL;

$variables = [
    'foo' => [
        'id_foo' => 'foo',
        'bar' => [
            'id_bar' => 'bar'
        ]
    ]
];

/** @var \RabelosCoder\GraphQL\Client $client */
$request = $client->query($mutation, $variables);

try {
    // returns response array
    $response = $request->send();

    return $response;
} catch (\Exception $e) {
    // Returns exception message
}

/**
 * Mutation With Single File Upload Example
 */
$mutation = <<<'GQL'
mutation ($file: Upload!){
  CreateObjectMutation (object: $file) {
    fileName
    filePath
  }
}
GQL;

$file = $_FILES['fieldName'];
$uploaded =  [
    'fileName' => $file['name'],
    'mimeType' => $file['type'],
    'filePath' => $file['tmp_name'],
];


$variables = [
    'file' => null,
];

/** @var \RabelosCoder\GraphQL\Client $client */
$request = $client->fileField('file')
            ->attachment($uploaded)
            ->query($mutation, $variables);

try {
    // returns response array
    $response = $request->send();

    return $response;
} catch (\Exception $e) {
    // Returns exception message
}

/**
 * Mutation With Multiple File Upload Example
 */
$mutation = <<<'GQL'
mutation ($files: [Upload!]!){
  CreateObjectMutation (object: $files) {
    fileName
    filePath
  }
}
GQL;

$files = $_FILES['fieldName']; // Remember that form field input name must contains [] at the end and the property multiple setted.
$uploaded = [];

foreach ($files as $file) {
    $uploaded[] = [
        'fileName' => $file['name'],
        'mimeType' => $file['type'],
        'filePath' => $file['tmp_name'],
    ];
}

$variables = [
    'files' => array_map(fn() => null, array_keys($uploaded)),
];

/** @var \RabelosCoder\GraphQL\Client $client */
$request = $client->filesField('files')
            ->attachments($uploaded)
            ->query($mutation, $variables);

try {
    // returns response array
    $response = $request->send();

    return $response;
} catch (\Exception $e) {
    // Returns exception message
}

In the previous examples, the client is used to execute queries and mutations. The response object is used to get the results in array format.

License

The MIT license. Please see LICENSE for more information.

统计信息

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

GitHub 信息

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

其他信息

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